【问题标题】:IOS detect when user stops touching UIImageViewIOS 检测用户何时停止触摸 UIImageView
【发布时间】:2014-07-20 22:55:44
【问题描述】:

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

【问题讨论】:

  • 为了做到这一点,您可能需要在图像视图上添加一个 UITapGestureRecognizer。要禁用滚动,您可以尝试 tableView.userInteractionEnabled = NO;
  • 但是当用户停止触摸时,手势识别器会检测到什么?我可以让 tableview 在用户触摸时禁用滚动,我只是无法在他们停止触摸时启用它
  • 您可以对其进行布尔运算,但我强烈建议您按照@The King 下面显示的操作

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


【解决方案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

UIGestureRecognizerUIGestureRecognizer Class Reference的状态属性可以通过switch来了解状态

typedef enum {
   UIGestureRecognizerStatePossible,

   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,

   UIGestureRecognizerStateFailed,

   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} 

最后,通过切换 UITableView 的 scrollEnabled 属性来停止和开始滚动,其中 UITableViewUIScrollView 的子类

【讨论】:

  • @The King,我可以知道原因吗?
  • 不太清楚如何使用这个解决方案,请原谅我的无知。
  • @ChuckKelly,或多或少我和国王在说同样的话,但我给出了一个抽象的答案,他给出了具体的答案。
【解决方案2】:
- (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");
    }
}

【讨论】:

  • 这些应该放在我的 imageviewclass 中吗?
  • 没有。你的viewController 班级
  • 但是当你松开手指时 touchesEnded 会被提升。
【解决方案3】:

我只是给出我的逻辑。

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;.

【讨论】:

  • 嗯,我认为你比我更接近,但这会产生一系列新问题。它可能会帮助您查看正在使用的上下文。请参阅我添加到我的帖子中的图像。使用您的解决方案,用户无法再流畅地擦除照片
  • 是的,因为如果你设置 tableView.userInteractionEnabled = YES;那么它的所有子视图将停止检测用户交互。所以使用 tableView.scrollEnabled = NO;
  • 即使启用了滚动功能,它仍然完全破坏了在一个大的触摸/拖动事件中流畅地将照片从屏幕上滑出的能力,就像照片店里的橡皮擦一样。 ScrollDisabled 是关键,但我认为 bc 如果我将禁用滚动功能添加到启用擦除的委托方法中,它完全按照我想要的方式工作,唯一的问题是一旦用户完成擦除,我就无法重新启用它图像
猜你喜欢
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
相关资源
最近更新 更多