2
votes

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.

1
Accepted answer here might help you find the solution.Rok Jarc
@rokjarc I saw that solution before I post this question. It is not available for this solution, because my images are bigger than the screen size initially (before it is zoomed), that means i should enable scrolling all the time.Bartu
In David's case scrolling is disabled only while zooming in/out. That's how user will usually use the UIScrollView.Rok Jarc
According to David's solution "The key in my case, is to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior." This means, if the zoom scale is 1, when it is not zoomed, there is no scrolling. I want to enable scrolling because my images are bigger then the screen size BEFORE it is zoomed in. Not suitable for what I am trying to achieve.Bartu
Of course, you're right. I missinterpreted the code there.Rok Jarc

1 Answers

6
votes

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.