我在我的项目中使用
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中进行滚动视图并使用故事板插座更改编程滚动视图