Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 按下按钮时如何执行动作?_Ios_Objective C_Uibutton - Fatal编程技术网

Ios 按下按钮时如何执行动作?

Ios 按下按钮时如何执行动作?,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我有一个按钮,我需要在按下按钮时将标签更改为@“按下”,如果它松开,我需要将其更改为@“放开”。 我已经找到了很多答案,但我不知道如何在Xcode 5中实现它们。 这些就是我尝试过的: 你可以在触地内侧开始动作,在触地内侧停止动作——你可以在IB中连接它们 或 向按钮添加两种控制状态的目标,UIControlEventTouchDown和UIControlEventTouchUpInside或uicontrolEventTouchOutside 甚至 Add a dispatch source

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

你可以在触地内侧开始动作,在触地内侧停止动作——你可以在IB中连接它们

向按钮添加两种控制状态的目标,UIControlEventTouchDown和UIControlEventTouchUpInside或uicontrolEventTouchOutside

甚至

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新手,根本不知道该怎么做!非常感谢您提前提供的帮助

我会做一个手势。下面是一些代码:

- (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。

第一个方法满足您的要求,下面是一个示例实现:

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

如果您是xCode新手,这可能很难理解。照这个做,你会没事的

  • 转到情节提要并在视图中放置一个按钮现在转到Inspector并将状态配置从默认更改为高亮显示

  • 在占位符“突出显示的标题”中放置“按下”

  • 现在单击
    助理编辑器
    打开故事板旁边的.m文件

  • CNTRL+单击并将
    从按钮拖动到.m文件代码,以创建一个TouchUpInside事件,我的事件名为
    按钮按下

  • 把这段代码放到方法体中,你就可以开始了

按下并释放后,其外观如下所示: