Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIControll in UIScrollView not receiving touch events

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.

I tried sub classing scrollview and overriding below code:

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

also added:

self.scrollView.delaysContentTouches = NO;

but still can not receive the touch event. How can I stop scrollview from preventing the UIControl from receiving touches?

Update

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;

and now the sevenswitch is working with tap but there is problems when make the switch on or off with touch tracking.

like image 626
Husein Behboudi Rad Avatar asked Mar 31 '16 14:03

Husein Behboudi Rad


Video Answer


1 Answers

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

In which _cntView is the main container view which i have placed inside the scrollview , please check if this working for you


Update

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

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

like image 65
HardikDG Avatar answered Oct 18 '22 10:10

HardikDG