触摸UI Scroll View中的视图上的事件

时间:2014-06-05 06:55:41

标签: ios iphone objective-c uiscrollview

我在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];
}

点击事件似乎根本不起作用。请建议。提前致谢

2 个答案:

答案 0 :(得分:0)

尝试以下代码,并且每个视图都是以编程方式创建的。

- (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];
}

答案 1 :(得分:0)

首先,您必须通过启用userInteractionEnabled来确保您的视图可以触摸。 其次,您需要为自来水事件声明numberOfTapsRequired

然后你有这样的代码:

UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSlidersActionView1:)];
tapSlidersView1.numberOfTapsRequired = 1;

self.View1.userInteractionEnabled = YES;
[self.View1 addGestureRecognizer:tapSlidersView1];