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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 @   ashamp  阅读(323)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示