我在项目中使用SevenSwitch。我需要将它添加到UIScrollView
中,但是当我将它添加到滚动视图中时,控件似乎无法接收触摸事件。
我尝试了子类scrollview
并覆盖代码:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return NO;
}
还补充道:
self.scrollView.delaysContentTouches = NO;
但仍然无法接收触摸事件。如何阻止scrollview
阻止UIControl
接收触摸?
更新
我的滚动视图上有一个轻击手势,因为我希望当用户点击滚动视图时,我会调用[self.scrollView endEditing:YES]
来关闭键盘。当我将其移除时,七个开关正在使用水龙头。
我将以下代码添加到我的点按手势中:
tap.cancelsTouchesInView = NO;
现在sevenswitch
正在使用点按,但在使用触摸追踪切换on
或off
时会出现问题。
答案 0 :(得分:3)
我已经制作了样本,其中我在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
是我在滚动视图中放置的主要容器视图,请检查这是否适用于您
<强>更新强>
正如我在评论中提到的那样,我没有得到你想用触摸追踪所说的内容,但我已经在滚动视图中使用了点击手势制作样本,这可能会有所帮助
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中进行滚动视图并更改编程滚动视图与故事板插座
答案 1 :(得分:2)
尝试以下可能对您有所帮助
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
您将接触到:
-(void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
注意:强>
滚动视图还有一个手势识别器。默认情况下,任何时候只有1个手势识别器可以处理触摸。您需要让自己成为手势的代表,然后实施gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
以返回YES
。这将允许它与滚动视图同时工作。
答案 2 :(得分: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;
}
}