知道怎么做吗?我的UIImageView
中的每个单元格内都有UITableView
,我想在用户开始触摸UIImageView
时禁用滚动,然后在用户启用后启用它停止在照片上拖动手指。
答案 0 :(得分:2)
我只是给出了我的逻辑。
将UIPanGestureRecognizer
添加到UITableView.
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
[cell addGestureRecognizer:panGestureRecognizer];
方法名称为handlePanFrom:
- (void)handlePanFrom:(UIPanGestureRecognizer*)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];
if (recognizer.state == UIGestureRecognizerStateBegan)
{
/// track began
tableView.userInteractionEnabled = NO;
}
else if (recognizer.state == UIGestureRecognizerStateChanged)
{
// track the movement
} else if (recognizer.state == UIGestureRecognizerStateEnded)
{
// final position
tableView.userInteractionEnabled = YES;
}
}
确保您的UIIMageView
必须设置为userInteractionEnabled = YES;
。因为默认情况下UIIMageView
必须设置userInteractionEnabled = NO;.
答案 1 :(得分:1)
有两种方法可以实现此目的,包括UITapGestureRecognizer
或UIPanGestureRecognizer
到您的UIImageView
,并设置目标是您的自定义UITableViewCell类或包含
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
UIImageView
自定义类中的方法,并通过协议将委托设置为自定义UITableViewCell
。
UIGestureRecognizer
,您可以检查其状态属性UIGestureRecognizer Class Reference,以便在开关的帮助下了解状态
typedef enum {
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
}
最后,通过切换UITableView的scrollEnabled属性来停止并开始滚动UITableView
是UIScrollView
的子类
答案 2 :(得分:1)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass:[UIImageView class]]) {
NSLog(@"self.tableView.scrollEnabled = NO");
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass:[UIImageView class]]) {
NSLog(@"self.tableView.scrollEnabled = YES");
}
}