ios – UIScrollView中的UIControll未接收到触摸事件

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

我尝试了子类滚动视图并覆盖下面的代码

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

还补充说:

self.scrollView.delaysContenttouches = NO;

但仍然无法接收触摸事件.如何阻止scrollview阻止UIControl接收触摸?

更新

我在滚动视图上有一个轻击手势,因为我想在用户点击滚动视图时调用[self.scrollView endEditing:YES]来关闭键盘.当我将其拆下时,七个开关正在使用水龙头.

我将下面的代码添加到我的点按手势中:

tap.cancelstouchesInView = NO;

现在sevenswitch正在使用水龙头,但是通过触摸跟踪打开或关闭开关时会出现问题.

解决方法

我已经制作了样本,其中我在scrollview中有sevenswitch并且它正常工作
- (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是我放置在scrollview中的主容器视图,请检查这是否适合您

更新

正如我在评论中提到的,我没有得到你想用触摸跟踪说的但我已经在滚动视图中使用点击手势制作样本可能有所帮助

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(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外部的触摸事件,并且还检测到七个开关上的触摸/点击.如果您想在Storyboard中进行滚动视图并使用故事板插座更改编程滚动视图

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...