1

我想知道如何在 xCode 中对UIImageView内部 a使用触摸方法。UIScrollView当我将UIImageViewsubview 添加到 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];
}
4

5 回答 5

5

试一试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];
}
于 2013-08-07T12:11:52.610 回答
2

如果您使用相同的手势识别器处理多个图像视图,它将不起作用。尝试为每个图像视图使用新的手势识别器。

于 2014-02-20T06:49:39.983 回答
1

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

于 2013-08-07T12:15:27.050 回答
0

您必须从 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 的实例中添加任何视图时,您将正确地在添加的视图中获得触摸。

于 2013-08-07T11:37:04.367 回答
0

应用转换时,图像视图与滚动视图中的子视图存在相同的问题。滚动后,我的手势识别器不再工作,所以我检查了滚动视图的偏移量并重置了我的图像,以便识别手势。当然我们需要将 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
    }
}
于 2016-07-01T20:23:46.817 回答