简体   繁体   中英

UIScrollView inside UIView, UIView gesture one-liner

I have a UIScrollView inside a UIView . For customized paging purpose I have set the clipping of this UIScrollView to No so the UIScrollView still shows up in Region A and Region B of the UIView .

[ region A [UIScrollView] region B ]

Now I want Region AB to be able to trigger scroll events of the UIScrollView's when touched.. I remember there is an one liner (something like [X addGestureRecognizer ...] ) that does the trick but I've forgotten what it is... It would be great if someone can tell me what that is!

This won't let you have a fluid control on your scrolling, like the one you get from the UIScrollView .

However, you can use this code :

UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(viewSwiped:)];
[leftSwipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:leftSwipeGestureRecognizer];
[leftSwipeGestureRecognizer release];

UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(viewSwiped:)];
[rightSwipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:rightSwipeGestureRecognizer];
[rightSwipeGestureRecognizer release];

firing this method :

- (void)viewSwiped:(UISwipeGestureRecognizer *)swipeGestureRecognizer {
    switch (swipeGestureRecognizer.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
            // Make your scroll view scroll
            break;
        case UISwipeGestureRecognizerDirectionRight:
            // Make your scroll view scroll
            break;
        default:
            // Do Nothing
            break;
    }
}

It will allow you to retrieve the swipes gestures and page your UIScrollView accordingly.

Two other solutions might be better :

  1. You could use the UIPanGestureRecognizer the same way as above. It recognizes real pan gestures and not only discrete swipes but the implementation of the sync between your finger and the UIScrollView will be slightly more complicated.
  2. You could let your UIScrollView take the total width of the UIView and manage the paging by yourself, recalculating your steps while tracking the current state of the UIScrollView via UIScrollViewDelegate . Again, a bit harder.

Hope this will help,

如果要将UIScrollView的滚动手势重定向到另一个视图,可以执行以下操作:

[paddingView addGestureRecognizer:scrollView.panGestureRecognizer];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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