Question

I am new to iphone development. I want zoom an imageview , I am using the following code,

- (void)viewDidLoad
{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.scrollViewOutlet.minimumZoomScale=0.5;

    self.scrollViewOutlet.maximumZoomScale=4.0;

    self.scrollViewOutlet.contentSize=self.imagetobeZoomed.image.size;

    self.scrollViewOutlet.clipsToBounds=YES;

    self.scrollViewOutlet.delegate=self;

}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{


    return self.imagetobeZoomed;
}

By using the code above I am able to zoom the image , but my problem is my image runs to the bottom left corner on zooming . That is I am not able to keep my image position fixed to the center of the screen.

I want image position to be remain fixed on zooming . Any help on this?

Was it helpful?

Solution

Finally I have found a solution my self.Here is complete code sample for image zoom :) Here the image position remains fixed on zooming in and out .

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.scrollViewOutlet.minimumZoomScale=0.5;
    self.scrollViewOutlet.maximumZoomScale=4.0;
    self.scrollViewOutlet.contentSize=CGSizeMake(self.imagetobeZoomed.frame.size.width,       self.imagetobeZoomed.frame.size.height);
    self.scrollViewOutlet.clipsToBounds=YES;
    self.scrollViewOutlet.delegate=self;
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imagetobeZoomed;
}


-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{   
    UIView *subView=[scrollView.subviews objectAtIndex:0];
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)? 
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)? 
(scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
     subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, 
                             scrollView.contentSize.height * 0.5 + offsetY);

}

OTHER TIPS

Just try NYOBetterZoom. This is your answer for sure. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top