UIScrollView中的UIControll未接收触摸事件

Hus*_*Rad 7 objective-c uiscrollview uicontrol uiswitch ios

我在我的项目中使用SevenSwitch.我需要将其添加到a中,UIScrollView但是当我将其添加到滚动视图中时,控件似乎无法接收触摸事件.

我尝试了子类化scrollview和覆盖下面的代码:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
  return NO;
}
Run Code Online (Sandbox Code Playgroud)

还补充说:

self.scrollView.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)

但仍然无法接收触摸事件.如何才能停止scrollview从防止UIControl从接收触摸?

更新

我在滚动视图上有一个轻击手势,因为我希望当用户点击滚动视图时我打电话[self.scrollView endEditing:YES]关闭键盘.当我将其拆下时,七个开关正在使用水龙头.

我将下面的代码添加到我的点按手势中:

tap.cancelsTouchesInView = NO;
Run Code Online (Sandbox Code Playgroud)

现在sevenswitch正在使用水龙头但是在进行切换onoff触摸跟踪时会出现问题.

Har*_*kDG 3

我制作了示例,其中滚动视图内有七个开关,并且它工作正常

- (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");
}
Run Code Online (Sandbox Code Playgroud)

其中_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");
}
Run Code Online (Sandbox Code Playgroud)

我已经以编程方式编写了所有新代码,它检测七个开关外部的触摸事件,并且还检测七个开关上的触摸/点击。
如果你想让它在故事板中制作滚动视图并使用故事板出口更改编程滚动视图