我的问题是,当我分页到我的subView时,我的Button无法正常工作。 ScrollView上的按钮是否一切正常。
主ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.sView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.sView.contentSize = CGSizeMake(self.sView.frame.size.width,
self.sView.frame.size.height*2);
self.sView.pagingEnabled=YES;
self.sView.backgroundColor = [UIColor redColor];
self.sView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.sView];
self.sVC = [[StartViewController alloc] initWithNibName:nil bundle:nil];
self.sVC.view.frame = CGRectMake(0, 0, self.sView.frame.size.width, self.sView.frame.size.height);
[self.sView addSubview:self.sVC.view];
[self addChildViewController:self.sVC];
}
Sub ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.playView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
self.playView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.playView];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
btn.backgroundColor = [UIColor whiteColor];
[btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[self.playView addSubview:btn];
}
- (void)show:(id)sender
{
NSLog(@"--test");
}
答案 0 :(得分:0)
您需要启用父滚动视图的用户交互
[_sView setUserInteractionEnabled:YES];
答案 1 :(得分:0)
好吧,主要问题是你的按钮位于视图控制器视图框之外。
你的视图控制器显然是(0,0,320,screenHeight),其中screenHeight是568或480.这是默认行为,你可能永远不会改变它。 仅当事件发生在视图控制器的框架内时,事件才会起作用。
您的代码存在的问题是您已将播放视图框架定义为(0,screenHeight,320,screenHeight)。因此,这个播放视图位于viewcontroller的视图下方,无法捕获任何事件。由于您的按钮位于播放视图中,因此它不在视图控制器中,并且无法正常工作。
您应该做的是通过设置其框架(0,0,320,screenHeight)在视图控制器中设置您的播放视图,以便捕获事件。然后,当您将视图控制器添加到其父级时,可以更改它的整个框架。
在SubviewController中:
- (void)viewDidLoad
{
[super viewDidLoad];
self.playView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.playView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.playView];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
btn.backgroundColor = [UIColor whiteColor];
[btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[self.playView addSubview:btn];
}
- (void)show:(id)sender
{
NSLog(@"--test");
}
在您的MainView控制器中:
- (void)viewDidLoad
{
[super viewDidLoad];
self.sView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.sView.contentSize = CGSizeMake(self.sView.frame.size.width,
self.sView.frame.size.height*2);
self.sView.pagingEnabled=YES;
self.sView.backgroundColor = [UIColor redColor];
self.sView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.sView];
self.sVC = [[StartViewController alloc] initWithNibName:nil bundle:nil];
self.sVC.view.frame = CGRectMake(0, _sView.frame.size.height, _sView.frame.size.width, _sView.frame.size.height);
[self.sView addSubview:self.sVC.view];
[self addChildViewController:self.sVC];
}