animating strokeEnd in a CAShapeLayer using an animation block in Swift

陌路散爱 提交于 2019-12-06 10:50:31

问题


I am attempting to trace out the shape of a path that I've place into a CAShapeLayer. In this function, the points in the points array represent the vertices of a regular polygon. The animation isn't happening, though. It's just appearing, totally animated.

Here is the code:

private func drawShape (point: [(x: Double, y:Double)]) {

    let shapeLayer = CAShapeLayer()

    let shapePath = UIBezierPath()
    shapePath.moveToPoint(CGPoint(x:point[point.count-1].x, y:point[point.count-1].y))

    for i in point {
        shapePath.addLineToPoint(CGPoint(x:i.x, y:i.y))
    }

    shapeLayer.path = shapePath.CGPath

    drawingSurface.layer.addSublayer(shapeLayer)

    shapeLayer.fillColor = UIColor.clearColor().CGColor
    shapeLayer.strokeColor = UIColor.blackColor().CGColor
    shapeLayer.strokeEnd = 0

    UIView.animateWithDuration(30, delay: 0, options: .CurveEaseInOut, animations: {
        shapeLayer.strokeEnd = 1
        }, completion: {finished in println("This Finished")
    })
}

What I expect is that it will draw the stroke over the duration that I have set (30 seconds at the moment), but instead, it just appears, fully drawn with no animation.

Any thoughts?


回答1:


I'm sure you already found the answer but from what I understand is that the animateWithDuration method can only animate about 8 different properties and the EndAngle is not one of them. Only CAAnimations can do this. animateWithDuration and CAAnimations are built for different things.

Quote from the ios documentation:

The following properties of the UIView class are animatable:

@property frame

@property bounds

@property center

@property transform

@property alpha

@property backgroundColor

@property contentStretch



来源:https://stackoverflow.com/questions/28784108/animating-strokeend-in-a-cashapelayer-using-an-animation-block-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!