0

我正在尝试实现一个 uitableview,它的行可以左右拖动(并在它们后面显示一些东西)。代码工作正常,我使用以下方法实现了它:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

我的问题是这些行还包含 UIButtons,单击它们时应该单击它们,但在拖动时应该拖动整个单元格。我找到了这个解决方案。基本上是在点击 UIButtons 时冒泡事件:

[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event]; 

但是,该事件似乎touchesMoved只冒泡一次。
我在这个领域看到了各种各样的问题。例子。但我没有看到任何解决方案或回应。

任何帮助、建议或创造性的解决方法将不胜感激!

4

2 回答 2

0

与其实现 touchesBegan 等,为什么不使用 UIPanGestureRecognizer?我只用一个简单的矩形视图对此进行了测试,该视图主要由 UIButton 覆盖。视图被拖动,无论我在哪里触摸,如果我点击按钮,按钮方法就会被触发。

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self.theView addGestureRecognizer:panGesture]; //theView is IBOutlet for small view containing a button
}

-(void)viewDidAppear:(BOOL)animated {
    self.currentViewFrame = self.theView.frame;
}

- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
    CGPoint translate = [sender translationInView:self.view];

    CGRect newFrame = self.currentViewFrame;
    newFrame.origin.x += translate.x;
    newFrame.origin.y += translate.y;
    sender.view.frame = newFrame;

    if (sender.state == UIGestureRecognizerStateEnded)
        self.currentViewFrame = newFrame;
}

-(IBAction)doClick:(id)sender {
    NSLog(@"click");
}
于 2012-09-09T23:00:30.920 回答
0

只需检查一下使用标签系统触摸了哪一个。

于 2012-09-09T22:04:10.607 回答