按下时更改UIButton的位置
按下时,我在随机位置移动UIButton时出现问题。练习是制作一个应用程序,用于统计按钮上的按键。在每次点击时,按钮必须改变其在屏幕上的位置。这是我的代码:按下时更改UIButton的位置
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)buttonPressed:(id)sender {
//[self tapCountMethod];
}
- (IBAction)button:(UIButton *)sender {
UIButton *button = (UIButton *)sender;
int xmin = ([button frame].size.width)/2;
int ymin = ([button frame].size.height)/2;
int x = xmin + arc4random_uniform(self.view.frame.size.width - button.frame.size.width);
int y = ymin + arc4random_uniform(self.view.frame.size.height - button.frame.size.height);
[button setCenter:CGPointMake(x, y)];
[button addTarget:self action: @selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
}
- (void) tapCounter {
tapCount ++;
labelCount.text = [NSString stringWithFormat:@"%ld times", (long)tapCount];
}
@end
因此,当我在buttonpressed中调用tapCount方法时,按钮停止移动。有人可以告诉我为什么会发生这种情况,以及我错了什么。你有什么建议请告诉我。我必须在哪里调用count方法?
因此,经过多次尝试和研究,我找到了解决方案。这是我最后的代码。
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.button.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
nil];
self.button.imageView.animationDuration = 0.7; //whatever you want (in seconds)
[self.button.imageView startAnimating];
self.labelCount.textColor = [UIColor colorWithRed:64./255. green:64./255. blue:64./255. alpha:1.];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (IBAction)buttonPressed:(id)sender {
NSInteger counter = self.tapCount;
if (counter > 0)
[self.labelCount setText:[NSString stringWithFormat:@"%ld",((long)counter + 1)]];
self.tapCount ++;
NSLog(@"%ld",(long)self.tapCount);
}
- (IBAction)button:(UIButton *)sender {
int xmin = ([self.button frame].size.width)/2;
int ymin = ([self.button frame].size.height)/2;
int x = xmin + arc4random_uniform(self.view.frame.size.width - self.button.frame.size.width);
int y = ymin + arc4random_uniform(self.view.frame.size.height - self.button.frame.size.height);
[self.button setCenter:CGPointMake(x, y)];
[self.button addTarget:self action: @selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[self.labelCount setText:[NSString stringWithFormat:@"1"]];
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
@end
谢谢大家
仍然似乎奇怪,你重新添加buttonPressed作为行动到你的按钮,每次按下它。顺便说一句,它看起来像你的按钮不能落在视图框架中的任何地方,因为你应该减去xmin和ymin,而不是完整的按钮高度或宽度。 – RobP 2014-12-05 17:30:09
我明白你的意思,但是当我只将按钮作为动作添加到我的按钮时,标签的文字不会改变。我在模拟器和设备上测试了应用程序,它的功能就像一个魅力。按钮通过屏幕。 – Newbie 2014-12-05 17:35:13
你应该做的就是在界面生成器中将按钮连接到它的动作*一次*。也许你想要的是,每当按钮被按下时,你想要按钮:被调用,在button:方法中你调用buttonPressed:方法来更新标签。 – RobP 2014-12-05 17:39:12
@property(强,非原子)IBOutlet中的UIButton *按钮; (IBAction)按钮:(UIButton *)发送者{int xmin =([self.button frame] .size.width)/ 2; int ymin =([self.button frame] .size.height)/ 2; int x = xmin + arc4random_uniform(self.view.frame.size.width - self.button.frame.size.width); int y = ymin + arc4random_uniform(self.view.frame.size.height - self.button.frame.size.height); [self.button setCenter:CGPointMake(x,y)]; [self.button addTarget:self action:@selector(buttonPressed :) forControlEvents:UIControlEventTouchUpInside]; } 这不帮助我。仍然是一样的 – Newbie 2014-12-05 15:03:53
不清楚你如何从界面构建器接线,但你可能不应该在你的“按钮”方法中添加一个目标到按钮。为什么不只是在一种方法中执行两个任务(移动,计数),或者至少直接调用IBAction方法中的移动和计数方法? – 2014-12-05 15:32:47