gpt4 book ai didi

iphone - 在 UIScrollView 中缩放 UIImageView 越界

转载 作者:技术小花猫 更新时间:2023-10-29 11:25:20 26 4
gpt4 key购买 nike

我已经在我的 UIScrollView 中成功实现了 UIImageView 的缩放,但是我遇到了一个令我恼火的奇怪问题。

基本上,当我放大图像时

enter image description here

我可以平移 View 以实际滚动出图像边界,然后我会留下一个黑色区域,如下所示: enter image description here

当我放大得更多时,我可以让黑色边框填满整个屏幕!同时,在 iPhone 照片应用程序中,您无法缩小实际图像。我的实现有什么问题?

看起来像这样:

UIImage *imageToDisplay = [UIImage imageWithData:tmpImageData];
imageViewMain.image = imageToDisplay;
imageViewMain.frame = CGRectMake(0, 0, scrollViewMain.frame.size.width, scrollViewMain.frame.size.height);
imageViewMain.contentMode = UIViewContentModeScaleAspectFit;

scrollViewMain.delegate = self;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.maximumZoomScale = 9.0;
scrollViewMain.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollViewMain.contentSize = imageViewMain.frame.size;

[self.view addSubview:scrollViewMain];
[scrollViewMain addSubview:imageViewMain];

我也实现了这个方法:

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageViewMain;
}

提前致谢!

编辑:当我第一次加载 View 时,我缩小了 UIImageView 以适应 ScrollView ,图像看起来像这样:

enter image description here

最佳答案

只需尝试这段代码....在需要的地方更改一些代码...为您的UIImageView 使用UIPinchGestureRecognizer,然后将此图像添加到您的UIScrollView 像下面这样..

UIImageView *img = [[UIImageView alloc]init];
img.image = [UIImage imageWithData:tmpImageData];
img.userInteractionEnabled = YES;
img.frame = CGRectMake(210,100, 200, 200);/// define frame which you want...
[img sizeToFit];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[img addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
[scrollViewMain addSubview:img];
[scrollViewMain bringSubviewToFront:img];
[img release];

然后将下面的代码粘贴到您的 .m 文件中...

#pragma mark GestureRecognizer Methods
-(void)scale:(id)sender
{
UIView *imgTempGest = [sender view];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

[imgTempGest setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

对于 LastScale,只需在 .h 文件中定义该变量,如下所示...

CGFloat lastScale;

希望对你有帮助...

关于iphone - 在 UIScrollView 中缩放 UIImageView 越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15570240/

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