swift 绘制、动画基础 (CAShapeLayer、CABasicAnimation)

//MARK: 绘制虚线
        let layer0 = CAShapeLayer()
        layer0.strokeColor = UIColor.green.cgColor
        layer0.lineWidth = 0.5
        //线段宽度10 间距10
        let arr :NSArray = NSArray(array: [10,10])
        //基于线段的起始位置
        layer0.lineDashPhase = 0
        layer0.lineDashPattern = arr as? [NSNumber]
        self.view.layer.addSublayer(layer0)
        //路径
        let mdotteShapePath = CGMutablePath()
        mdotteShapePath.move(to: CGPoint(x:0,y: 30))
        mdotteShapePath.addLine(to: CGPoint(x:UIScreen.main.bounds.size.width,y:30))
        layer0.path = mdotteShapePath

        //MARK: 实心矩形
        let layer = CAShapeLayer()
        layer.frame = CGRect(x:80,y:50,width:150,height:50)
        layer.backgroundColor = UIColor.green.cgColor
        view.layer.addSublayer(layer)

        //MARK: 矩形框
        let layer1 = CAShapeLayer()
        layer1.fillColor = UIColor.clear.cgColor
        layer1.strokeColor = UIColor.green.cgColor
        view.layer.addSublayer(layer1)

        let path1 = UIBezierPath(rect: CGRect(x:80,y:110,height:50))
        layer1.path = path1.cgPath

        //MARK: 圆角矩形
        let path2 = UIBezierPath(roundedRect: CGRect(x:80,y:170,height:50),cornerRadius: 50)
        let layer2 = CAShapeLayer()
        layer2.path = path2.cgPath
        layer2.fillColor = UIColor.clear.cgColor
        layer2.strokeColor = UIColor.green.cgColor
        view.layer.addSublayer(layer2)

        //MARK: 圆形
        let radius: CGFloat = 20
        let startAngle: CGFloat = 0.0
        let endAngle: CGFloat = CGFloat(M_PI * 2)
        let path3 = UIBezierPath(arcCenter: view.center,radius: radius,startAngle: startAngle,endAngle: endAngle,clockwise: true)
        let layer3 = CAShapeLayer()
        layer3.path = path3.cgPath
        layer3.fillColor = UIColor.clear.cgColor
        layer3.strokeColor = UIColor.green.cgColor
        view.layer.addSublayer(layer3)

        //MARK: 贝塞尔曲线
        let startPoint = CGPoint(x:20,y:400)
        let endPoint = CGPoint(x:320,y:400)
        let controlPoint = CGPoint(x:170,y:300)
        let controlPoint1 = CGPoint(x:120,y:300)
        let controlPoint2 = CGPoint(x:220,y:500)

       

        //曲线1
        let path10 = UIBezierPath()
        let layer10 = CAShapeLayer()
        path10.move(to: startPoint)
        path10.addQuadCurve(to: endPoint,controlPoint: controlPoint)
        layer10.path = path10.cgPath
        layer10.fillColor = UIColor.clear.cgColor
        layer10.strokeColor = UIColor.black.cgColor
        view.layer.addSublayer(layer10)


        //曲线2
        let path20 = UIBezierPath()
        let layer20 = CAShapeLayer()
        path20.move(to: startPoint)
        path20.addCurve(to: endPoint,controlPoint1: controlPoint1,controlPoint2: controlPoint2)
        layer20.path = path20.cgPath
        layer20.fillColor = UIColor.clear.cgColor
        layer20.strokeColor = UIColor.black.cgColor
        view.layer.addSublayer(layer20)

        //MARK: 动画
        //动画1
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 1
        animation.duration = 2
        layer10.add(animation,forKey: "")

        //动画2
        layer20.strokeStart = 0
        layer20.strokeEnd = 1
        let animation21 = CABasicAnimation(keyPath: "strokeStart")
        //起始位置中间 结束为止左端
        animation21.fromValue = 0.5
        animation21.toValue = 0
        animation21.duration = 2
        let animation22 = CABasicAnimation(keyPath: "strokeEnd")
        //起始位置中间 结束为止右端
        animation22.fromValue = 0.5
        animation22.toValue = 1
        animation22.duration = 2
        layer20.add(animation21,forKey: "")
        layer20.add(animation22,forKey: "")

        //动画3
        let animation23 = CABasicAnimation(keyPath: "lineWidth")
        animation23.fromValue = 1
        animation23.toValue = 10
        animation23.duration = 5
        layer1.add(animation23,forKey: "")

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...