UIButton Touch and Hold

前端 未结 5 1203
暗喜
暗喜 2020-11-28 07:45

I haven\'t found a very easy way to do this. The ways I\'ve seen require all these timers and stuff. Is there any easy way I can hold a UIButton and cause it to repeat the

5条回答
  •  孤街浪徒
    2020-11-28 08:13

    I know this is an old question, but as an easy way, like to consider using "[NSObject performSelector:withObject:afterDelay:]" to repeatedly invoke methods in any particular time interval.

    In this case:

    NSTimeInterval someTimeInterval = 1;
    
    - (IBAction)action:(id)sender {
        UIButton * const button = sender;
        if (button.state != UIControlStateHighlighted) {
            return;
        }
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender];
        [self performSelector:_cmd withObject:sender afterDelay:someTimeInterval];
    }
    

提交回复
热议问题