【问题标题】:Touches began not work with scrollView触摸开始不适用于滚动视图
【发布时间】:2014-09-13 22:07:54
【问题描述】:

当用户点击图片时,我有这个命令:

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

现在 touch Began 方法无法识别图像中的更多触摸,我该如何解决?

【问题讨论】:

  • 你有没有从UIScollView继承你自己的类?如果没有,你怎么能指望那个班级会把–touchesBegan:withEvent: 传给你呢? UIScollView 基本上从其子视图中“窃取”触摸,除非您直接覆盖它...
  • 如何直接覆盖它?
  • 例如在您自己的继承自 UISCrollView 的类中。

标签: ios objective-c uiscrollview touchesbegan


【解决方案1】:

试试imageView.userInteractionEnabled = YES 默认情况下imageView没有。

如果您在控制器中实现touchesBegan:withEvent:,它将无法工作,因为它将调用UIView 的子类,因此您需要继承ScrollView 并需要覆盖此方法。

如果userInteractionEnabled 不起作用,另一种选择是使用UITapGestureRecognizer 如果它符合您的需要。

【讨论】:

    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多