UIView动画中的setFrame不会移动,只会卡入到位

Tig*_*ing 5 iphone objective-c uiview ipad uiviewanimation

在我的viewDidLoad方法中,我将一个按钮放在视图的左侧,屏幕外.

然后我使用这两种方法来动画它:

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

    [self showButton];
}
Run Code Online (Sandbox Code Playgroud)

方法:

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

该按钮立即出现,不会设置动画.此外,立即调用animationDone选择器.

为什么不将我的按钮设置为屏幕动画?

编辑:这必须与在viewDidAppear中尝试启动动画有关...

joe*_*ern 6

我尝试了你的动画代码,它工作正常.

在哪里设置按钮的初始框架?您是否有可能kMyButtonFrameCenter在开始动画之前错误地将按钮的框架设置为?这可以解释为什么立即调用animationDone选择器.

这是有效的代码:

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

如您所见,我没有更改动画代码中的任何内容.所以我认为问题是按钮的框架.

有点偏离主题:如果您没有为iOS <4构建应用程序,您可能需要查看UIView的iOS 4.0附带的"带块的动画".

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

===编辑===

看完你的评论后,我的怀疑似乎不正确.以他的答案激励48指向正确的方向.你应该在viewDidAppear方法或方法中放置按钮的位置,showButton以确保调用动画之前按钮被放置在屏幕之外