Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 728
kusumoto_teruya Avatar asked Feb 13 '23 05:02

kusumoto_teruya


2 Answers

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.

like image 98
Aviel Gross Avatar answered Feb 16 '23 03:02

Aviel Gross


Try adding this line [self layoutIfNeeded]; as the last line within your animation block. Something like that:

[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) {}
];
like image 22
Solomiya Avatar answered Feb 16 '23 02:02

Solomiya