2014-04-13 117 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) {} 
];