#import "ViewController.h" #define kuan [UIScreen mainScreen].bounds.size.width #define gao [UIScreen mainScreen].bounds.size.height @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *huaKuang; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //設置contentSize _huaKuang.contentSize=CGSizeMake((kuan)*4, gao); _huaKuang.backgroundColor=[UIColor blackColor]; //設置分頁 _huaKuang.pagingEnabled=YES; //隱藏滾動條 //滾動時是否顯示水平滾動條 _huaKuang.showsHorizontalScrollIndicator=NO; //滾動時是否顯示垂直滾動條 _huaKuang.showsVerticalScrollIndicator=NO; //設置代理,需要遵循代理協議<UIScrollViewDelegate>,寫在@interface ViewController ()的後面 _huaKuang.delegate=self; //添加子視圖,因為是多個所以寫了一個方法來實現添加 [self tianJianZiShiTu]; } -(void)tianJianZiShiTu { //假如有六個圖片,就要創建六個UIScrollView和六個UIImageView,並且找到六個圖片 for(int i=0;i<3;i++) { //創建UIScrollView //為了區分開不同的照片加一個20黑邊,需要設置ScrollViewscroll View中的Left和View中的Width UIScrollView *uisv=[[UIScrollView alloc] initWithFrame:CGRectMake((kuan+20)*i, 0, kuan, gao)]; //把創建完成的添加到總的那個UIScrollView上 [_huaKuang addSubview:uisv]; //創建UIImageView UIImageView *uiiv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kuan, gao)]; //把創建的UIImageView添加到UIScrollView中 [uisv addSubview:uiiv]; //設置UIImageView的圖片 NSString *imageName = [NSString stringWithFormat:@"new_feature_%d",i + 1]; uiiv.image=[UIImage imageNamed:imageName]; //設置tag值 uiiv.tag=1000; //設置UIScrollView的代理 uisv.delegate=self; //設置縮放范圍 uisv.minimumZoomScale=0.5; uisv.maximumZoomScale=1.5; //定義點擊事件 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianJiShiJian:)]; //設置有效點擊數(就是雙擊) tap.numberOfTapsRequired=2; //添加到UIScrollView中 [uisv addGestureRecognizer:tap]; } } -(void)dianJiShiJian:(UITapGestureRecognizer *)tap { //獲取點擊事件的view UIScrollView *uisv1=(UIScrollView *)tap.view; if(uisv1.zoomScale!=1.0) { [uisv1 setZoomScale:1.0 animated:YES]; return ; } CGPoint location=[tap locationInView:tap.view]; CGRect rect=CGRectMake(location.x-100, location.y-100, 200, 200); [uisv1 zoomToRect:rect animated:YES]; } //指定縮放的視圖 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ if (scrollView == _huaKuang) { return nil; } UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:1000]; return imageView; } //滾動結束,把所有的縮放視圖的縮放比例置為1.0 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (scrollView == _huaKuang) { for (id obj in _huaKuang.subviews) { if ([obj isKindOfClass:[UIScrollView class]]) { UIScrollView *scaleSC = (UIScrollView *)obj; scaleSC.zoomScale = 1.0; } } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end