UIControll in UIScrollView does not receive touch events

I use SevenSwitch in my project. I need to add it to a UIScrollView , but it seems that the control cannot receive touch events when I add it to the scroll list.

I tried a subclass of scrollview and redefined the code below:

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

also added:

 self.scrollView.delaysContentTouches = NO; 

but still can't get the touch event. How to stop scrollview from preventing getting UIControl touches?

Update

I have a gesture of clicking on my scroll view because I want when I click on the scroll screen I would call [self.scrollView endEditing:YES] to close the keyboard. When I remove it, seven switches work with a tap.

I am adding the code below to my touch gesture:

 tap.cancelsTouchesInView = NO; 

and now sevenswitch works with the crane, but there are problems when creating the on or off switch with touch tracking.

+7
ios objective-c uicontrol uiscrollview uiswitch
source share
3 answers

I made a sample in which I have sevenswitch inside scrollview and it works fine

 - (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"); } 

In which _cntView is the main view of the container I placed inside scrollview, check if this works for you


Update

As I mentioned in the comment, I didn’t get what you are trying to say with touch tracking, but I did a swipe with a tap gesture that could 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"); } 

I made all the new code programmatically, and it detects touch events outside of 7Switch, and also detects touch / tap on seven switches.
If you want to do this scrollview in a Storyboard and change the software scroll using a storyboard

+3
source share

Try the following that may help you.

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scrollView addGestureRecognizer:singleTap]; 

and you will get the following touches:

 -(void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture { CGPoint touchPoint=[gesture locationInView:scrollView]; } 

Note:

The scroll list also has a gesture recognizer. By default, only one gesture recognizer can process strokes at any given time. You need to make yourself a delegate of your gesture, and then implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: to return YES . This will allow it to work simultaneously with the scroll view.

+2
source share
 - (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; } } 
+1
source share

All Articles