2

在下一个代码中,有 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];
    }
4

1 回答 1

2

您可能遇到了基于 UIView 的动画的某种限制。您可能会尝试的几件事是:

  1. animateWithDuration也用于:hideTabBar:确实,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];
    }
    
  2. 使用 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 基础知识,但这是我可以解决我在两个相关动画中遇到的类似问题的唯一方法。

希望能帮助到你。

于 2012-12-25T16:44:07.443 回答