触摸UIScrollView的事件

时间:2013-02-05 10:00:37

标签: iphone uiscrollview xcode4.5

如何获取UIScrollView的触摸事件。我有一个UIViewController,我在viewController中放置了一个scrollView。我需要将imageViews添加到scrollView中,如果点击一个特定的图像,它应该重定向到相应的viewController。 我知道如何将触摸事件添加到UIViewController.But,这对滚动视图不起作用。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch= [touches anyObject];
    if ([touch view] == img1)
    {

        ViewController2 *viewController2=[[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
        [self presentViewController: viewController2 animated:YES completion:nil];



    }
}

我可以获得scrollView的触摸事件吗?

3 个答案:

答案 0 :(得分:0)

执行此操作的唯一方法是创建自定义UIScrollView您的自定义UIScrollView将是UIScrollView的子类。

还有更多选项,您可以查看this

答案 1 :(得分:0)

设置如下内容:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];    

and you get the touch in:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
  { 
    CGPoint touchPoint=[gesture locationInView:scrollView];
  }

答案 2 :(得分:0)

您可以使用ScrollViewDelegate并使用“ScrollViewDidScroll”函数来获取scrollview滚动事件。

设置点击手势识别器:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];   



- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
  { 
    CGPoint touchPoint=[gesture locationInView:scrollView];
  }
相关问题