简体   繁体   中英

Using swipe gesture within UIScrollView without disabling horizontal and vertical scrolling

I looked through other solutions, yet I can't find the right solution for myself. I have a UIImageView within a UIScrollView , where I show big pictures.

I have pinch gesture enabled within my UIScrollView as well as left and right swipe gesture recognizers.

Right now, scrollview's pan gesture seems to disable, (or corrupt) swipe gestures. I don't want to disable horizontal or vertical scrolling on my UIScrollView , and initial scaling of my photos are too large to disable horizontal scrolling.

What I want to do is to trigger swipe gesture when i come at the edge of my UIScrollView.

Here are some codes;

- (void)viewDidLoad
{
    // recognizer for pinch gestures
    UIPinchGestureRecognizer *pinchRecognizer =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
    [self.myScrollView addGestureRecognizer:pinchRecognizer];
    [self.myScrollView setClipsToBounds:NO];

    // recognizer for swipe gestures
    UISwipeGestureRecognizer *recognizer;

    // left and right swipe recognizers for left and right animation
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self myScrollView] addGestureRecognizer:recognizer];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self myScrollView] addGestureRecognizer:recognizer];
    ....

My left swipe handler, currently left and right swipes don't have any additional features

-(void)handleLeftSwipe:(UISwipeGestureRecognizer *)recognizer
{    
    if(!self.tableView.hidden) self.tableView.hidden = YES;
    [self showRequiredStuff];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.75;
    transition.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromLeft;
    transition.delegate = self;
    [self.view.layer addAnimation:transition forKey:nil];

My initial screen size;

#define IMAGE_VIEW_WIDTH 320.0
#define IMAGE_VIEW_HEIGHT 384.0

I use scaling for pictures, to make them scale as small as they can be, but most of them are wide images, which case horizontal scrolling is enabled, and vertical scrolling is disabled. Although my swipe handlers are horizontal as well.

I suppose I have clearly explained what is going on and what I need. I have posted codes because I am a newbie on iphone application development, I both want to help other people to see as much code as they can, and maybe someone will point out any bad programming, and we all will benefit from it.

Additional findings from related solutions; After setting;

@interface myViewController () <UIScrollViewDelegate>

and

self.myScrollView.delegate = self;

Detecting if a the edge is reached, horizontally

- (BOOL) hasReachedAHorizontalEdge {
    CGPoint offset = self.myScrollView.contentOffset;
    CGSize contentSize = self.myScrollView.contentSize;
    CGFloat height = self.myScrollView.frame.size.height;
    CGFloat width  = self.myScrollView.frame.size.width;

if ( offset.x == 0 || 
    (offset.x + width) == contentSize.width ) {
    return YES;
}

    return NO;

}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if ( [self hasReachedAHorizontalEdge] ) {
        NSLog(@"Reached horizontal edge.");
        // required here 
    }
}

At this point, all I need to disable the scrolling at the reached end, etc. if I reached right edge of scroll, I need to disable only right scrolling, that way swipe will be triggered.

I added a thin UIView over the edges of my scrollView and have them recognize swipes. I could not get the scrollView to cooperate with another swipe gesture recognizer. This isn't ideal, but it works for now. I will have to investigate if there is a way to override the scrollView's swipe gesture recognizer.

The trick would be to check and see if the swipe starts near the edges, and then to decide whether to call the scrollView's or my swipe recognizer.

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