2

次のコードには、次々に呼び出される 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::確かに、beginAnimationUIViewをアニメーション化する古い方法です。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の基本を学ぶためにしばらく時間がかかることになりますが、2つの関連するアニメーションで発生した同様の問題を解決できる唯一の方法でした。

それが役に立てば幸い。

于 2012-12-25T16:44:07.443 に答える