ios如何判断用户停止点击按钮

用户连续点击同一个按钮,如何判断用户间断了3秒以上没有点击?

阅读 3.9k
4 个回答

你可以在点击按钮的时候开启一个定时器,判断时间间隔

debounce(防抖)

# coffescript
debounce: (fn, context, wait)->
    tid = null
    # if content is a number, assign it to wait
    if not isNaN context
      wait = context if context > 0
      context = undefined
    wait = 200 if isNaN(wait) or wait < 0

    ->
      args = arguments
      clearTimeout tid
      tid = setTimeout ->
        fn?.apply context, args
        return
      , wait
      return

debounce(防抖)和throttle(节流)

以上为前端语法,参考原理

// 点击发送按钮

  • (void)giftSendViewPressedSendBtn:(DNGiftSendView *)giftSendView {

    // 礼物单元格数据模型
    NSIndexPath *currentIndexPath = giftSendView.viewModel.currentIndexPath;
    NSArray<DNGiftSendViewCellViewModel > cellmodels = [giftSendView.viewModel.cellViewModels objectAtIndex:currentIndexPath.section];
    DNGiftSendViewCellViewModel *cellmodel = [cellmodels objectAtIndex:currentIndexPath.row];

    // 提示
    if (!currentIndexPath) {

    [DNToastView showToastWith:@"请选择礼物"];
    return;

    }

    // 发送礼物时间间隔定时器 init
    [self giftSendViewScheduledTimerInitWithDuration:kGiftSendViewScheduledTimerInitWithDuration andGift:cellmodel.gift];

    // 发送礼物
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(giftSendViewSendGifts) object:nil];
    [self performSelector:@selector(giftSendViewSendGifts) withObject:nil afterDelay:kGiftSendViewSendGiftDelayDuration];

}

// 发送礼物时间间隔定时器 init

  • (void)giftSendViewScheduledTimerInitWithDuration:(NSTimeInterval)interval andGift:(DNGift *)gift {

    if (!_giftSendViewScheduledTimer) {

    __weak typeof (self) weakSelf = self;
    
    _giftSendViewScheduledTimer = [NSTimer scheduledTimerWithTimeInterval:interval firing:^{
    
        if (weakSelf.giftSendViewScheduledTimer.isValid) {
    
            // 发送本地消息
            if([weakSelf giftSendViewSendLocalMsg:gift]) {

                [weakSelf.waittingSendGifts addObject:gift];// 增加等待发送礼物
            }

            [weakSelf giftSendViewScheduledTimerDestroy];// 发送礼物时间间隔定时器 destroy
        }
    }];
}

}

// 发送多个礼物

  • (void)giftSendViewSendGifts {

    // 礼物数为0
    if (self.waittingSendGifts.count == 0) {

    return;

    }

    // 发送多个礼物
    __weak typeof(self) weakSelf = self;
    [_artistLiveDataController requestGiftsSendWithActivityId:self.activityId andGifts:self.waittingSendGifts andCallback:^(NSError error, NSArray<DNGift > *sendFailGifts) {

    if (error) {
    
        // 余额不足提示
        if (error.code == DNDataControlErrorUserRemainingMoneyNotEnough) {
    
            [weakSelf giftSendFailedViewAnimationShow];// 显示余额不足提示
            [self requestUserRemainingMoney];// 请求用户余额
        }
    
        // 其他错误处理
        if (error.code == DNDataControlErrorGiftSendFailed) {
    
        }

        // 把扣掉的余额加回来
        for (int i=0; i<sendFailGifts.count; i++) {

            DNGift *failureGift = [sendFailGifts objectAtIndex:i];
            weakSelf.giftSendView.remainMoney += failureGift.giftPrice.floatValue;// 减少余额
            NSLog(@"--------发送礼物[%@]失败!!!--------",failureGift.giftName);
        }
    } else {

        NSLog(@"--------发送礼物成功!!!--------");
    }

}];

// 清空等待发送的礼物
weakSelf.waittingSendGifts = [NSMutableArray arrayWithCapacity:0];

}

利用Objective-C的methodSwizzing,替换sendAction:to:forEvent:方法,在替换的方法里面定义一个变量用来存储点击的时间。
可以参考:
http://www.jianshu.com/p/e4f1...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏