2014-03-04 55 views
0

我正在構建一個向該單元格添加平移手勢的可滑動單元格。基本上,它具有與郵箱應用程序中的單元格相同的外觀和感覺,您可以在其中有一個頂部視圖,您可以向左或向右滑動以顯示下方的另一個視圖(revealView)。使用RAC滑動單元格

我想與反應的方法來建立這個,所以我做的方法是:

  • 首先,當我設置視圖和平移手勢,我過濾rac_gestureSignal得到手勢的當前狀態並使用綁定更新頂視圖位置(這裏簡化一些實現細節)以及當手勢結束/取消時隱藏/顯示揭示視圖。我還呼籲setNeedsLayout當任panDirection或revealView變化(以相應地更新revealView幀)從它們的值合併的信號,以及去除揭示視圖對細胞複用:

    - (void)setupView 
    { 
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:nil]; 
        panGesture.delegate = self; 
    
        RACSignal *gestureSignal = [panGesture rac_gestureSignal], 
           *beganOrChangeSignal = [gestureSignal filter:^BOOL(UIGestureRecognizer *gesture) { 
         return gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateBegan; 
        }], 
           *endOrCancelSignal = [gestureSignal filter:^BOOL(UIGestureRecognizer *gesture) { 
         return gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled; 
        }]; 
    
        RAC(self, contentSnapshotView.center) = [beganOrChangedSignal map:^id(id value) { 
         return [NSValue valueWithCGPoint:[self centerPointForTranslation:[panGesture translationInView:self]]]; 
        }]; 
    
        [beganOrChangeSignal subscribeNext:^(UIPanGestureRecognizer *panGesture) { 
         [self updateTopViewFrame]; 
    
         [panGesture setTranslation:CGPointZero inView:self]; 
        }]; 
    
        [[endOrCancelSignal filter:^BOOL(UIPanGestureRecognizer *gestureRecognizer) { 
         return [self shouldShowRevealView]; 
        }] subscribeNext:^(id x) { 
         [self showRevealViewAnimated:YES]; 
        }]; 
    
        [[endOrCancelSignal filter:^BOOL(UIPanGestureRecognizer *gestureRecognizer) { 
         return [self shouldHideRevealView]; 
        }] subscribeNext:^(id x) { 
         [self hideRevealViewAnimated:YES]; 
        }]; 
    
        [[RACSignal merge:@[RACObserve(self, panDirection), RACObserve(self, revealView)]] subscribeNext:^(id x) { 
         [self setNeedsLayout]; 
        }]; 
    
        [[self rac_prepareForReuseSignal] subscribeNext:^(id x) { 
         [self.revealView removeFromSuperview]; 
         self.revealView = nil; 
        }]; 
    
        [self addGestureRecognizer:panGesture]; 
    } 
    
  • 然後,我暴露一個信號屬性(revealViewSignal),當揭示視圖顯示/隱藏時它將發送YES/NO值。因此,您可以訂閱此信號,從而在視圖改變其狀態時採取行動。在內部,這個信號將是一個RACSubject發送下一個事件的每個顯示/隱藏動畫結束後:

    - (void)showRevealViewAnimated:(BOOL)animated 
    { 
        [UIView animateWithDuration:animated ? 0.1 : 0.0 
            animations:^{ 
             // SHOW ANIMATIONS 
            } 
            completion:^(BOOL finished) { 
             [(RACSubject *)self.revealViewSignal sendNext:@(YES)]; 
            }]; 
    } 
    
    - (void)hideRevealViewAnimated:(BOOL)animated 
    { 
        [UIView animateWithDuration:animated ? 0.1 : 0.0 
            animations:^{ 
             // HIDE ANIMATIONS 
            } 
            completion:^(BOOL finished) { 
             [(RACSubject *)self.revealViewSignal sendNext:@(NO)]; 
            }]; 
    } 
    

一切正常,但我只是想知道,如果這是建立這種說法正確的做法以RAC方式。此外,還有兩種手勢識別器委託方法,我希望在上面的相同設置方法中進行設置,但我無法弄清楚是否可以使用rac_signalForSelector:fromProtocol:方法,因此我最終實現了它們一如既往:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    return [self checkIfGestureShouldBegin:gestureRecognizer]; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return [self checkIfGestureShouldRecognize:gestureRecognizer]; 
} 

任何幫助將高度appreaciated,謝謝!

回答

1

不幸的是,目前沒有辦法使用RAC來實現返回值的協議方法。

這是一個棘手的問題,因爲信號不需要同步發送值,但顯然你需要在調用委託方法時返回。你可能不要想阻止信號,因爲它很容易死或活鎖。

相關問題