捏缩放 - 无法在缩放时保持图像位置固定

时间:2012-08-30 06:01:33

标签: objective-c ios xcode image-zoom pinchzoom

我是iphone开发的新手。我想缩放图像视图,我使用以下代码,

- (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;
}

通过使用上面的代码,我可以缩放图像,但我的问题是我的图像在缩放时运行到左下角。那就是我无法将我的图像位置固定在屏幕中央。

我希望图像位置在缩放时保持固定。对此有何帮助?

2 个答案:

答案 0 :(得分:6)

最后我找到了一个自己的解决方案。这里是图像缩放的完整代码示例:)这里图像位置在放大和缩小时保持固定。

- (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);

}

答案 1 :(得分:1)

试试NYOBetterZoom。这肯定是你的答案。 :)