gpt4 book ai didi

iphone - UIScrollview 具有多个图像并捏合缩放

转载 作者:行者123 更新时间:2023-12-03 20:28:07 33 4
gpt4 key购买 nike

我正在尝试在 iPhone 上复制“照片”应用程序。意思是,具有多个图像的 ScrollView ,并且每个图像都可以捏合来放大/缩小。

Apple ScrollViewSuite 示例代码仅处理 ScrollView 内的 1 个图像。我发现对于多个图像,滚动图像变得不可预测。此外,捏合缩放后 View 会变得不居中。

我在 Stack Overflow 上找到了这个:Zoom UIScrollView with multiple images

但是,我应该在 ScrollView 委托(delegate)函数中编写什么?

最佳答案

这就是我发现的工作原理。支持多幅图像的分页和缩放。享受吧!

#define VIEW_FOR_ZOOM_TAG (1)

@implementation SVViewController

- (void)viewDidLoad {
[super viewDidLoad];

UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;

CGRect innerScrollFrame = mainScrollView.bounds;

for (NSInteger i = 0; i < 3; i++) {
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:@"page%d", i + 1]]];
imageForZooming.tag = VIEW_FOR_ZOOM_TAG;

UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 1.0f;
pageScrollView.maximumZoomScale = 2.0f;
pageScrollView.zoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView addSubview:imageForZooming];

[mainScrollView addSubview:pageScrollView];

if (i < 2) {
innerScrollFrame.origin.x += innerScrollFrame.size.width;
}
}

mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x +
innerScrollFrame.size.width, mainScrollView.bounds.size.height);

[self.view addSubview:mainScrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
return NO;
}

@end

关于iphone - UIScrollview 具有多个图像并捏合缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102119/

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