gpt4 book ai didi

iphone - 如何在有或没有 ScrollView 的情况下使 ImageView 可缩放?

转载 作者:行者123 更新时间:2023-11-28 18:37:10 25 4
gpt4 key购买 nike

我的 IB 中有一个 UImageView,并在该 UImageView 上添加了一个 map 图像。我想让该图像捏缩放。

这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Map";

self.mapImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.mapScrollView addSubview:self.mapImageView];
[self.mapScrollView setContentSize:CGSizeMake(self.mapImageView.frame.size.width, self.mapImageView.frame.size.height)];
[self.mapScrollView setMinimumZoomScale:1.0];
[self.mapScrollView setMaximumZoomScale:4.0];
}

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

但这没有帮助,我可以看到 UIScrollView 即将出现,但我的图像没有任何变化。
提前致谢。

最佳答案

在你的 ViewController.h 文件中添加 UIScrollViewDelegate

然后将以下代码添加到您的ViewController.m 文件

如果您使用此代码,则无需添加 UIPinchGestureRecognizer

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

UIView* zoomView = [scrollView.delegate viewForZoomingInScrollView:scrollView];

CGRect zoomViewFrame = zoomView.frame;

if(zoomViewFrame.size.width < scrollView.bounds.size.width)

{

zoomViewFrame.origin.x = (scrollView.bounds.size.width - zoomViewFrame.size.width) / 2.0;

}

else

{
zoomViewFrame.origin.x = 0.0;
}

if(zoomViewFrame.size.height < scrollView.bounds.size.height)

{ zoomViewFrame.origin.y = (scrollView.bounds.size.height - zoomViewFrame.size.height) / 2.0;

}
else

{
zoomViewFrame.origin.y = 0.0;
}
zoomView.frame = zoomViewFrame;
}

改变viewDidLoad如下

- (void)viewDidLoad
{
[super viewDidLoad];
self.mapScrollView.delegate = self;
self.mapScrollView.minimumZoomScale = 1.0;
self.mapScrollView.maximumZoomScale = 4.0;
UIImage * myImage= [UIImage imageNamed:@"Background.png"]; //add your image here
[self.mapImageView setImage:myImage];
[self.mapImageView sizeToFit];
self.mapScrollView.contentSize = myImage.size;

}

在这里指定要缩放的imageView

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

要避免 UIScrollView 在 zoomimg 上的奇怪行为,请使用此方法

- (void)view:(UIView*)view setCenter:(CGPoint)centerPoint
{
CGRect viewFrame = view.frame;
CGPoint contentOffset = self.mapScrollView.contentOffset;

CGFloat x = centerPoint.x - viewFrame .size.width / 2.0;
CGFloat y = centerPoint.y - viewFrame .size.height / 2.0;

if(x < 0)
{
contentOffset.x = -x;
viewFrame .origin.x = 0.0;
}
else
{
viewFrame .origin.x = x;
}
if(y < 0)
{
contentOffset.y = -y;
viewFrame .origin.y = 0.0;
}
else
{
viewFrame .origin.y = y;
}

view.frame = viewFrame ;
self.mapScrollView.contentOffset = contentOffset;
}

然后在viewDidAppear上调用上面的方法

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGPoint centerPoint = CGPointMake(CGRectGetMidX(self.mapScrollView.bounds),
CGRectGetMidY(self.mapScrollView.bounds));
[self view:self.mapImageView setCenter:centerPoint];
}

看看这个link如果您需要更多说明。我从那个链接学到了这项技术。

关于iphone - 如何在有或没有 ScrollView 的情况下使 ImageView 可缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16665820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com