8

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.

Husein Behboudi Rad
  • 5,434
  • 11
  • 57
  • 115

2 Answers2

3

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

HardikDG
  • 5,892
  • 2
  • 26
  • 55
  • I also create what you do in a new fresh create view controller that made in storyboard. Now sevenswitch works with tap but have problem with track touching. Also I creating my scroll view programmatically and add it to the view. In this scrollview it is not working even by tapping. – Husein Behboudi Rad Apr 04 '16 at 15:22
  • @HuseinBehboodiRad so now does it works in your scrollview programatically? also ` touch tacking` means it don't work with pan/drag gesture on the switch ? – HardikDG Apr 04 '16 at 16:23
  • touch tracking do not work with storyboard and with my programmatically scrollview added – Husein Behboudi Rad Apr 04 '16 at 16:52
  • sorry i am not getting your issue of touch tracking – HardikDG Apr 04 '16 at 17:25
  • @HuseinBehboodiRad i have updated code, if you still have issue – HardikDG Apr 07 '16 at 03:02
  • Single tap is working. Do not required a new gesture. holding and tracking the sevenswitch is the problem . It has bad animation. – Husein Behboudi Rad Apr 07 '16 at 07:19
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;
    }

}
iAleksandr
  • 595
  • 10
  • 11