UIScrollView结合UIImageView实现图片缩放
程序员文章站
2024-01-19 08:08:58
...
实现UIImageView的图片缩放功能,需要结合UIScrollView实现。
示例代码
// 定义缩放比例
CGFloat scaleMini = 1.0;
CGFloat scaleMax = 3.0;
// 主视图 UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor redColor];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// 图片视图及子视图UIScrollView
//
NSArray *images = @[@"001.jpg", @"002.jpg", @"003.jpg", @"004.jpg"];
//
[images enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIImage *image = [UIImage imageNamed:obj];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// 双击实现缩放
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTapGesture setNumberOfTapsRequired:2];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:doubleTapGesture];
//
UIScrollView *subScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake((idx * scrollView.frame.size.width), 0.0, scrollView.frame.size.width, scrollView.frame.size.height)];
[scrollView addSubview:subScrollerView];
subScrollerView.delegate = self;
[subScrollerView setMinimumZoomScale:scaleMini];
[subScrollerView setMaximumZoomScale:scaleMax];
//
imageView.frame = subScrollerView.bounds;
[subScrollerView addSubview:imageView];
}];
// 根据图片数组重置主视图contentSize
scrollView.contentSize = CGSizeMake((images.count * scrollView.frame.size.width), scrollView.frame.size.height);
// 双击缩放响应
BOOL isScaleBig = NO;
- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
{
UIImageView *imageView = (UIImageView *)recognizer.view;
UIScrollView *scrollView = (UIScrollView *)imageView.superview;
// 放大缩小
CGFloat scale = scrollView.zoomScale;
if (isScaleBig) {
[scrollView setZoomScale:scale / scaleMini animated:YES];
isScaleBig = false;
} else {
[scrollView setZoomScale:scale * scaleMini animated:YES];
isScaleBig = true;
}
}
// 设置协议UIScrollViewDelegate,并实现协议方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
for (UIView *view in scrollView.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
isScaleBig = !isScaleBig;
return view;
}
}
return nil;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
for (UIView *view in scrollView.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
[self showInCenter:scrollView imageView:view];
}
}
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
// 缩放效果 放大或缩小
if (scrollView.minimumZoomScale >= scale) {
[scrollView setZoomScale:scaleMini animated:YES];
}
if (scrollView.maximumZoomScale <= scale) {
[scrollView setZoomScale:scaleMax animated:YES];
}
}
// 缩放时居中显示处理
- (void)showInCenter:(UIScrollView *)scrollView imageView:(UIImageView *)imageView
{
// 居中显示
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;
imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);
}
上一篇: pandas读取csv并绘制散点图