Ios Objective-C中的标签文本

Ios Objective-C中的标签文本,ios,objective-c,uiview,Ios,Objective C,Uiview,我对标签有问题。测试 CGRect frame = _gobutton.frame; frame.origin.x=rand()%330+1; frame.origin.y=rand()%550+1; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; _gobutton.frame = frame; [UIView commitAnimations]; i=i+1; self.score.

我对标签有问题。测试

CGRect frame = _gobutton.frame;
frame.origin.x=rand()%330+1;
frame.origin.y=rand()%550+1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
_gobutton.frame = frame;
[UIView commitAnimations];

i=i+1;

self.score.Text = [NSString stringWithFormat:@"%i",i];
我试图在屏幕上移动按钮,当我点击按钮时,会在标签上显示分数。当前,当我单击按钮时,分数会增加,但我的按钮会移回其原始位置


如何修复此问题?

您发布的代码应将您的按钮动画化到新位置。正如holex在他的评论中所说,你真的应该使用更新的方法,比如animateWithDuration:animations:及其变体

如果在Xcode 5及更高版本中使用“自动布局”默认或XIB文件和故事板,则无法通过移动帧来设置视图或脚本的动画。相反,您需要使用位置约束,将插座附着到约束,并更改约束的常量值


如果要设置帧的动画,需要关闭XIB/情节提要上的“自动布局”,并使用较旧的struts and springs样式调整大小。

由于使用具有“自动布局”的情节提要,它将始终返回其位置。 如果您计划为视图设置动画,我将以编程方式创建它

    _gobutton = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];

     [_gobutton addTarget:self action:@selector(yourButtonSelector:) forControlEvents:UIControlEventTouchUpInside];

     [_gobutton setBackgroundImage:[UIImage imageNamed:@"aphoto"] forState:UIControlStateNormal];

     [self.view addSubview:_gobutton];

只需从故事板上取下按钮,然后像那样创建按钮。你可以把它移到任何你想要的地方。通过编程更改情节提要约束更容易。

这可能不是问题的根源,但从iOS4开始,强烈建议使用基于块的动画。是的,4。再来一次:从iOS4开始。我们现在是版本8。
    _gobutton = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];

     [_gobutton addTarget:self action:@selector(yourButtonSelector:) forControlEvents:UIControlEventTouchUpInside];

     [_gobutton setBackgroundImage:[UIImage imageNamed:@"aphoto"] forState:UIControlStateNormal];

     [self.view addSubview:_gobutton];