链接
https://www.cnblogs.com/wendingding/p/3754268.html
1.
if (maxScale > 1) {
//设置缩放比例
scr.maximumZoomScale = maxScale;
//设置scrollView的代理为当前控制器
scr.delegate = self;
}
2.当前控制器成为代理
@interface MYSeeBigPictureViewController () <UIScrollViewDelegate>
3.真正实现代理的方法
#pragma mark - UIScrollViewDelegate
//Asks the delegate for the view to scale when zooming is about to occur in the scroll view.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
查看大图控制器
//
// MYSeeBigPictureViewController.m
// 百思不得姐
//
// Created by 吴飞 on 2018/6/30.
// Copyright © 2018年 吴飞. All rights reserved.
//
#import "MYSeeBigPictureViewController.h"
//分类
#import "UIView+frame.h"
//第三方
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>
//宏
#define MYScreenH [UIScreen mainScreen].bounds.size.height
#define MYScreenW [UIScreen mainScreen].bounds.size.width
@interface MYSeeBigPictureViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property(weak,nonatomic) UIImageView *imageView;
@end
@implementation MYSeeBigPictureViewController
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(UIButton *)sender {
}
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scr = [[UIScrollView alloc] init];
scr.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scr.frame = self.view.bounds;
//给UIScrollView增加手势,
[scr addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back:)]];
//这个地方不能使用addSubView 因为当前控制的view是先加载xib完成后再插入的UIScrollView 控件
// [self.view addSubview:scr];
//所以要使用insert
[self.view insertSubview:scr atIndex:0];
//添加UIImageView 到scrollView
UIImageView *imageView = [[UIImageView alloc ] init];
self.imageView = imageView;
//当图片下载成功后 按照按钮可用
[imageView sd_setImageWithURL:[NSURL URLWithString:self.topic.image1] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (!image){
[SVProgressHUD showErrorWithStatus:@"图片下载失败"];
return ;
}
self.saveButton.enabled = YES;
}];
//宽度 = scrollView的宽度
imageView.MY_Width = scr.MY_Width;
//高度 = 按照服务器返回的图片宽高进行缩放
CGFloat h = (scr.MY_Width * self.topic.height) / self.topic.width;
imageView.MY_Height = h;
imageView.MY_x = 0;
if (imageView.MY_Height > MYScreenH) {//图片高度超过一个屏幕 得按照图片真实的高度 增加滚动操作
imageView.MY_y = 0;
//scrollView的设置contentSize
scr.contentSize = CGSizeMake(0, imageView.MY_Height);
}else{
imageView.MY_center = CGPointMake(MYScreenW * 0.5, scr.MY_Height * 0.5);
}
[scr addSubview:imageView];
//图片缩放
CGFloat maxScale = self.topic.width / imageView.MY_Width ;//图片的最大缩放比例
if (maxScale > 1) {
scr.maximumZoomScale = maxScale;
scr.delegate = self;
}
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
#pragma mark - 这个方法会调用多次(只要布局有变化,就会调用)
- (void)viewDidLayoutSubviews {}
- (void)viewDidAppear:(BOOL)animated{}
@end
网友评论