gpt4 book ai didi

ios - 使用 RAC 滑动单元格

转载 作者:行者123 更新时间:2023-11-29 03:11:37 24 4
gpt4 key购买 nike

我正在构建一个可滑动的单元格,向该单元格添加平移手势。基本上,它与 Mailbox 应用程序中的单元格具有相同的外观和感觉,您有一个顶 View ,您可以向左或向右滑动以显示下面的另一个 View (revealView)。

我想用响应式(Reactive)方法构建它,所以我这样做的方式是:

  • 首先,当我设置 View 和平移手势时,我过滤 rac_gestureSignal 以获取手势的当前状态并使用绑定(bind)更新顶 View 位置(此处简化了一些实现细节)当手势结束/取消时隐藏/显示 revealView。当 panDirection 或 revealView 发生变化时(为了相应地更新 revealView 框架),我也会调用 setNeedsLayout 从它们的值中合并信号,以及删除单元格重用的显示 View :

    - (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),它会在显示 View 显示/隐藏时发送 YES/NO 值。因此,您可以订阅此信号,并因此在 View 更改其状态时采取行动。在内部,这个信号将是一个 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 方式构建这种 View 的正确方法。另外,我想在上面的相同设置方法中设置两个手势识别器委托(delegate)方法,但我无法弄清楚是否可以在这里使用 rac_signalForSelector:fromProtocol: 方法,所以我最终实现了它们一如既往:

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

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

非常感谢任何帮助,谢谢!

最佳答案

不幸的是,目前没有办法使用 RAC 来实现返回值的协议(protocol)方法。

这是一个棘手的问题,因为不需要信号来同步发送值,但显然您需要在调用委托(delegate)方法时返回<​​em>一些东西。您可能不想阻止信号,因为它很容易死锁或活锁。

关于ios - 使用 RAC 滑动单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22185189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com