繁体   English   中英

触摸UIScrollView的事件

[英]Touches event for a UIScrollView

我该如何获取UIScrollView的触摸事件?我有一个UIViewController,并在viewController中放置了一个scrollView。我需要在scrollView中添加imageViews,如果单击了特定图像,则应将其重定向到其对应的viewController。 我知道如何向UIViewController添加触摸事件,但这不适用于scrollView。

-(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的touches事件?

要做到这一点的唯一方法是通过创建一个自定义UIScrollView您的自定义UIScrollView会的一个子类UIScrollView

也为更多的选项,你可以检查

设置如下所示:

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

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

设置点击手势识别器:

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



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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM