0
votes

I'm trying to display a large and high quality zoomable image using CATiledLayer and UIScrollView. My code is based on the iOS Large image downsizing sample code.

The hierarchy of my UIScrollView is like that :

  • UIScrollView
    • UIImageView : that represents the first thumbnail of the large image
    • Old TiledView
    • TiledView

I want that the image into the scrollview become movable.

I tried what I found on this StackOverflow topic and it works. But after the first zoom on the image, it becomes impossible to move the image. I don't know why ?

Here is my code :

-(id)initWithFrame:(CGRect)frame image:(UIImage*)_image {
    if((self = [super initWithFrame:frame])) {      
    // Set up the UIScrollView
        // Piece of code

        self.canCancelContentTouches = NO;

        UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture setMinimumNumberOfTouches:1];
        [panGesture setMaximumNumberOfTouches:1];
        [panGesture setDelegate:self];
        [frontTiledView addGestureRecognizer:panGesture];
        frontTiledView.exclusiveTouch = YES;

        UIPanGestureRecognizer* panGesture2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture2 setMinimumNumberOfTouches:1];
        [panGesture2 setMaximumNumberOfTouches:1];
        [panGesture2 setDelegate:self];
        [backgroundImageView addGestureRecognizer:panGesture2];
        backgroundImageView.exclusiveTouch = YES;

        UIPanGestureRecognizer* panGesture3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture3 setMinimumNumberOfTouches:1];
        [panGesture3 setMaximumNumberOfTouches:1];
        [panGesture3 setDelegate:self];
        [backTiledView addGestureRecognizer:panGesture3];
        backTiledView.exclusiveTouch = YES;
    }
    return self;
}

- (void)move:(UIGestureRecognizer*)sender {
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:frontTiledView];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        positionDeplacement = frontTiledView.center;
    }

    translatedPoint = CGPointMake(positionDeplacement.x+translatedPoint.x, positionDeplacement.y+translatedPoint.y);
    frontTiledView.center = translatedPoint;
    backTiledView.center = translatedPoint;
    backgroundImageView.center = translatedPoint;
}

Thanks.

1
This is a bit confusing. You have three views that you seem to want to move simultaneously and equally. I've loaded the LargeImage example and I'm able to pan and zoom that image without any problems. What are you trying to do that is different?Daniel Zhang
There are 3 views in the LargeImage example scrollview, that's why I want to move them simultaneously and equally. On what subview do you apply the pan gesture ?Seb
I'm not able to reproduce what you describe using your code. I'm not sure why you want to use a UIPanGestureRecognizer in a UIScrollView if that's what you are doing. The scroll view has its own delegate methods for panning.Daniel Zhang
I want to use UIPanGestureRecognizer in order to make the image into the scrollview movable. In the ImageScrollView file, we have 3 objects : frontTiledView, backTiledView and backgroundImageView. I tried to apply the gesture to those 3 views in order to get what I want. Maybe it's not the good way to do this.. Am I understandable ? :/ If you did it, can you send me your code ? I'll become the happiest guy in the world ^^Seb
Ok, I think I understand what you are asking. You want to move the image within the scroll view and not just use the scroll view to scroll over the image. I don't have the answer to how to do that!Daniel Zhang

1 Answers

0
votes

I found the solution to my problem. I just have to add again the gestures in the scrollViewDidEndZooming delegate method and it works like a charm :

Here is my code :

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    // set the new scale factor for the TiledImageView
    imageScale *=scale;
    CGRect imageRect = CGRectMake(0.0f,0.0f,CGImageGetWidth(image.CGImage) * imageScale,CGImageGetHeight(image.CGImage) * imageScale);

    // Create a new TiledImageView based on new frame and scaling.
    frontTiledView = [[TiledImageView alloc] initWithFrame:imageRect image:image scale:imageScale]; 
    [self addSubview:frontTiledView];
    [frontTiledView release];

    if (imageRect.size.width < 320 || imageRect.size.height < 460)
        [self initGestures];
}

- (void)initGestures {
    self.canCancelContentTouches = NO;
    frontTiledView.exclusiveTouch = YES;
    backgroundImageView.exclusiveTouch = YES;
    backTiledView.exclusiveTouch = YES;

    UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture setMinimumNumberOfTouches:1];
    [panGesture setMaximumNumberOfTouches:1];
    [panGesture setDelegate:self];
    [frontTiledView addGestureRecognizer:panGesture];

    UIPanGestureRecognizer* panGesture2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture2 setMinimumNumberOfTouches:1];
    [panGesture2 setMaximumNumberOfTouches:1];
    [panGesture2 setDelegate:self];
    [backgroundImageView addGestureRecognizer:panGesture2];

    UIPanGestureRecognizer* panGesture3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture3 setMinimumNumberOfTouches:1];
    [panGesture3 setMaximumNumberOfTouches:1];
    [panGesture3 setDelegate:self];
    [backTiledView addGestureRecognizer:panGesture3];
}