在接下来的代码中,有 2 个方法被依次调用。第一个使中心按钮消失,第二个使选项卡栏消失。分开来说,他们工作得很好。
我的问题是,当我尝试依次调用它们时, hideCenterButton
没有动画。该按钮不会滚动到屏幕左侧,而是消失。
-(void)hideCenterButton:(BOOL)animated
{
if(animated){
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = -100;
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}}
...
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//[UIView setAnimationDelay:1];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]] || [view isKindOfClass:[UIImageView class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
最佳答案
您可能遇到了基于 UIView 的动画的某种限制。您可以尝试的几件事是:
对
hideTabBar:
也使用animateWithDuration
:事实上,beginAnimation
是 UIView 动画的旧方法; animateWithDuration 是较新的(随 iOS4 引入);通过在机器人案例中使用相同的动画调用,您可能会得到更好的结果;这应该很简单;这应该有效:- (void)hideTabBar:(UITabBarController *) tabbarcontroller { [UIView animateWithDuration:0.3 delay:0.0f options:UIViewAnimationCurveLinear animations:^{ for(UIView *view in tabbarcontroller.view.subviews) { if([view isKindOfClass:[UITabBar class]] || [view isKindOfClass:[UIImageView class]]) { [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)]; } else { [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)]; } } } completion:nil]; }
使用核心动画重写动画:这是一个稍微低级的机制,但它可以让您在性能方面获得最佳结果;例如,这就是重写第一个动画的方法:
首先您需要提供回调:
- (void)animationDidStop:(CABasicAnimation*)anim finished:(BOOL)flag {
CALayer* layer = [anim valueForKey:@"animationLayer"];
layer.position = [anim.toValue CGPointValue];
}
然后你可以像这样为按钮设置动画:
CABasicAnimation* move = [CABasicAnimation animationWithKeyPath:@"position"];
CGPoint pos = self.centerButton.center;
pos.x -= 100;
move.toValue = [NSValue valueWithCGPoint:pos];
move.duration = 0.3;
move.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
move.removedOnCompletion = NO;
move.fillMode = kCAFillModeForwards;
move.autoreverses = NO;
move.delegate = self;
[move setValue:self.centerButton.layer forKey:@"animationLayer"];
[self.centerButton.layer addAnimation:move forKey:@"buttonPosition"];
使用核心动画,您最终将编写更多代码,并且需要花一些时间来学习核心动画基础知识,但这是我解决两个相关动画遇到的类似问题的唯一方法。
希望有帮助。
关于objective-c - 尝试让 2 个动画同时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032361/