gpt4 book ai didi

ios - 无法在 iOS 的 viewController 中调用 touchesBegan/touchesMoved 方法

转载 作者:行者123 更新时间:2023-11-28 22:31:48 30 4
gpt4 key购买 nike

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

触摸开始触摸移动触摸结束

但是,这些方法没有被调用。我试图调用这些方法以响应用户触摸 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 时。

谁能看出我做错了什么?

提前感谢所有回复的人

最佳答案

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

实现这个方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
返回是;
}

然后像这样将 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 检测到平移手势时,它都会调用该方法,您可以通过调用方法获取 UIPanGestureRecognizer 的位置 [recognizer locationInView:self.tableView.view];这将返回一个 CGPoint。您还可以通过调用返回 UIGestureRecognizerState[recognizer state] 获取 UIPanGestureRecognizer 的状态。

关于ios - 无法在 iOS 的 viewController 中调用 touchesBegan/touchesMoved 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17201478/

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