4

のサブビューの1つをアニメートし、UIWindowそれをUIWindow使用から削除するメソッドがありremoveFromSuperviewます。しかし、removeFromSuperviewアニメーションブロックの後に置くと、アニメーションが再生される前にからが削除されるため、アニメーションが表示さremoveFromSuperviewれません:-(アニメーションが最初に再生されてからサブビューが削除されるように遅延するにはどうすればよいですか?アニメーションブロックの後に試しましたが、表示されませんでしたアニメーションも何らかの理由でスリープするため、望ましい効果があります。UIViewUIWindowremoveFromSuperview[NSThread sleepForTimeInterval:1];

このメソッドの私のコード:

  - (void) animateAndRemove
    {
     NSObject *mainWindow = [[UIApplication sharedApplication] keyWindow];

     [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.8]; 
     UIView *theView = nil;
     for (UIView *currentView in [mainWindow subviews])
     {
      if (currentView.tag == 666)
      {
       currentView.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.0];
       theView = currentView;
       }
     }
     [UIView setAnimationTransition:  UIViewAnimationTransitionNone forView:theView cache:YES];
      [UIView commitAnimations]; 



 //[NSThread sleepForTimeInterval:1];

 [theView removeFromSuperview];


    }
4

2 に答える 2

16

アニメーションブロックの委任メカニズムを使用して、アニメーションが終了したときに何をするかを決定する必要があります。あなたの場合、

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:theView];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
 ....

これにより[theView removeFromSuperview]、アニメーションの完了後に確実に呼び出されます。

于 2010-04-16T13:15:00.247 に答える
7

iOS 4.0以降をターゲットにしている場合は、アニメーションブロックを使用できます。

[UIView animateWithDuration:0.2
     animations:^{view.alpha = 0.0;}
     completion:^(BOOL finished){ [view removeFromSuperview]; }];

(上記のコードはAppleのUIViewドキュメントからのものです)

于 2011-04-28T17:03:00.827 に答える