使用ReactiveCocoa为UIbutton增加长按功能,实现步进累加效果

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
@weakify(btn)
UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc] init];
[btn addGestureRecognizer:longPressGes];
RACSignal *timer = [RACSignal interval:0.1 onScheduler:[RACScheduler mainThreadScheduler]];
RACSignal *longPressGesSignal = [longPressGes rac_gestureSignal];
RACSignal *longPressGesBeganSignal = [longPressGesSignal filter:^BOOL(UILongPressGestureRecognizer *value) {
    return value.state == UIGestureRecognizerStateBegan;
}];
RACSignal *longPressGesEndedSignal = [longPressGesSignal filter:^BOOL(UILongPressGestureRecognizer *value) {
    return value.state == UIGestureRecognizerStateEnded
    || value.state == UIGestureRecognizerStateCancelled
    || value.state == UIGestureRecognizerStateFailed ;
}];
RACSignal *notifyWhenNeed = [longPressGesBeganSignal flattenMap:^RACStream *(id value) {
    return [timer takeUntil:longPressGesEndedSignal];
}];
[notifyWhenNeed subscribeNext:^(id x) {
    @strongify(btn)
    [btn sendActionsForControlEvents:UIControlEventTouchUpInside];
}];

  

posted @ 2019-08-22 23:53  ashamp  阅读(320)  评论(0编辑  收藏  举报