Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIView动画中的setFrame不';不能移动,只能在适当的位置捕捉_Iphone_Objective C_Ipad_Uiview_Uiviewanimation - Fatal编程技术网

Iphone UIView动画中的setFrame不';不能移动,只能在适当的位置捕捉

Iphone UIView动画中的setFrame不';不能移动,只能在适当的位置捕捉,iphone,objective-c,ipad,uiview,uiviewanimation,Iphone,Objective C,Ipad,Uiview,Uiviewanimation,在我的viewDidLoad方法中,我将一个按钮放置在视图左侧的屏幕外 然后,我使用以下两种方法为其设置动画: -(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self showButton]; } 方法是: -(void) showButton { [myButton setTitle:[self getButtonTitle] forState:UIControlSta

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

然后,我使用以下两种方法为其设置动画:

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

    [self showButton];
}
方法是:

-(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];
}
该按钮立即出现,并且不会设置动画。此外,将立即调用animationDone选择器

为什么它不在屏幕上设置我的按钮的动画


编辑:这与试图在ViewDidDisplay中启动动画有关…

我尝试了你的动画代码,效果很好

在哪里设置按钮的初始帧?在开始动画之前,是否可能错误地将按钮的帧设置为
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];
}
正如您所看到的,我没有在您的动画代码中更改任何内容。所以我认为问题在于按钮的框架

有点离题:如果你没有为iOS<4构建应用程序,你可能想看看iOS 4.0附带的UIView的“带块动画”

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


看了你的评论,我的怀疑似乎不正确。用他的答案指出正确的方向。在调用动画之前,应将按钮放置在
viewdideappear
方法中或
showButton
方法中,以确保按钮放置在屏幕外部。viewDidLoad用于更多数据类型设置。任何视觉效果(如动画)都应该出现在ViewDid中。您已经确认了这一点,如果您稍等一点,它会起作用。

奇怪的是,如果我稍等一点,动画会起作用。动画调用在ViewDidDisplay中,但将按钮的位置也放在其中可能会有所帮助(在您调用showButton方法之前)。我尝试将按钮的位置放在ViewDidDisplay中,但首先出现了,但没用。我试着把它放在视图中,但没有效果。然而,我确实发现使用延迟为0.2的计时器可以使一切正常工作。您是否尝试过像我在上面的代码中那样做?因为它在那里工作。当视图出现时,你有很多需要绘制的元素吗?是的,我只是试了一下。我试着在那里设置框架,设置“中心”和其他一些东西。最后,我简单地设置了一个计时器,让它在ViewDid出现后0.2秒关闭,一切都是这样。我还是不知道为什么。