ios如何判断用户停止点击按钮
伊谢尔伦
伊谢尔伦 2017-04-18 09:29:08
[iOS讨论组]
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(4)
PHP中文网

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

黄舟

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...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号