2

我是 iphone 开发的新手。我想缩放 imageview ,我正在使用以下代码,

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

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

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

4

2 回答 2

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

}
于 2012-08-30T08:01:53.260 回答
1

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

于 2012-08-30T06:12:27.227 回答