UIScrollView中的UIControl无法接收触摸事件

8

我在我的项目中使用了SevenSwitch。我需要将其添加到UIScrollView中,但当我将其添加到滚动视图中时,该控件似乎无法接收触摸事件。

我尝试子类化scrollview并重写以下代码:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
  return NO;
}

新增内容:

self.scrollView.delaysContentTouches = NO;

但仍然无法接收到触摸事件。我该如何停止scrollview阻止UIControl接收触摸事件?

更新

我在我的滚动视图上有一个轻拍手势,因为我希望当用户轻触滚动视图时,调用[self.scrollView endEditing:YES]来关闭键盘。当我删除它时,七个开关可以通过轻拍工作。

我在我的轻拍手势中添加了以下代码:

tap.cancelsTouchesInView = NO;

现在sevenswitch在点击时可以工作,但是在使用触摸追踪时打开或关闭开关存在问题。


有人有任何想法吗? - Husein Behboudi Rad
为什么你想在其中添加触摸事件?我已经放置了一个SevenSwitch在scrollview中,并制作了一个样例演示,而且它可以正常工作(获得控件的操作),而不需要进行任何更改。 - HardikDG
我不想要触摸事件,只想让它工作。当我将其添加到滚动视图中时,无法切换它。 - Husein Behboudi Rad
2个回答

3

我做了一个示例,在其中我在ScrollView内放置了七个开关,并且它们可以正常工作。

- (void)viewDidLoad {
    [super viewDidLoad];
    SevenSwitch *mySwitch = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5);
    [mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    //[self.view addSubview:mySwitch];

    mySwitch.on = true;

    [_cntView addSubview:mySwitch];

    SevenSwitch *mySwitch3 = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70);
    [mySwitch3 addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mySwitch3];

    //self.view.backgroundColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
    mySwitch3.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
    mySwitch3.activeColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.inactiveColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.onTintColor = [UIColor colorWithRed:0.45f green:0.58f blue:0.67f alpha:1.00f];
    mySwitch3.borderColor = [UIColor clearColor];
    mySwitch3.shadowColor = [UIColor blackColor];

    [_cntView addSubview:mySwitch3];
}

- (void)switchChanged:(SevenSwitch *)sender {
    NSLog(@"Changed value to: %@", sender.on ? @"ON" : @"OFF");
}

在这里,_cntView是我放在滚动视图中的主要容器视图,请检查是否适用于您。


更新

正如我在评论中提到的那样,我不明白您想通过触摸跟踪表达什么,但我已经在滚动视图中使用了轻拍手势的示例,希望能有所帮助。

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    [scrollview setContentSize:CGSizeMake(self.view.frame.size.width,700)];
    [self.view addSubview:scrollview];

    SevenSwitch *mySwitch = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5);
    [mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    //[self.view addSubview:mySwitch];

    mySwitch.on = true;

    [scrollview addSubview:mySwitch];

    SevenSwitch *mySwitch3 = [[SevenSwitch alloc] initWithFrame:CGRectZero];
    mySwitch3.center = CGPointMake(self.view.bounds.size.width * 0.5, self.view.bounds.size.height * 0.5 + 70);
    [mySwitch3 addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    mySwitch3.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
    mySwitch3.activeColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.inactiveColor = [UIColor colorWithRed:0.07f green:0.09f blue:0.11f alpha:1.00f];
    mySwitch3.onTintColor = [UIColor colorWithRed:0.45f green:0.58f blue:0.67f alpha:1.00f];
    mySwitch3.borderColor = [UIColor clearColor];
    mySwitch3.shadowColor = [UIColor blackColor];

    [scrollview addSubview:mySwitch3];


    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSingleTap:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.cancelsTouchesInView = NO;
    [scrollview addGestureRecognizer:singleTap];
}


- (void)actionSingleTap:(UITapGestureRecognizer *)sender {
    NSLog(@"Tap");
}

- (void)switchChanged:(SevenSwitch *)sender {
    NSLog(@"Changed value to: %@", sender.on ? @"ON" : @"OFF");
}

我已经通过编程方式完成了所有新代码,并且它能够检测到sevenSwitch外部的触摸事件,并且也可以检测到在seven switch上的触摸/点击事件。
如果您想在Storyboard中使其可滚动并使用Storyboard outlet更改编程式的scrollview。


我还在storyboard中创建了一个新的视图控制器,以便能够像你所做的那样进行操作。现在七段开关可以通过点击来工作,但是在轨道触摸方面存在问题。 此外,我正在编程创建滚动视图并将其添加到视图中。但是在这个滚动视图中,即使通过点击也无法工作。 - Husein Behboudi Rad
@HuseinBehboodiRad,这样在你的滚动视图程序中能正常工作了吗?此外,“touch tacking”意味着开关上的pan/drag手势不起作用吗? - HardikDG
触摸跟踪在使用Storyboard和我编程添加的滚动视图时无法正常工作。 - Husein Behboudi Rad
抱歉,我没有理解您所提到的“触摸跟踪”的问题。 - HardikDG
@HuseinBehboodiRad 我已更新代码,如果你仍有问题。 - HardikDG
单击功能正常。不需要新手势。长按并跟踪七段开关是问题所在。它的动画效果很差。 - Husein Behboudi Rad

1
- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionSingleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [self.scrollView addGestureRecognizer:singleTap];
}

- (void)actionSingleTap:(UITapGestureRecognizer *)sender {

    CGPoint touchPoint = [sender locationInView:self.scrollView];
    NSLog(@"Touch point coordinates are : %f - %f", touchPoint.x , touchPoint.y );

    UIView *hitImageView = [self.scrollView hitTest:touchPoint withEvent:nil];
    NSLog(@"%@", hitImageView);

    switch (hitImageView.tag) {
        case 1:
            // do something
            break;

        default:
            break;
    }

}

单击功能正常。不需要新手势。长按并跟踪七段开关是问题所在。它的动画效果很差。 - Husein Behboudi Rad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接