iOS用UIScorllView实现两指缩放功能


两指缩放功能不仅可以用UIPinchGestureRecognizer手势来实现,还能用UIScorllView来实现,UIScrollView可以轻松的实现最大与最小缩放值,以及滚动的效果。代码如下:

#import "ViewController.h"
 
@interface ViewController ()
 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
     
    _scrollView.minimumZoomScale = 1.0;     // 最小缩放值
    _scrollView.maximumZoomScale = 10.0;    // 最大缩放值
    [_scrollView setZoomScale:_scrollView.minimumZoomScale];    // 初始时候的缩放值
    _scrollView.delegate = self;
     
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
    _imageView.frame = self.view.frame;
    [_scrollView addSubview:_imageView];
}
 
#pragma mark - 返回需要缩放的控件
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
 
@end

关键点就是调用viewForZoomingInScrollView:代理方法,这里返回了需要缩放的控件。

Demo的GitHub地址

你可能感兴趣的:(iOS用UIScorllView实现两指缩放功能)