UIScrollView 中的 UIControll 未接收到触摸事件 [英] UIControll in UIScrollView not receiving touch events
问题描述
我在我的项目中使用 SevenSwitch.我需要将它添加到 UIScrollView
中,但是当我将它添加到滚动视图时,控件似乎无法接收触摸事件.
I use SevenSwitch in my project. I need to add it into a UIScrollView
but it seems that the control can not receive touch events when I add it into scroll view.
我尝试子类化 scrollview
并覆盖下面的代码:
I tried sub classing scrollview
and overriding below code:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return NO;
}
还补充:
self.scrollView.delaysContentTouches = NO;
但仍然无法接收触摸事件.如何阻止 scrollview
阻止 UIControl
接收触摸?
but still can not receive the touch event. How can I stop scrollview
from preventing the UIControl
from receiving touches?
更新
我在我的滚动视图上有一个点击手势,因为我希望当用户点击滚动视图时我调用 [self.scrollView endEditing:YES]
来关闭键盘.当我取下它时,七个开关与水龙头一起工作.
I have a tap gesture on my scroll view because I want that when user tap the scroll view I call [self.scrollView endEditing:YES]
to close the keyboard. When I remove it the seven switch is working with tap.
我将以下代码添加到我的点击手势中:
I add below code to my tap gesture:
tap.cancelsTouchesInView = NO;
现在sevenswitch
正在使用tap,但是在使用触摸跟踪切换on
或off
时会出现问题.
and now the sevenswitch
is working with tap but there is problems when make the switch on
or off
with touch tracking.
推荐答案
我已经制作了示例,其中我在滚动视图中有 Sevenswitch 并且它工作正常
I have made sample in which i have sevenswitch inside the scrollview and it's working properly
- (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
是我放置在滚动视图中的主容器视图,请检查这是否适合您
In which _cntView
is the main container view which i have placed inside the scrollview , please check if this working for you
更新
正如我在评论中提到的,我没有得到您想要通过触摸跟踪表达的意思,但我在滚动视图中使用点击手势制作了示例,这可能会有所帮助
As i mention in the comment i didn't getting what you are trying to say with touch tracking but i have made sample with tap gesture in the scrollview which may help
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 之外的触摸事件,还可以检测到七个开关上的触摸/点击.
如果您想让它在 Storyboard 中制作滚动视图并使用以下命令更改编程滚动视图故事板出口
I have made all the new code programatically and it detects touch events outside the sevenSwitch and also detect touch/tap on the seven switch also.
If you want to make it make scrollview in Storyboard and change the programatic scrollview with the storyboard outlet
这篇关于UIScrollView 中的 UIControll 未接收到触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!