现在的位置: 首页 > 综合 > 正文

UIScrollView下对图片捏合放大缩小和双击放大缩小

2018年01月23日 ⁄ 综合 ⁄ 共 2824字 ⁄ 字号 评论关闭


1、UIScrollView下图片的捏合放大和缩小,我们直接用scrollView自带的属性就可以了,这个没什么好说,我们直接贴代码:

  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 imageNamed:imageName];  
  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里面设置一下:

  1. #pragma mark -UIScrollView delegate  
  2. -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView  
  3. {  
  4.     return theImage;  
  5. }  

2、scrollView对图片的双击放大和缩小

这里我是通过对双击手势(UIGestureRecognizer)和scrollView的setZoomScale方法来实现放大和缩小。

#创建双击手势

  1. //双击手势  
  2. UITapGestureRecognizer *doubelGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleGesture:)];  
  3. doubelGesture.numberOfTapsRequired=2;  
  4. [theImage addGestureRecognizer:doubelGesture];  
  5. [doubelGesture release];  

#另外,我们要记录当前的倍率,然后通过判断,是放大还是缩小。当然,还配合捏合的放大和缩小,所以,要在scrollView的delegate里面记录当前倍率,代码:

  1. -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale  
  2. {  
  3.     currentScale=scale;  
  4. }  

#双击手势的方法

  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. }  

好了,现在就大功告成了。因为项目中要求的功能比较单一,所以,这个控件整体来说还是比较单一的。有什么不对或者不好的地方,希望大家指出,谢谢。另外,我写了一个Demo,大家尽管拿。PinScrollView

抱歉!评论已关闭.