IOS检测用户何时停止触摸UIImageView

时间:2014-05-31 06:32:42

标签: ios objective-c cocoa-touch uiimageview ontouchevent

知道怎么做吗?我的UIImageView中的每个单元格内都有UITableView,我想在用户开始触摸UIImageView时禁用滚动,然后在用户启用后启用它停止在照片上拖动手指。

3 个答案:

答案 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)

有两种方法可以实现此目的,包括UITapGestureRecognizerUIPanGestureRecognizer到您的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属性来停止并开始滚动UITableViewUIScrollView的子类

答案 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");
    }
}