我制作了示例,其中滚动视图内有七个开关,并且它工作正常
- (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
是我放置在滚动视图内的主容器视图,请检查这是否适合您
Update
正如我在评论中提到的,我没有得到你想要通过触摸跟踪说的话,但我已经在滚动视图中使用点击手势制作了示例,这可能会有所帮助
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");
}
我以编程方式编写了所有新代码,它检测七个开关外部的触摸事件,并且还检测七个开关上的触摸/点击。
如果你想让它在故事板中制作滚动视图并使用故事板出口更改编程滚动视图