单个 UIView 上的多个同时 UIViewAnimations [英] Multiple Simultaneous UIViewAnimations on a Single UIView
问题描述
目标:
在单个 UIView 上同时播放多个动画.
问题:
据我所知,这是不可能的.每个动画必须按顺序发生,不能重叠.
Goal:
Have several animations play at the same time on a single UIView.
Problem:
From what I can tell, this is not possible. Each animation has to occur in sequence and cannot overlap.
本质上,我希望单个 UIView 在垂直动画的同时进行水平动画.由于我使用的是 UIViewAnimationOptionRepeat,我无法在 onCompletion 中嵌套任何其他动画: 这是我想要工作但没有:>
In essence, I want a single UIView to animate vertically while animating horizontally at the same time. Since I'm using UIViewAnimationOptionRepeat, I cannot nest any other animation in onCompletion: Here's what I want to work but doesn't:
[UIView animateWithDuration:duration
delay:kANIMATION_DELAY
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
object.frame = CGRectMake(object.frame.origin.x + 40,
object.frame.origin.y,
object.frame.size.width,
object.frame.size.height);
[UIView animateWithDuration:duration
delay:kANIMATION_DELAY
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
object.frame = CGRectMake(object.frame.origin.x,
object.frame.origin.y - 40,
object.frame.size.width,
object.frame.size.height);
} completion:^(BOOL finished) {
}];
} completion:^(BOOL finished) {
}];
推荐答案
答案不是使用 UIViewAnimation 块,而是使用 CABasicAnimations.使用它们,您可以分别操纵中心坐标 (x,y).这是我的代码现在的样子:
The answer is not to use UIViewAnimation blocks but instead CABasicAnimations. With them you can manipulate the center coordinate (x,y) separately. Here's what my code looks like now:
UIView *view = views[i];
// Add the horizontal animation
CABasicAnimation *horizontal = [CABasicAnimation animationWithKeyPath:@"position.x"];
horizontal.delegate = self;
horizontal.fromValue = [NSNumber numberWithFloat:25.0];
horizontal.toValue = [NSNumber numberWithFloat:50.0];
horizontal.repeatCount = INFINITY;
horizontal.duration = 6;
horizontal.autoreverses = YES;
horizontal.beginTime = 0; // ignore delay time for now
horizontal.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:horizontal forKey:@"horizontal_animation"];
// Add the vertical animation
CABasicAnimation *vertical = [CABasicAnimation animationWithKeyPath:@"position.y"];
vertical.delegate = self;
vertical.fromValue = [NSNumber numberWithFloat:100.0];
vertical.toValue = [NSNumber numberWithFloat:400.0];
vertical.repeatCount = INFINITY;
vertical.duration = 2;
vertical.autoreverses = YES;
vertical.beginTime = 0; // ignore delay time for now
vertical.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[view.layer addAnimation:vertical forKey:@"vertical_animation"];
这允许我在两个单独的动画中处理重复、持续时间等.然后,当我的动画结束时,我可以调用 follow 方法来删除动画.要么,
This allows me to handle repeat, duration, etc. in two separate animations. Then, when my animations are over, I can just call the follow method to remove animations. Either,
[view.layer removeAllAnimations];
或者,如果我想删除更具体的动画,
or, if I want a more specific animation removed,
[view.layer removeAnimationForKey:@"vertical_animation"];
然后,如果您想对动画何时开始/停止进行更多自定义控制,您只需要像这样添加委托方法:
Then, if you want to some more custom control over when the animation starts/stops, you just need to add the delegate methods like so:
-(void)animationDidStart:(CAAnimation *)anim {
// custom code here
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// custom code here
}
这很甜蜜也很容易.希望这可以帮助任何有类似需求的人.干杯!
It's pretty sweet and easy. Hope this helps anyone in a similar need. Cheers!
这篇关于单个 UIView 上的多个同时 UIViewAnimations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!