【问题标题】:Detect drag event of UITableViewCell [duplicate]检测 UITableViewCell 的拖动事件 [重复]
【发布时间】:2012-03-18 16:35:26
【问题描述】:

可能重复:
How to get notified of UITableViewCell move start and end

我已经使用自定义设计实现了UITableView。这个UITableView 必须支持编辑模式。进入编辑模式时,UITableViewCell 会被附加控件(EditControl、ReorderControl...)修饰。它们不适合我的定制设计,这就是我想替换它们的图像的原因。为此,我将UITableViewCell 子类化并覆盖layoutSubview,在那里我替换了这些控件的图像。

问题:开始拖放操作时,EditControl 的图像被替换回 UITableViewCell 中某处的原始图像。我可以再次替换它

– tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

当用户将可拖动单元格移动到另一个 indexPath 时,为时已晚。

所以我的问题归结为:如何检测用户实际开始拖动UITableViewCell 的那一刻?

【问题讨论】:

  • 使用 PanGesture 检测图片拖动操作的时刻
  • 感谢您的评论。已经尝试过了,没有成功。我可以将 GestureRecognizer 附加到单元格的内容视图,然后我的处理程序被解雇。但它不会因为 ReorderControl 的触摸而被解雇。覆盖 touchesBegan 也是如此...
  • 发布您的代码可能有助于找到解决方案:)
  • 我已经找到了两个解决方案。第一个:当单元格处于选中状态时,EditControl 的图像不会被 UITableViewCell 替换。所以我一直选择那个状态,并在 touchesBegan 中实现了我自己的选择跟踪......第二个也是首选的解决方案:转储那个丑陋的黑客,它依赖于太多可能会改变的内部结构。如果您仍然感兴趣,我可以发布 hacky 解决方案...

标签: iphone ios uitableview drag-and-drop


【解决方案1】:

虽然 Bala 的评论朝着正确的方向发展,但我最初在正确实施方面遇到了一些问题。现在我发现了它是如何做到的。简而言之:您必须创建UITableViewCell 的自定义子类。覆盖layoutSubviews 以将UILongPressGestureRecognizer 附加到UITableViewCellReorderControl。定义协议并使用委托将拖动状态通知给您想要的任何人。

CustomTableViewCell.h:

#import <UIKit/UIKit.h>

@protocol CustomTableViewCellDelegate;

@interface CustomTableViewCell : UITableViewCell {
}

@property (nonatomic, assign) id <CustomTableViewCellDelegate> delegate;

@end

@protocol CustomTableViewCellDelegate
- (void)CustomTableViewCell:(CustomTableViewCell *)cell isDragging:(BOOL)value;
@end

CustomTableViewCell.m:

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

@synthesize delegate = _delegate;

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [_delegate CustomTableViewCell:self isDragging:YES];    // Dragging started
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [_delegate CustomTableViewCell:self isDragging:NO];     // Dragging ended
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];

    for (UIView *view in self.subviews) {
        if ([NSStringFromClass ([view class]) rangeOfString:@"ReorderControl"].location != NSNotFound) {    // UITableViewCellReorderControl
            if (view.gestureRecognizers.count == 0) {
                UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
                gesture.cancelsTouchesInView    = NO;
                gesture.minimumPressDuration    = 0.150;
                [view addGestureRecognizer:gesture];
            }
        }
    }
}

@end

请注意,虽然此代码不使用任何私有 API,但如果 Apple 更改其内部实现(即通过更改 UITableViewCellReorderControl 的类名),它仍可能停止工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多