简体   繁体   English

如何在以UIScrollView作为子视图的父视图中检测滑动?

[英]How to detect swipe in a parent view having UIScrollView as a subview?

I have 3 nested views, Main view (parent) -> UIScrollView -> ImageView. 我有3个嵌套视图,主视图(父视图)-> UIScrollView-> ImageView。 I am building a content viewer, where a user swipes to get the next/previous image from a list. 我正在构建一个内容查看器,用户在其中滑动即可从列表中获取下一张/上一张图像。 But i want this next image to load only after the user has reached either the rightmost/bottom(end of the image) or the leftmost/top corner. 但我希望仅在用户到达最右/底(图像的末端)或最左/上角后才加载下一张图像。 There is a transition animation involved, where in the next/previous image follows the swipe while loading the next item. 涉及过渡动画,在下一个/上一个图像中,在加载下一个项目时跟随着滑动。 How should I go about achieving this? 我应该如何实现这一目标?

PS:I have already tried adding gesture recognizers in the parent view, but it doesn't work. PS:我已经尝试在父视图中添加手势识别器,但是它不起作用。

You should use Swipe Gestures 您应该使用滑动手势

UISwipeGestureRecognizer *swipe_left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe_left)];
    swipe_left.direction = UISwipeGestureRecognizerDirectionLeft;
    [yourView addGestureRecognizer:swipe_left];
 UISwipeGestureRecognizer *swipe_right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe_right)];
    swipe_right.direction = UISwipeGestureRecognizerDirectionRight;
    [yourView addGestureRecognizer:swipe_right];

-(void)swipe_left
{
    //Write your content offset if required
}


-(void)swipe_right
{

   someView.contentOffset = CGPointMake(470, 0);      //Example
}

Regards 问候

Deepak 迪帕克

Happy New Year!! 新年快乐!!

It seems like you are using the scrollview to move the current image around and then when the user encounters either of the two trigger points (left-top/right-bottom) you want to trigger your animation and load in the next image. 似乎您正在使用滚动视图来移动当前图像,然后当用户遇到两个触发点(左上/右下)中的任意一个时,您要触发动画并加载到下一个图像中。 If that is the case, why don't you just use the scrollView delegate method -scrollViewDidScroll:(UIScrollView *)scroller ? 如果是这种情况,为什么不只使用scrollView委托方法-scrollViewDidScroll:(UIScrollView *)scroller In that method you can check the scrollView's contentOffset property and compare it against the current image's dimensions. 在该方法中,您可以检查scrollView的contentOffset属性,并将其与当前图像的尺寸进行比较。 When the contentOffset falls within your parameters for triggering the animation and load, do so. contentOffset处于触发动画和加载的参数之内时,请这样做。

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

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