简体   繁体   English

Objective-C中的标签文本

[英]Label Text in Objective-C

I have problem about Label.Test 我对Label.Test有问题

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];

I'm trying to move the button around the screen and when I click the button show the score in the label. 我试图在屏幕上移动按钮,当我单击按钮时,在标签中显示分数。 Currently, when I click the button the score increases but my button moves back to its original locations. 当前,当我单击该按钮时,分数会增加,但是我的按钮会移回其原始位置。

How do I fix this? 我该如何解决?

The code you posted should animate your button to a new location. 您发布的代码应使按钮动画到新位置。 As holex says in his comment, you should really be using the newer methods like animateWithDuration:animations: and its variants. 正如Holex在他的评论中所说,您实际上应该使用较新的方法,例如animateWithDuration:animations:及其变体。

If you are using auto-layout (the default or XIB files and storyboards in Xcode 5 and later) then you can't animate a view (or, which is a type of view) by moving it's frame. 如果使用自动布局(Xcode 5和更高版本中的默认布局或XIB文件和情节提要),则无法通过移动框架来为视图(或视图类型)设置动画。 Instead you need to use position constraints, attach outlets to the constraints, and change the constraint's constant values. 相反,您需要使用位置约束,将出口连接到约束,并更改约束的常量值。

If you want to animate the frame, you need to turn off auto-layout on your XIB/Storyboard and use the older struts and springs style resizing. 如果要对框架进行动画处理,则需要在XIB / Storyboard上关闭自动布局,并使用较旧的支柱和弹簧样式调整大小。

Since you are using the storyboard with auto-layout it will always return to its position. 由于您将情节提要与自动布局一起使用,它将始终返回其位置。 If you plan on animating a view I would create it programmatically. 如果您打算对视图设置动画,则可以通过编程方式创建它。

    _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];

Just take the button off of the storyboard and create your button like that. 只需将按钮从情节提要板上移除,然后像这样创建按钮即可。 You will be able to move it anywhere you want. 您将可以将其移动到所需的任何位置。 Its easier then changing the storyboard constraints programmatically. 通过编程更改情节提要约束比较容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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