Greg
Greg

Reputation: 451

animating strokeEnd in a CAShapeLayer using an animation block in Swift

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?

Upvotes: 2

Views: 1675

Answers (1)

Orion
Orion

Reputation: 1276

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

Upvotes: 5

Related Questions