创建自定义动画属性 [英] Create a custom animatable property
问题描述
在 UIView
上,您可以更改backgroundColour动画。在 UISlideView
上,您可以更改动画值。
您可以将自定义属性添加到您自己的 UIView
子类,以便它可以设置动画?
如果我有一个 CGPath
在我的 UIView
中,然后我可以通过更改路径绘制的百分比来设置它的绘图。
我可以将该动画封装到子类中。
ie我有一个 UIView
,带有 CGPath
,可以创建一个圆圈。
如果圆圈不存在则代表0%。如果圆圈已满,则表示100%。我可以通过改变路径绘制的百分比来绘制任何值。我还可以通过动画 CGPath
的百分比并重新绘制路径来动画更改(在UIView子类中)。
<我可以在
UIView
上设置一些属性(即百分比),这样我就可以将更改保持在 UIView animateWithDuration
阻止并为路径百分比的变化设置动画? 我希望我已经解释了我想做的事情。
基本上,我想做的就是... ...
[UIView animateWithDuration:1.0
动画:^ {
myCircleView.percentage = 0.7;
}
完成:nil];
以及给定百分比设置的圆形路径。
如果您扩展CALayer并实现自定义
- (void) drawInContext:(CGContextRef)context
你可以通过覆盖 needsDisplayForKey来创建一个可动画的属性
(在您的自定义CALayer类中),如下所示:
+(BOOL)needsDisplayForKey:(NSString * )key {
if([key isEqualToString:@percentage]){
return YES;
}
return [super needsDisplayForKey:key];
}
当然,你还需要一个 @ property
名为百分比
。从现在开始,您可以使用核心动画为百分比属性设置动画。我也没有使用 [UIView animateWithDuration ...]
调用来检查它是否有效。它可能会奏效。但这对我有用:
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@percentage];
animation.duration = 1.0;
animation.fromValue = [NSNumber numberWithDouble:0];
animation.toValue = [NSNumber numberWithDouble:100];
[myCustomLayer addAnimation:animation forKey:@animatePercentage];
哦,并使用 yourCustomLayer
myCircleView
,执行此操作:
[myCircleView.layer addSublayer:myCustomLayer];
On UIView
you can change the backgroundColour animated. And on a UISlideView
you can change the value animated.
Can you add a custom property to your own UIView
subclass so that it can be animated?
If I have a CGPath
within my UIView
then I can animate the drawing of it by changing the percentage drawn of the path.
Can I encapsulate that animation into the subclass.
i.e. I have a UIView
with a CGPath
that creates a circle.
If the circle is not there it represents 0%. If the circle is full it represents 100%. I can draw any value by changing the percentage drawn of the path. I can also animate the change (within the UIView subclass) by animating the percentage of the CGPath
and redrawing the path.
Can I set some property (i.e. percentage) on the UIView
so that I can stick the change into a UIView animateWithDuration
block and it animate the change of the percentage of the path?
I hope I have explained what I would like to do well.
Essentially, all I want to do is something like...
[UIView animateWithDuration:1.0
animations:^{
myCircleView.percentage = 0.7;
}
completion:nil];
and the circle path animate to the given percentage.
If you extend CALayer and implement your custom
- (void) drawInContext:(CGContextRef) context
You can make an animatable property by overriding needsDisplayForKey
(in your custom CALayer class) like this:
+ (BOOL) needsDisplayForKey:(NSString *) key {
if ([key isEqualToString:@"percentage"]) {
return YES;
}
return [super needsDisplayForKey:key];
}
Of course, you also need to have a @property
called percentage
. From now on you can animate the percentage property using core animation. I did not check whether it works using the [UIView animateWithDuration...]
call as well. It might work. But this worked for me:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"percentage"];
animation.duration = 1.0;
animation.fromValue = [NSNumber numberWithDouble:0];
animation.toValue = [NSNumber numberWithDouble:100];
[myCustomLayer addAnimation:animation forKey:@"animatePercentage"];
Oh and to use yourCustomLayer
with myCircleView
, do this:
[myCircleView.layer addSublayer:myCustomLayer];
这篇关于创建自定义动画属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!