ios - 如何在按下按钮时执行操作?

标签 ios objective-c uibutton

我有一个按钮,我需要在按下按钮时将标签更改为 @"PRESSED",如果松开,我需要将其更改为 @"LET GO"。 我找到了很多答案,但我不知道如何在 Xcode 5 中实现它们。 这些是我试过的:

You can start your action on the touchDownInside and stop it on the touchUpInside actions - you can hook them up in IB.

Add targets to your button for both control states, UIControlEventTouchDown and UIControlEventTouchUpInside or 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 新手,完全不知道该怎么做!非常感谢您提前提供的帮助!

最佳答案

我会用 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。

关于ios - 如何在按下按钮时执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24258396/

相关文章:

ios - 内部触摸时用颜色填充 UIButton

iphone - 如何在 uibutton 中分配两次标题

objective-c - 单击时 UIButton 获得了他的默认文本

ios - 如果文本是单个单词,UITextField adjustmentFontSizeToFitWidth 不起作用

ios - 如何检测更改了哪个 EKevent

ios - 如何以某种通用方式删除有序的对多 CoreData 关系中的对象?

iphone - 如何取消之前在 Objective-C 中安排的事件?

ios - 删除应用程序不会删除应用程序组数据

ios - 使用 xib 创建可重用的 UIView(并从 Storyboard 中加载)

ios - 如何在 iOS 6+ 上解码 h264 字节流?