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

马上就要开项目了,大哥让我写个控件出来,到时候可以直接用。

好了闲话不多说,直接说思路。


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

       //控制器
        theScroll=[[UIScrollView alloc] initWithFrame:frame];
        theScroll.userInteractionEnabled=YES;
        theScroll.maximumZoomScale=2.0;//最大倍率(默认倍率)
        theScroll.minimumZoomScale=1.0;//最小倍率(默认倍率)
        theScroll.decelerationRate=1.0;//减速倍率(默认倍率)
        theScroll.delegate=self;
        theScroll.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight;
        [self addSubview:theScroll];
        
        //图片
        UIImage *theImageName=[UIImage imageNamed:imageName];
        theImage=[[UIImageView alloc] initWithImage:theImageName];
        theImage.userInteractionEnabled=YES;
        theImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight;
        //图片默认作为2倍图处理
        theImage.frame=CGRectMake(0, 0, theImageName.size.width/2, theImageName.size.height/2);
        [theScroll addSubview:theImage];

另外,我们还要在scrollView的delegate里面设置一下:

#pragma mark -UIScrollView delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return theImage;
}

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

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

#创建双击手势

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

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

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

#双击手势的方法

#pragma mark -DoubleGesture Action
-(void)doubleGesture:(UIGestureRecognizer *)sender
{
    
    //当前倍数等于最大放大倍数
    //双击默认为缩小到原图
    if (currentScale==_maxScale) {
        currentScale=minScale;
        [theScroll setZoomScale:currentScale animated:YES];
        return;
    }
    //当前等于最小放大倍数
    //双击默认为放大到最大倍数
    if (currentScale==minScale) {
        currentScale=_maxScale;
        [theScroll setZoomScale:currentScale animated:YES];
        return;
    }
    
    CGFloat aveScale =minScale+(_maxScale-minScale)/2.0;//中间倍数
    
    //当前倍数大于平均倍数
    //双击默认为放大最大倍数
    if (currentScale>=aveScale) {
        currentScale=_maxScale;
        [theScroll setZoomScale:currentScale animated:YES];
        return;
    }
    
    //当前倍数小于平均倍数
    //双击默认为放大到最小倍数
    if (currentScale<aveScale) {
        currentScale=minScale;
        [theScroll setZoomScale:currentScale animated:YES];
        return;
    }
}

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

另外,我写了一个Demo,大家尽管拿。PinScrollView



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值