ViewController.h
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
}
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//创建滚动视图
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
_scrollView.delegate = self;
//设置最大的方法倍数
_scrollView.maximumZoomScale = 2;
//设置最小缩小倍数
_scrollView.minimumZoomScale = .5;
[self.view addSubview:_scrollView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageView.tag = 101;
imageView.userInteractionEnabled = YES;
imageView.image = [UIImage imageNamed:@"0.JPG"];
[_scrollView addSubview:imageView];
//添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
tap.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tap];
}
- (void)tapAction {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
// _scrollView.zoomScale = 2;
// if (_scrollView.zoomScale == 2) {
// _scrollView.zoomScale = 1;
// }else {
// _scrollView.zoomScale = 2;
// }
_scrollView.zoomScale = _scrollView.zoomScale == 2 ? 1:2;
[UIView commitAnimations];
}
#pragma mark - UIScrollView delegate
//用于设置需要放大缩小的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:101];
return imageView;
}
//如果在对视图进行所放的时候会实时调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
//缩放比例
CGFloat scale = scrollView.zoomScale;
NSLog(@"正在缩放,缩放倍数:%f",scale);
}
//将要开始缩放
//- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content
//结束缩放
//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end