UIScrollView下對圖片捏合放大縮小和雙擊放大縮小

原文地址  

http://blog.csdn.net/u010424349/article/details/9139183


馬上就要開項目了,大哥讓我寫個控件出來,到時候可以直接用。

好了閒話不多說,直接說思路。


1、UIScrollView下圖片的捏合放大和縮小,我們直接用scrollView自帶的屬性就可以了,這個沒什麼好說,我們直接貼代碼:

[plain] view plaincopy

  1. //控制器  

  2.  theScroll=[[UIScrollView alloc] initWithFrame:frame];  

  3.  theScroll.userInteractionEnabled=YES;  

  4.  theScroll.maximumZoomScale=2.0;//最大倍率(默認倍率)  

  5.  theScroll.minimumZoomScale=1.0;//最小倍率(默認倍率)  

  6.  theScroll.decelerationRate=1.0;//減速倍率(默認倍率)  

  7.  theScroll.delegate=self;  

  8.  theScroll.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight;  

  9.  [self addSubview:theScroll];  

  10.    

  11.  //圖片  

  12.  UIImage *theImageName=[UIImage p_w_picpathNamed:p_w_picpathName];  

  13.  theImage=[[UIImageView alloc] initWithImage:theImageName];  

  14.  theImage.userInteractionEnabled=YES;  

  15.  theImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight;  

  16.  //圖片默認作爲2倍圖處理  

  17.  theImage.frame=CGRectMake(0, 0, theImageName.size.width/2, theImageName.size.height/2);  

  18.  [theScroll addSubview:theImage];  



另外,我們還要在scrollView的delegate裏面設置一下:

[plain] view plaincopy

  1. #pragma mark -UIScrollView delegate  

  2. -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView  

  3. {  

  4.     return theImage;  

  5. }  



2、scrollView對圖片的雙擊放大和縮小

這裏我是通過對雙擊手勢(UIGestureRecognizer)和scrollView的setZoomScale方法來實現放大和縮小。

#創建雙擊手勢

[plain] view plaincopy

  1. //雙擊手勢  

  2. UITapGestureRecognizer *doubelGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleGesture:)];  

  3. doubelGesture.numberOfTapsRequired=2;  

  4. [theImage addGestureRecognizer:doubelGesture];  

  5. [doubelGesture release];  


#另外,我們要記錄當前的倍率,然後通過判斷,是放大還是縮小。當然,還配合捏合的放大和縮小,所以,要在scrollView的delegate裏面記錄當前倍率,代碼:

[plain] view plaincopy

  1. -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale  

  2. {  

  3.     currentScale=scale;  

  4. }  


#雙擊手勢的方法


[plain] view plaincopy

  1. #pragma mark -DoubleGesture Action  

  2. -(void)doubleGesture:(UIGestureRecognizer *)sender  

  3. {  

  4.       

  5.     //當前倍數等於最大放大倍數  

  6.     //雙擊默認爲縮小到原圖  

  7.     if (currentScale==_maxScale) {  

  8.         currentScale=minScale;  

  9.         [theScroll setZoomScale:currentScale animated:YES];  

  10.         return;  

  11.     }  

  12.     //當前等於最小放大倍數  

  13.     //雙擊默認爲放大到最大倍數  

  14.     if (currentScale==minScale) {  

  15.         currentScale=_maxScale;  

  16.         [theScroll setZoomScale:currentScale animated:YES];  

  17.         return;  

  18.     }  

  19.       

  20.     CGFloat aveScale =minScale+(_maxScale-minScale)/2.0;//中間倍數  

  21.       

  22.     //當前倍數大於平均倍數  

  23.     //雙擊默認爲放大最大倍數  

  24.     if (currentScale>=aveScale) {  

  25.         currentScale=_maxScale;  

  26.         [theScroll setZoomScale:currentScale animated:YES];  

  27.         return;  

  28.     }  

  29.       

  30.     //當前倍數小於平均倍數  

  31.     //雙擊默認爲放大到最小倍數  

  32.     if (currentScale<aveScale) {  

  33.         currentScale=minScale;  

  34.         [theScroll setZoomScale:currentScale animated:YES];  

  35.         return;  

  36.     }  

  37. }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章