2014-04-13 179 views
4

我想使settingViewmyView顯示並隨動畫消失。
我致電-showSettingView
if (!self.settingView):動畫正常工作。
但是,else if (self.myView)settingView沒有動畫就消失了。
如何修復它使settingView隨動畫消失?UIView動畫不起作用

@property (nonatomic, strong) UIView *myView; 
@property (nonatomic, strong) UIView *settingView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIBarButtonItem* settingButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettingView)]; 
    self.navigationItem.rightBarButtonItem = settingButton; 
} 

- (void)showSettingView 
{ 
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 568, 320, 480)]; 
    self.myView.opaque = NO; 
    self.myView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f]; 
    [self.view addSubview:self.myView]; 

    if (!self.settingView) { 
     self.settingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; 
     self.settingView.backgroundColor = [UIColor blackColor]; 
     [self.myView addSubview:self.settingView]; 
     [UIView animateWithDuration:0.3f 
           delay:0.0f 
          options:UIViewAnimationOptionTransitionFlipFromBottom 
         animations:^{self.myView.frame = CGRectMake(0, 60, 320, 480);} 
         completion:^(BOOL finished) {} 
     ]; 
    } else if (self.myView){ 
     self.myView.frame = CGRectMake(0, 60, 320, 480); 
     [UIView animateWithDuration:0.3f 
           delay:0.0f 
          options:UIViewAnimationOptionCurveEaseIn 
         animations:^{self.myView.frame = CGRectMake(0, 568, 320, 480);} 
         completion:^(BOOL finished) {} 
     ]; 
     [self.settingView removeFromSuperview]; 
     self.settingView = nil; 
     [self.myView removeFromSuperview]; 
    } 
} 

謝謝。

回答

6

您可以在動畫開始之前解除設置視圖。試試這個:

[UIView animateWithDuration:0.3f 
          delay:0.0f 
         options:UIViewAnimationOptionCurveEaseIn 
        animations:^{self.myView.frame = CGRectMake(0, 568, 320, 480);} 
        completion:^(BOOL finished) { 
         [self.settingView removeFromSuperview]; 
         self.settingView = nil; 
         [self.myView removeFromSuperview]; 
        } 
    ]; 

*另外:如果您在使用自動佈局,你需要做的其中一個選項:

A.設置的myView而不是框架的約束,該塊之前,然後在塊內部調用只有layoutIfNeeded:

B.在動畫塊中設置transform屬性。

2

嘗試添加此行 [self layoutIfNeeded]; 作爲您的動畫塊中的最後一行。類似的東西:

[UIView animateWithDuration:0.3f 
         delay:0.0f 
        options:UIViewAnimationOptionTransitionFlipFromBottom 
       animations:^{ 
        self.myView.frame = CGRectMake(0, 60, 320, 480); 
        // any other stuff you have to move 

        [self layoutIfNeeded]; 
       } 
       completion:^(BOOL finished) {} 
];