简体   繁体   中英

setFrame in UIView animation doesn't move, only snaps in place

In my viewDidLoad method I place a button off to the left of the view, off screen.

Then I use these two methods to animate it:

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self showButton];
}

And the method:

-(void) showButton {
    [myButton setTitle:[self getButtonTitle] forState:UIControlStateNormal];

    // animate in
    [UIView beginAnimations:@"button_in" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [myButton setFrame:kMyButtonFrameCenter]; // defined CGRect
    [UIView commitAnimations];
}

The button immediately appears and does not animate. Further, the animationDone selector is called immediately.

Why doesn't it animate my button into the screen?

EDIT: This has to have something to do with trying to start an animation while in viewDidAppear...

I tried your animation code and it works fine.

Where do set the initial frame of the button? Is it possible that you mistakenly set the button's frame to kMyButtonFrameCenter before you start the animation? That would explain why the animationDone selector is called immediately.

Here is the code that works:

-(void) showButton {
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [myButton setTitle:@"test" forState:UIControlStateNormal];
    myButton.frame = CGRectMake(-100.0, 100.0, 100.0, 30.0);
    [self.view addSubview:myButton];

    // animate in
    [UIView beginAnimations:@"button_in" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [myButton setFrame:CGRectMake(100.0, 100.0, 100.0, 30.0)]; 
    [UIView commitAnimations];
}

As you see I did not change anything in your animation code. So I think the problem is the button's frame.

A bit off-topic: If you don't have build your app for iOS < 4 you might want to have a look at UIView's "animation with blocks" that came with iOS 4.0.

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^void{myButton.frame = kMyButtonFrameCenter} completion:^(BOOL completed){NSLog(@"completed");}];

=== EDIT ===

After reading your comment, it seems that my suspicion is not correct. inspire48 points in the right direction with his answer. You should put the placement of the button inside the viewDidAppear method or in the showButton method to make sure that the button is placed outside the screen before you call the animation

Put the animation call in viewDidAppear. viewDidLoad is used for more data-type setup. Any visual effects such as animations should go in viewDidAppear. You've confirmed this—it works if you wait a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM