Ios 如果按钮未保持足够长的时间,则取消按Objective-C
我在Objective-C中有一个UIButton,如果该按钮在10秒前松开,我想取消按下。我该怎么办Ios 如果按钮未保持足够长的时间,则取消按Objective-C,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我在Objective-C中有一个UIButton,如果该按钮在10秒前松开,我想取消按下。我该怎么办 提前感谢您可以使用以下内容: [yourButton addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handle:)]]; 创建名为timer的NSTimer属性: @prop
提前感谢您可以使用以下内容:
[yourButton addGestureRecognizer:[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handle:)]];
创建名为timer的NSTimer属性:
@property (nonatomic, strong) NSTimer *timer;
和一个柜台:
@property (nonatomic, strong) int counter;
- (void)incrementCounter {
self.counter++;
}
因此,当手势开始时,启动一个计时器,该计时器每秒触发递增方法,直到手势结束。在这种情况下,您需要将minimumPressDuration设置为0,否则手势不会立即开始。
通过这种方式,您可以获取计时器,并在按下self.counter=10秒的按钮时设置条件
如果有帮助,请告诉我。您可以使用以下方法:
[yourButton addGestureRecognizer:[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handle:)]];
创建名为timer的NSTimer属性:
@property (nonatomic, strong) NSTimer *timer;
和一个柜台:
@property (nonatomic, strong) int counter;
- (void)incrementCounter {
self.counter++;
}
因此,当手势开始时,启动一个计时器,该计时器每秒触发递增方法,直到手势结束。在这种情况下,您需要将minimumPressDuration设置为0,否则手势不会立即开始。
通过这种方式,您可以获取计时器,并在按下self.counter=10秒的按钮时设置条件
请让我知道它是否有用。为了坚持
UIButton
的预期设计,我建议使用UIButton
的uicontroleventTouchInside
和UIControlEventTouchDown
控制事件
(提示:-addTarget:action:forControlEvents:
方法)
逻辑上:
UIControlEventTouchDown上启动计时器
UIControlEventTouchUpInside上停止计时器
UIControlEventTouchUpInside
操作方法中
- 检查计时器持续时间(如果为10秒,则执行操作,否则不执行)
例子:
先决条件:
ui按钮*btnTest代码>
NSTimer*tmrTest代码>
int i_tmrCount代码>
代码:
PS:如果是
ui标签
,则使用ui长按GestureRecognitizer
,但对于ui按钮
,这并不理想,因为您必须放弃连接到按钮UIControlEventTouchUpInside
的方法的实现,而是将连接到ui长按手势识别器的方法作为主按钮方法(感觉有点脏)以坚持ui按钮的预期设计,我建议使用ui按钮的UIControlEventTouchUpInside
和UIControlEventTouchDown
控制事件
(提示:-addTarget:action:forControlEvents:
方法)
逻辑上:
在UIControlEventTouchDown上启动计时器
在UIControlEventTouchUpInside上停止计时器
在UIControlEventTouchUpInside
操作方法中
- 检查计时器持续时间(如果为10秒,则执行操作,否则不执行)
例子:
先决条件:
声明以下对象(在类的.h文件中)
ui按钮*btnTest代码>
NSTimer*tmrTest代码>
int i_tmrCount代码>
代码:
PS:如果是ui标签
,则使用ui长按GestureRecognitizer
,但对于ui按钮
,这并不理想,因为您必须放弃连接到按钮的UIControlEventTouchUpInside
的方法的实现,而将连接到UILongPressGestureRecognitizer
的方法作为主按钮方法(感觉有点脏)然后你需要UILongPressGestureRecognitor…&&这将有助于检查我的答案不要浪费你的时间然后你需要UILongPressGestureRecognitor…&&这将有助于检查我的答案不要浪费你的时间1。
和2。
是一样的。。。打字错误也许你的意思是“着陆”?@NicolasMiari:是的,打字错误。谢谢你指出。1。
和2。
是一样的。。。打字错误也许你的意思是“着陆”?@NicolasMiari:是的,打字错误。谢谢你指出这一点。
- (void)btnTestAct:(UIButton *)sender
{
//stop the timer
[tmrTest invalidate];
if (i_tmrCount >= 10) {
//do something
NSLog(@"action");
} else {
//do nothing
NSLog(@"reset");
}
//reset i_tmrCount to initial state (to handle next occurrence)
i_tmrCount = 0;
}