iPhone UIView 动画持续时间只能立即生效
这段代码在子类 uiview 的 Touchsend 方法中执行,但是它没有动画,它只是立即更改背景颜色。怎么了?
self.backgroundColor = [UIColor blackColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:20];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if (score < .40) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor greenColor];
}
[UIView commitAnimations];
This code is being executed in the touchesended method of a subclassed uiview, however it doesn't animate, it just instantly changes the background color. What is wrong?
self.backgroundColor = [UIColor blackColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:20];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if (score < .40) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor greenColor];
}
[UIView commitAnimations];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[UIView commitAnimations]
方法不是阻塞的 - 因此,如果您调用它,它将开始动画,但它将立即执行此点之后的代码。如果它下面有任何改变背景颜色的代码,它会破坏你的动画。如果这不起作用,请尝试为不同的属性设置动画 - 我清楚地知道我的头顶位置和位置。
alpha
是可动画的,但我不确定颜色。 (Alpha只是一个float
值,center只是一个CGPoint
,两者都是基元,但UIColor
是一个引用类型,而UIView可能不知道如何为指针设置动画。)The
[UIView commitAnimations]
method isn't blocking - so if you call it, it will start animating, but it will execute code after this point instantly. If you have any code below it that changes the background color, it will break your animations.If that doesn't work, try animating a different property - I know off the top of my head position &
alpha
are animatable, but I'm uncertain of Color. (Alpha is just afloat
value, and center is just aCGPoint
, both of which are primitives, butUIColor
is a referenced type and UIView may not know how to animate a pointer.)