UIView动画不起作用

kus*_*uya 4 objective-c uiviewanimation ios

我想提出settingViewmyView显示和动画消失.
我打电话-showSettingView
if (!self.settingView):动画正常工作.
但是,else if (self.myView):settingView没有动画就消失了.
如何修复它以使settingView动画消失?

@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];
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Avi*_*oss 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];
                     }
     ];
Run Code Online (Sandbox Code Playgroud)

*另外:如果您使用的是自动布局,则需要执行以下选项之一:

A. myView在块之前设置而不是帧的约束,然后 layoutIfNeeded:在块内部调用.

B. transform在动画块中设置属性.