在下一个代码中,有两个方法一个接一个地调用。第一个使中心按钮消失,第二个使标签消失。另外他们工作正常。
我的问题是,当我尝试一个接一个地调用它们时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];
}
答案 0 :(得分:2)
您可能已经遇到了基于UIView的动画的某种限制。您可能尝试的几件事情是:
也为animateWithDuration
使用hideTabBar:
:事实上,beginAnimation
是动画UIView的老路; animateWithDuration是最近的(iOS4引入);通过在bot案例中使用相同的动画调用,您可能会获得更好的结果;这应该是直截了当的;这应该有效:
- (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];
}
使用Core Animation重写动画:这是一种稍低级别的机制,但它可以让您在性能方面获得最佳效果;这就是,例如,你如何重写第一个动画:
首先你需要提供一个回调:
- (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"];
使用Core Animation,你最终会编写更多的代码,你需要花一些时间来学习Core Animation的基础知识,但这是我解决类似问题的唯一方法,我有两个相关的动画。
希望它有所帮助。