Thursday, April 12, 2012

How to zoom two Images placed side by side, together?

I have two scrollviews placed side by side and they can be zoomed individually. So far, it works fine. Now, there's a requirement to zoom the two images together. I was given the roambi app as reference in which two scrollviews can be scrolled together by scrolling either one of them for convenience during comparison. I've gone through scrollview delegate methods but was unable to achieve the required results. How do I do this?



Answer:





I had implemented something similar a while ago (I did it for buttons). This is how I did it:
Take two UIScrollViews and reference them (I've used firstScrollView and secondScrollView)
Take two UIButtons and reference them (I've used firstImgBtn and secondImgBtn). Set the delegates to both the scrollviews and use the following delegate methods:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    //return the respective button in the scrollview to be zoomed
    if(scrollView==firstScrollView){
        return firstImgBtn;
    }
    else{
        return secondImgBtn;
    }
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    // zoom in the other scrollview when one has zoomed
    if(zoomTogether){//a bool to decide whether to zoom the two together or not
        if(scrollView==firstScrollView){
            secondScrollView.zoomScale = firstScrollView.zoomScale;
        }
        else{
            firstScrollView.zoomScale = secondScrollView.zoomScale;
        }
    }
}
This can be applied to any subclass of UIView-in your case it will be UIImageViews

No comments:

Post a Comment