0

我有一个包含 UITableView 的 viewController。这个viewController实现了UITableViewDelegate和UITableViewDataSource,我也在尝试实现以下方法:

touchesBegin touchesMoved touchesEnded

但是,这些方法没有被调用。我试图调用这些方法以响应用户触摸 UITableView 内的 UIImageView,但这样做只会调用此方法:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    [self.view bringSubviewToFront:_imageView];

    [UIView animateWithDuration:.3 animations:^{


        CGRect rect = [self.view convertRect:[tableView rectForRowAtIndexPath:indexPath] fromView:tableView];

        CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
        _imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);

    }];


    NSLog(@"Table row %d has been tapped", indexPath.row);


}

我希望是继续调用这个方法,但也调用:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch view] == _imageView) {
        //need to make sure user can only move the UIImageView up or down
        CGPoint location = [touch locationInView:_imageView];
        //ensure UIImageView does not move outside UIITableView

        CGPoint pt = [[touches anyObject] locationInView:_imageView];
        CGRect frame = [_imageView frame];
        //Only want to move the UIImageView up or down, not sideways at all

        frame.origin.y += pt.y - _startLocation.y;

        [_imageView setFrame: frame];

        _imageView.center = location;

        return;
    }
}

当用户在 UITableView 内拖动 UIImageView 时。

谁能看到我做错了什么?

提前感谢所有回复的人

4

2 回答 2

2

除了我试图对 UIWebView 实现触摸检测外,我遇到了类似的问题。我最终通过将 UIPanGestureRecognizer 添加到 UIWebView 的.view属性来克服它。

实现这个方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }

然后将 UIPanGestureRecognizer 添加到 UITableView 中,如下所示:

UIPanGestureRecognizer *panGesture; panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)]; panGesture.maximumNumberOfTouches = 1; panGesture.minimumNumberOfTouches = 1; panGesture.delegate = self; [self.tableView addGestureRecognizer:panGesture];

然后实现方法

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer { }

现在,每次 UIPanGestureRecognizer 检测到平移手势时,它都会调用该方法,您可以通过调用[recognizer locationInView:self.tableView.view];返回 CGPoint 的方法来获取 UIPanGestureRecognizer 的位置。您还可以通过调用[recognizer state]which 返回 a来获取 UIPanGestureRecognizer 的状态UIGestureRecognizerState

于 2013-06-19T22:12:32.777 回答
0

您是否在触摸将在内部的视图上设置了 userInteractionEnabled?

于 2013-06-20T01:37:07.963 回答