Search code examples
iphoneuiscrollviewsubviewzoominguiscrollviewdelegate

Zooming uiscrollview also zoom anoher one


I have a screen in my project that contains 3 UIScrollView. These scrollviews contain an image, when i am trying to zoom one of the scrollview I dont know how but all of them are zooming together.

This is my code:

if ([FrameString isEqualToString:@"LB"]) {
    leftButton.hidden = YES;

    if (imageLeft == nil) {
        imageLeft = [[UIImageView alloc]initWithImage:pickedImage];
    }

    imageLeft.frame = CGRectMake(0 , 0, imageLeft.frame.size.width/4, imageLeft.frame.size.height/4);
    self.Scrollview.maximumZoomScale = 0.9;
    self.Scrollview.minimumZoomScale = 0.6;
    self.Scrollview.frame = CGRectMake(self.Scrollview.frame.origin.x, self.Scrollview.frame.origin.y, self.Scrollview.frame.size.width, self.Scrollview.frame.size.height);
    [self.Scrollview setContentSize:CGSizeMake(imageLeft.frame.size.width, imageLeft.frame.size.height)];
    self.Scrollview.clipsToBounds = YES;
    self.Scrollview.delegate = self;
    [self.Scrollview setScrollEnabled:YES];
    [self.Scrollview addSubview:imageLeft];
}

if ([FrameString isEqualToString:@"RDB"]) {
    RightDownButton.hidden = YES;
    if (RightDown == nil) {
        RightDown = [[UIImageView alloc]initWithImage:pickedImage];
    }

    RightDown.frame = CGRectMake(0 , 0, RightDown.frame.size.width/3, RightDown.frame.size.height/3);
    self.scrollview1.maximumZoomScale = 0.9;
    self.scrollview1.minimumZoomScale = 0.6;
    self.scrollview1.frame = CGRectMake(self.scrollview1.frame.origin.x, self.scrollview1.frame.origin.y, self.scrollview1.frame.size.width, self.scrollview1.frame.size.height);
    [self.scrollview1 setContentSize:CGSizeMake(RightDown.frame.size.width, RightDown.frame.size.height)];
    self.scrollview1.clipsToBounds = YES;
    self.scrollview1.delegate = self;
    [self.scrollview1 setScrollEnabled:YES];
    [self.scrollview1 addSubview:RightDown];

}
if ([FrameString isEqualToString:@"RUB"]) {
    rightUpButton.hidden = YES;
    if (RightUp == nil) {
        RightUp = [[UIImageView alloc]initWithImage:pickedImage];
    }

    RightUp.frame = CGRectMake(0 , 0, RightUp.frame.size.width/3, RightUp.frame.size.height/3);
    self.scrollview2.maximumZoomScale = 0.9;
    self.scrollview2.minimumZoomScale = 0.6;
    self.scrollview2.frame = CGRectMake(self.scrollview2.frame.origin.x, self.scrollview2.frame.origin.y, self.scrollview2.frame.size.width, self.scrollview2.frame.size.height);
    [self.scrollview2 setContentSize:CGSizeMake(RightUp.frame.size.width, RightUp.frame.size.height)];
    self.scrollview2.clipsToBounds = YES;
    self.scrollview2.delegate = self;
    [self.scrollview2 setScrollEnabled:YES];
    [self.scrollview2 addSubview:RightUp];
}

Solution

  • When zooming, its calling the same delegate for both the scrollViews. So irrespective of which scrollView is zooming, everything in the delegate method gets implemented.

    an easy way to fix this would be to set a tag for both the scrollviews

    like self.Scrollview.tag = 1 adn self.scrollview1.tag = 2

    check for the tag in the scrollView delegate methods

    and run it only if the tag matches the one you are interested in.

    eg:-

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    if (scrollView.tag == TagYouAreInterestedIn){
        //do stuff
    
        }
       return view;
    }
    

    Hope this helps.