//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 绘制、动画基础 (CAShapeLayer、CABasicAnimation)全部内容,希望文章能够帮你解决swift 绘制、动画基础 (CAShapeLayer、CABasicAnimation)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)