触摸开始不能与scrollView一起工作



我有这样一个命令,它捕获用户点击图像的时间:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if(CGRectContainsPoint(card1.frame,touchLocation))
    {
        cardSelected = @"pagseguro";
        [card1.layer setBorderColor: [[UIColor redColor] CGColor]];
        [card1.layer setBorderWidth: 1.0];
        [card2.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [card2.layer setBorderWidth: 1.0];
    }
    if(CGRectContainsPoint(card2.frame,touchLocation))
    {
        cardSelected = @"sumup";
        [card1.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [card1.layer setBorderWidth: 1.0];
        [card2.layer setBorderColor: [[UIColor redColor] CGColor]];
        [card2.layer setBorderWidth: 1.0];
    }
}

在我输入scrollView之前,这段代码工作得很好,我正在拉动屏幕的滚动,如下所示:

[self.view addSubview:scroll];
    scroll.contentSize = scroll.frame.size;
    scroll.frame = self.view.frame;

现在触摸开始方法不识别这个更多的触摸在图像中,我该如何修复它?

尝试imageView.userInteractionEnabled = YES,默认imageView上没有。

如果你在你的控制器中实现touchesBegan:withEvent:,它将不起作用,因为它将调用UIView的子类,所以你需要子类ScrollView,需要覆盖这个方法。

如果userInteractionEnabled不能工作,另一个选择是使用UITapGestureRecognizer,如果它适合你的需要。

最新更新