UI Scroll View 内的视图上的触摸事件
Posted
技术标签:
【中文标题】UI Scroll View 内的视图上的触摸事件【英文标题】:Touch events on views inside UI Scroll View 【发布时间】:2014-06-05 06:55:41 【问题描述】:我在 MainView 上实现了一个 UIScrollView,如下所示:
UIScrollView* scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(-160, -540, 480, 1300)];
[scrollView addSubview:self.MainView];
scrollView.contentSize = CGSizeMake(400, 2000);
[self.view addSubview:scrollView];
我在 Storyboard 的 MainView 下有以下视图:
Main View ----> View1, View2
我想分别用 View 1 和 View 2 实现触摸事件。为此,我有以下代码:
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSlidersActionView1:)];
[self.View1 addGestureRecognizer:tapSlidersView1];
UITapGestureRecognizer *tapSlidersView2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSlidersActionView2:)];
[self.View2 addGestureRecognizer:tapSlidersView2];
以及相应的事件处理程序:
- (void)tapSlidersActionView1:(UITapGestureRecognizer *)sender
[self.navigationController pushViewController:DropDownView1 animated:YES];
- (void)tapSlidersActionView2:(UITapGestureRecognizer *)sender
[self.navigationController pushViewController:DropDownView2 animated:YES];
点击事件似乎根本不起作用。请建议。提前致谢
【问题讨论】:
主视图、view1和view2的框架是什么? 是的。在主视图中,我在情节提要中有两个子视图:View1 和 View2 只需创建一个 self.view 相同框架的 invisibleView 然后将其添加到 self.view 但在此之前添加一个您的临时视图。所以试试看,让我知道 【参考方案1】:首先,您必须通过启用userInteractionEnabled
来确保您的视图是可触摸的。
其次,您需要为您的点击事件声明numberOfTapsRequired
。
然后你的代码是这样的:
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSlidersActionView1:)];
tapSlidersView1.numberOfTapsRequired = 1;
self.View1.userInteractionEnabled = YES;
[self.View1 addGestureRecognizer:tapSlidersView1];
【讨论】:
@StackCoder 提供更多信息。你有什么改变?您是否确保使用断点跳入了 tap 函数?是否有任何其他视图覆盖并掩盖了您的 View1 和 View2?【参考方案2】:尝试以下代码,并且每个视图都是以编程方式创建的..
- (void)viewDidLoad
[super viewDidLoad];
UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[mainView setBackgroundColor:[UIColor blackColor ]];
[self.view addSubview:mainView];
UIScrollView* scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,mainView .bounds.size.width, mainView.bounds.size.height)];
scrollView.contentSize = CGSizeMake(400, 2000);
[mainView addSubview:scrollView];
UIView *View1=[[UIView alloc]initWithFrame:CGRectMake(50, 150, 100, 100)];
[View1 setBackgroundColor:[UIColor grayColor]];
[scrollView addSubview:View1];
UIView *View2=[[UIView alloc]initWithFrame:CGRectMake(200, 150, 100, 100)];
[View2 setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:View2];
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSlidersActionView1:)];
[View1 addGestureRecognizer:tapSlidersView1];
UITapGestureRecognizer *tapSlidersView2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSlidersActionView2:)];
[View2 addGestureRecognizer:tapSlidersView2];
// Do any additional setup after loading the view from its nib.
- (void)tapSlidersActionView1:(UITapGestureRecognizer *)sender
//your class
// secViewController *DropDownView1=[[secViewController alloc]init];
[self.navigationController pushViewController:DropDownView1 animated:YES];
- (void)tapSlidersActionView2:(UITapGestureRecognizer *)sender
//your class
// secViewController *DropDownView2=[[secViewController alloc]init];
[self.navigationController pushViewController:DropDownView2 animated:YES];
【讨论】:
我无法以编程方式创建视图。需要他们完成故事板 好的,在你的主视图上创建一个双视图并制作一个轻击手势识别器对象并将它们添加到第一个视图和第二个视图..不要忘记在主视图上为这些视图设置一个 iboutlet以上是关于UI Scroll View 内的视图上的触摸事件的主要内容,如果未能解决你的问题,请参考以下文章