我有一个按钮,我需要在按下按钮时更改标签@"PRESSED"
如果它被释放,我需要将其更改为@"LET GO"
。
我找到了很多答案,但我不知道如何在Xcode 5中实现它们。
那就是我尝试的那些:
您可以在touchDownInside上开始操作并在touchUpInside操作上停止操作 - 您可以在IB中将它们连接起来。
或
为两个控件状态添加目标,UIControlEventTouchDown和UIControlEventTouchUpInside或UIControlEventTouchUpOutside
甚至
Add a dispatch source iVar to your controller...
dispatch_source_t _timer;
Then, in your touchDown action, create the timer that fires every so many seconds. You will do your repeating work in there.
If all your work happens in UI, then set queue to be
dispatch_queue_t queue = dispatch_get_main_queue();
and then the timer will run on the main thread.
- (IBAction)touchDown:(id)sender {
if (!_timer) {
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
// This is the number of seconds between each firing of the timer
float timeoutInSeconds = 0.25;
dispatch_source_set_timer(
_timer,
dispatch_time(DISPATCH_TIME_NOW, timeoutInSeconds * NSEC_PER_SEC),
timeoutInSeconds * NSEC_PER_SEC,
0.10 * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
// ***** LOOK HERE *****
// This block will execute every time the timer fires.
// Do any non-UI related work here so as not to block the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Do UI work on main thread
NSLog(@"Look, Mom, I'm doing some work");
});
});
}
dispatch_resume(_timer);
}
Now, make sure to register for both touch-up-inside and touch-up-outside
- (IBAction)touchUp:(id)sender {
if (_timer) {
dispatch_suspend(_timer);
}
}
Make sure you destroy the timer
- (void)dealloc {
if (_timer) {
dispatch_source_cancel(_timer);
dispatch_release(_timer);
_timer = NULL;
}
}
我是iOS的新手,不知道如何做到这一点!非常感谢您提前提供任何帮助!
答案 0 :(得分:1)
我会用GestureRecognzier来做这件事。这是一些代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonIsPressed:)];
gr.minimumPressDuration = 0.1;
[self.button addGestureRecognizer:gr];
}
-(IBAction)buttonIsPressed:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
self.label.text = @"Button is pressed!";
}
else if (gesture.state == UIGestureRecognizerStateEnded) {
self.label.text = @"Button is not pressed";
}
}
您需要一个IBOutlet作为标签和按钮。
答案 1 :(得分:0)
第一种方法可以满足您的需求,这是一个示例实现:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2.0 - 50.0, self.view.bounds.size.height / 2.0 - 25.0, 100.0, 50.0)];
myButton.backgroundColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.8 alpha:1.0];
[myButton setTitle:@"PRESS ME" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(setPressed:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(setLetGo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
-(void)setPressed:(id)sender
{
UIButton *myButton = (UIButton *)sender;
[myButton setTitle:@"PRESSED" forState:UIControlStateNormal];
}
-(void)setLetGo:(id)sender
{
UIButton *myButton = (UIButton *)sender;
[myButton setTitle:@"LET GO" forState:UIControlStateNormal];
}
答案 2 :(得分:0)
如果您是xCode的新手,这可能很难理解。按照这个,你就没事了。
Assistant Editor
打开Storyboard
CNTRL + Click and drag
从Button到.m文件代码创建一个TouchUpInside事件,我的名为buttonPressed
这就是它的外观,压力和释放: