我有一个包含UITableView的viewController。这个viewController实现了UITableViewDelegate和UITableViewDataSource,我也试图实现以下方法:
的touchesBegan 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时。
谁能看到我做错了什么?
提前致谢所有回复
的人答案 0 :(得分:2)
除了我试图对UIWebView实施触摸检测外,我遇到了类似的问题。我最终通过向UIWebView的.view
属性添加UIPanGestureRecognizer来克服它。
实施此方法:
- (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检测到平移手势时,它都会调用该方法,您可以通过调用返回CGPoint的方法[recognizer locationInView:self.tableView.view];
来获取UIPanGestureRecognizer的位置。您还可以通过调用返回[recognizer state]
的{{1}}来获取UIPanGestureRecognizer的状态。
答案 1 :(得分:0)
您是否在视图上设置了userInteractionEnabled,触摸将在哪里?