Created
January 7, 2016 14:02
-
-
Save mzsima/0ffdd87b3fda0292f38f to your computer and use it in GitHub Desktop.
subtract shape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// SubtractShape | |
// | |
// Created by MizushimaYusuke on 1/7/16. | |
// Copyright © 2016 MizushimaYusuke. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
var shapes = [CAShapeLayer]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = UIColor.darkGrayColor() | |
createShape() | |
createButton() | |
} | |
func createShape() { | |
let path = UIBezierPath() | |
path.moveToPoint(CGPoint(x: -100, y: -120)) | |
path.addLineToPoint(CGPoint(x: 100, y: -120)) | |
path.addLineToPoint(CGPoint(x: 0, y: 150)) | |
path.closePath() | |
let shape = CAShapeLayer() | |
shape.path = path.CGPath | |
shape.fillColor = UIColor.clearColor().CGColor | |
shape.strokeColor = UIColor.orangeColor().CGColor | |
shape.lineWidth = 5 | |
shape.position = CGPoint(x: CGRectGetMidX(view.bounds), y: 200) | |
view.layer.addSublayer(shape) | |
shapes.append(shape) | |
} | |
func createButton() { | |
let btn = UIButton(type: .System) | |
btn.setTitle("→", forState: .Normal) | |
btn.titleLabel?.font = UIFont.boldSystemFontOfSize(50) | |
btn.sizeToFit() | |
btn.center = CGPoint(x: CGRectGetMidX(view.bounds), y: 400) | |
view.addSubview(btn) | |
btn.addTarget(self, action: "next", forControlEvents: .TouchUpInside) | |
} | |
func next() { | |
if let shapecopy = shapes.last?.copy() as? CAShapeLayer { | |
let path = UIBezierPath(CGPath: shapecopy.path!) | |
path.applyTransform(CGAffineTransformMakeRotation(CGFloat(M_PI) / 12.0)) | |
shapecopy.path = path.CGPath | |
view.layer.addSublayer(shapecopy) | |
shapes.append(shapecopy) | |
subtractShapes() | |
} | |
} | |
func subtractShapes() { | |
var mask : CAShapeLayer? = self.view.layer.sublayers?.filter { $0.name == "mask" }.first as? CAShapeLayer | |
if mask == nil { | |
mask = CAShapeLayer() | |
mask!.name = "mask" | |
mask!.lineWidth = 0 | |
mask!.position = CGPoint(x: CGRectGetMidX(view.bounds), y: 200) | |
mask!.fillRule = kCAFillRuleEvenOdd | |
mask!.fillColor = UIColor.orangeColor().CGColor | |
view.layer.addSublayer(mask!) | |
} | |
let maskPath = UIBezierPath() | |
maskPath.usesEvenOddFillRule = true | |
for s in shapes { | |
s.strokeColor = UIColor.clearColor().CGColor | |
let stroke = CGPathCreateCopyByStrokingPath(s.path!, nil, 5, .Butt, .Miter, 0) | |
maskPath.appendPath(UIBezierPath(CGPath: stroke!)) | |
} | |
mask!.path = maskPath.CGPath | |
} | |
} | |
extension CAShapeLayer { | |
public override func copy() -> AnyObject { | |
let copyLayer = CAShapeLayer() | |
copyLayer.position = self.position | |
copyLayer.fillColor = self.fillColor | |
copyLayer.strokeColor = self.strokeColor | |
copyLayer.lineWidth = self.lineWidth | |
copyLayer.path = self.path! | |
copyLayer.transform = self.transform | |
return copyLayer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4721