Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 如果按钮未保持足够长的时间,则取消按Objective-C_Ios_Objective C_Uibutton - Fatal编程技术网

Ios 如果按钮未保持足够长的时间,则取消按Objective-C

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

我在Objective-C中有一个UIButton,如果该按钮在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秒的按钮时设置条件


如果有帮助,请告诉我。

您可以使用以下方法:

[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秒,则执行操作,否则不执行)

  • 例子:
    先决条件:
  • 声明以下对象(在类的.h文件中)
    • 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;
    }