【问题标题】:Touch method UIImageView inside UIScrollViewUIScrollView里面的触摸方法UIImageView
【发布时间】:2013-08-08 17:45:40
【问题描述】:

我想知道如何在 xCode 中的 UIScrollView 内使用触摸方法来处理 UIImageView。 当我将UIImageView 子视图添加到 self.view 时,我可以使用 touch 方法。但是当我将 UIImageView 子视图添加到 UIScrollView 时,我不能。我该如何解决这个问题?

这是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    touches = [event allTouches];
    for (UITouch *touch in touches) {

        NSLog(@"Image Touched");
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.9)];
    scrollView.scrollEnabled = TRUE;
    scrollView.bounces = TRUE;
    scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    scrollView.userInteractionEnabled = YES;
    [self.view addSubview:scrollView];

     UIImageView *ImageView = [UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 0.04, 10, [UIScreen mainScreen].bounds.size.width * 0.28, [UIScreen mainScreen].bounds.size.height * 0.22)];
     ImageView.image = [UIImage imageNamed(@"image.png")];
     ImageView.layer.borderColor = [UIColor whiteColor].CGColor;
     ImageView.layer.borderWidth = 1;
     ImageView.userInteractionEnabled = YES;
     [scrollView addSubview:ImageView];
}

【问题讨论】:

  • 你应该改用 UITapGestureRecognizer ..
  • 当我使用它时,整个 UIScrollView 区域都是可触摸的。我只希望 UIScrollView 内的 UIImageView 是可触摸的。
  • 在这种情况下,您应该将手势添加到 UIImageView 而不是 UIscrollView
  • 但是 UIScrollView 不允许 UIImageView 的触摸能力。

标签: objective-c cocoa-touch uiscrollview uiimageview


【解决方案1】:

您必须从 UIScrollView 派生一个 CustomScrollView 并覆盖所有触摸方法,如 touchesBegan、Moved、Ended 和 Canceled,如下所示:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UIView *view in self.subviews)
    {
        UITouch *touch = [touches anyObject];
        CGPoint pt = [touch locationInView:view];

        if (CGRectContainsPoint(view.frame, pt))
        {
            [view touchesMoved:touches withEvent:event];
        }

    }
}

在你这样做之后。当您在 customScrollView 的实例中添加任何视图时,您将正确地在添加的视图中获得触摸。

【讨论】:

  • UIImageView的用户交互默认是关闭的,你可以通过设置imageView.userInteractionEnabled = YES;或者简单的你可以从 UIImageView 派生并覆盖它的触摸。
【解决方案2】:

试试UIGestureRecognizers。通过多层触摸管理,它们更容易管理。

- (void)touchedImage:(UITapGestureRecognizer *)gesture {
    // When the gesture has ended, perform your action.
    if (gesture.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Touched Image");
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.9)];
    scrollView.scrollEnabled = TRUE;
    scrollView.bounces = TRUE;
    scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    scrollView.userInteractionEnabled = YES;
    [self.view addSubview:scrollView];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 0.04, 10, [UIScreen mainScreen].bounds.size.width * 0.28, [UIScreen mainScreen].bounds.size.height * 0.22)];
    imageView.image = [UIImage imageNamed:@"image.png"];
    imageView.layer.borderColor = [UIColor whiteColor].CGColor;
    imageView.layer.borderWidth = 1;
    imageView.userInteractionEnabled = YES;
    [scrollView addSubview:imageView];

    // Create a tap gesture
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchedImage:)];
    [imageView addGestureRecognizer:tap];
}

【讨论】:

  • 很高兴听到它,快乐的编码:)
  • Ryan Poolos 您的触摸方法运行良好,但请告诉我,如果我们使用多个图像阵列,我们如何从滚动视图中获取当前图像
  • 打开一个新问题并将其链接到此处。我很乐意看看。
【解决方案3】:

UIImageView的用户交互默认是禁用的,你可以通过设置imageView.userInteractionEnabled = YES;来启用它

【讨论】:

    【解决方案4】:

    如果您使用同一个手势识别器处理多个图像视图,它将无法正常工作。尝试为每个图像视图使用新的手势识别器。

    【讨论】:

    • 在这个问题上浪费了一整天。只有最后添加的视图在响应触摸事件。
    • 这里也一样。我将UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchedImage:)]; 部分排除在循环之外,但它不起作用。对于多个图像视图,它必须进入 for 循环。
    【解决方案5】:

    在应用转换时,图像视图与滚动视图中的子视图存在相同的问题。 滚动后,我的手势识别器不再工作,所以我检查了滚动视图的偏移量并重置了我的图像,以便识别手势。 当然我们需要将 userInterActionEnabled 设置为 true 并在 viewDidLoad() 中添加手势

     override func viewDidLoad() {
         imageView.userInteractionEnabled = true 
         addGesture() 
    
     }
    
     func addGesture(){
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.enableGesture(_:)));
        singleTap.numberOfTapsRequired = 1
        self.imageView.addGestureRecognizer(singleTap)
        self.imageView.tag = 1
    
    }
    
    override func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let offset = scrollView.contentOffset.y
        if offset > 0 {
            imageView.layer.transform = avatarTransform
        }
        else {
            imageView.transform = CGAffineTransformIdentity
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多