kusumoto_teruya kusumoto_teruya - 4 months ago 23
iOS Question

UIView animation not work

I'd like to make

settingView
on
myView
display and disappear with animation.

I call
-showSettingView


if (!self.settingView)
: the animation correctly work.

But,
else if (self.myView)
:
settingView
disappear WITHOUT animation.

How do I fix it to make
settingView
disappear with animation?

@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];
}
}


Thank you.

Answer

You dealloc the settings view before the animation starts. Try this:

[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];
                     }
     ];

*Also: If you are using auto layout you need to do one of these options:

A. Set the constrains of myView instead of the frame, before the block, and then call only layoutIfNeeded: inside the block.

B. Set the transform property in the animation block.