Swift CAShapeLayer,UIBezierPath绘制图形

前端之家收集整理的这篇文章主要介绍了Swift CAShapeLayer,UIBezierPath绘制图形前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、绘制矩形

//利用CAShapeLayer绘图一个简单的矩形
        let layer=CAShapeLayer()
        layer.frame=CGRectMake(110,100,150,100)
        layer.backgroundColor=UIColor.blackColor().CGColor
        view.layer.addSublayer(layer)

        //利用UIBezierPath绘制简单的矩形
        let path=UIBezierPath(rect: CGRectMake(110,100))
        let layer=CAShapeLayer()
        layer.path=path.CGPath
        //填充颜色
        layer.fillColor=UIColor.blackColor().CGColor
        //边框颜色
        layer.strokeColor=UIColor.redColor().CGColor
        view.layer.addSublayer(layer)

2、绘制圆形

let path=UIBezierPath(roundedRect: CGRectMake(100,50,100),cornerRadius: 50)
        let layer=CAShapeLayer()
        layer.path=path.CGPath
        layer.fillColor=UIColor.blackColor().CGColor
        layer.strokeColor=UIColor.greenColor().CGColor
        view.layer.addSublayer(layer)

        //制定起始角和半径画圆
        let radius:CGFloat=60.0
        let startAngle:CGFloat=0.0
        let endAngle:CGFloat=CGFloat(M_PI*2)
        let path=UIBezierPath(arcCenter: view.center,radius: radius,startAngle: startAngle,endAngle: endAngle,clockwise: true)
        let layer=CAShapeLayer()
        layer.path=path.CGPath
        layer.fillColor=UIColor.blackColor().CGColor
        layer.strokeColor=UIColor.greenColor().CGColor
        view.layer.addSublayer(layer)

3、画弧

let startPoint=CGPointMake(50,300)
        let endPoint=CGPointMake(300,300)
        let controlPoint=CGPointMake(170,50)

        let layer1=CALayer()
        layer1.frame=CGRectMake(startPoint.x,startPoint.y,5,5)
        layer1.backgroundColor=UIColor.redColor().CGColor

        let layer2=CALayer()
        layer2.frame=CGRectMake(endPoint.x,endPoint.y,5)
        layer2.backgroundColor=UIColor.redColor().CGColor

        let layer3=CALayer()
        layer3.frame=CGRectMake(controlPoint.x,controlPoint.y,5)
        layer3.backgroundColor=UIColor.redColor().CGColor

        let path=UIBezierPath()
        let layer=CAShapeLayer()
        path.moveToPoint(startPoint)
        path.addQuadCurveToPoint(endPoint,controlPoint: controlPoint)

        layer.path=path.CGPath
        layer.fillColor=UIColor.clearColor().CGColor
        layer.strokeColor=UIColor.blackColor().CGColor

        view.layer.addSublayer(layer)
        view.layer.addSublayer(layer1)
        view.layer.addSublayer(layer2)
        view.layer.addSublayer(layer3)
4、画弧2
 //
        let startPoint=CGPointMake(50,300)
        let controlPoint1=CGPointMake(120,180)
        let controlPoint2=CGPointMake(220,420)

        let layer1=CALayer()
        layer1.frame=CGRectMake(startPoint.x,5)
        layer2.backgroundColor=UIColor.redColor().CGColor

        let layer3=CALayer()
        layer3.frame=CGRectMake(controlPoint1.x,controlPoint1.y,5)
        layer3.backgroundColor=UIColor.redColor().CGColor

        let layer4=CALayer()
        layer4.frame=CGRectMake(controlPoint2.x,controlPoint2.y,5)
        layer4.backgroundColor=UIColor.redColor().CGColor

        let path=UIBezierPath()
        let layer=CAShapeLayer()
        path.moveToPoint(startPoint)
        path.addCurveToPoint(endPoint,controlPoint1:controlPoint1,controlPoint2: controlPoint2)

        layer.path=path.CGPath
        layer.fillColor=UIColor.clearColor().CGColor
        layer.strokeColor=UIColor.blackColor().CGColor

        view.layer.addSublayer(layer)
        view.layer.addSublayer(layer1)
        view.layer.addSublayer(layer2)
        view.layer.addSublayer(layer3)
        view.layer.addSublayer(layer4)

5、添加动画

let layer=CAShapeLayer()
        layer.backgroundColor=UIColor.lightGrayColor().CGColor
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue=0
        animation.toValue=1
        animation.duration=2
        layer.addAnimation(animation,forKey: "")
       view.layer.addSublayer(layer)

猜你在找的Swift相关文章