Change CAShapeLayer without Animation
I want to set the strokeEnd
property of a CAShapeLayer
without the default animation, with no animation at all. I've looked around to try to find how to do this but everything seems to be about how to animate properties.
In Core Animations terminology, a more general term for an animation is an "action". You can for example see that the CAAnimation
conforms to the CAAction
protocol. You also see the terminology "action" used when disabling them (disabling the animations).
There are many different ways of changing the actions of a layer. Many of them are documented pretty well in the discussion of the actionForKey:
documentation on CALayer
(excerpt below). Some of them are more relevant when subclassing (and you can also override actionForKey:
in your subclass to add more implicit actions for new keys.
This method searches for the layer's associated actions in the following order:
nil
if it does not handle the action. NSNull
object if it does not handle the action and the search should be terminated. actions
dictionary. The two ways that are most interesting when you want to disable animation are (two different because they are used for slightly different things):
CATransaction
(not mentioned above) [NSNull null]
for the @"strokeEnd"
key in the actions dictionary (number 2 above) Disabling actions
Using a transaction to disable animations is good when you temporarily want to disable actions completely for a number of different properties while still having animations everywhere else. In code it looks something like this:
[CATransaction begin];
[CATransaction setDisableActions:YES];
// change your property here
yourShapeLayer.strokeEnd = 0.7;
[CATransaction commit]; // animations are disabled until here...
Changing the actions dictionary
You can permanently change the default animation for one or more keys by modifying the layers action dictionary. Setting [NSNull null]
means that there should be no animation and that the layer should stop looking elsewhere for a default animation. You can also use this to add animatable properties. Removing an animation using the action dictionary looks something like this:
yourShapeLayer.actions = @{@"strokeEnd": [NSNull null]};
yourShapeLayer.strokeEnd = 0.7; // this won't have an animation
You can do something like the following:
NSDictionary *actions = @{@"strokeEnd": [NSNull null]};
yourShapeLayer.actions = actions;
This will make it so it won't animate for the property. You could also specify additional properties to not animate by adding them to this:
NSDictionary *actions = @{@"strokeEnd": [NSNull null], @"position": [NSNull null], @"position": [NSNull null]};
链接地址: http://www.djcxy.com/p/74254.html
上一篇: 带CAShapeLayer的CAReplicatorLayer
下一篇: 没有动画改变CAShapeLayer