欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

scrollView浏览图片并缩放

程序员文章站 2022-03-08 22:29:46
一、应用场景 类似微信发朋友圈添加图片(相册或拍照),点击图片可以浏览,浏览时可对当前图片进行缩放,并且该缩放不影响其他图片正常显示;浏览时可删除图片 二、关键代码 //这里主要的问题是:如果采用CGAffineTransformScale对scrollview进行缩放,则浏览时移到下一张图片也会跟 ......

一、应用场景

类似微信发朋友圈添加图片(相册或拍照),点击图片可以浏览,浏览时可对当前图片进行缩放,并且该缩放不影响其他图片正常显示;浏览时可删除图片

二、关键代码

//这里主要的问题是:如果采用cgaffinetransformscale对scrollview进行缩放,则浏览时移到下一张图片也会跟着缩放;如果捏合手势添加到scrollview的imageview中,同样采用cgaffinetransformscale对该图片缩放(pinges.view.transform = cgaffinetransformscale(self.scrollview.transform, newscale, newscale);)并没有效果;

//因此,采用的策略是:在scrollview中嵌套scrollview,然后再嵌套imageview,并且采用zoomtorect缩放,而非cgaffinetransformscale

- (void)culayoutsubviews
{
    bl_loadnavheight;
    self.scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 0, screenwidth_n(), screenheight_n())];
    self.scrollview.backgroundcolor = [uicolor blackcolor];
    [self.view addsubview:self.scrollview];
    
    self.scrollview.delegate = self;
    self.scrollview.pagingenabled = yes;
    self.scrollview.scrollenabled = yes;
    self.scrollview.showshorizontalscrollindicator = no;
    
    [self.imagearr enumerateobjectsusingblock:^(id  _nonnull obj, nsuinteger idx, bool * _nonnull stop) {
        
        uiscrollview *subscrollview = [[uiscrollview alloc] initwithframe:cgrectmake(idx*screenwidth_n(), navviewh, screenwidth_n(), screenheight_n()-navviewh)];
        subscrollview.backgroundcolor = [uicolor blackcolor];
        subscrollview.contentsize = cgsizemake(screenwidth_n(), screenheight_n()-navviewh);
        subscrollview.delegate = self;
        subscrollview.minimumzoomscale = self.minscale;
        subscrollview.maximumzoomscale = self.maxscale;
        [subscrollview setzoomscale:1.0];
        
        uiimage *img = (uiimage *)obj;
        uiimageview *imgview = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, screenwidth_n(), screenheight_n()-navviewh)];
        imgview.userinteractionenabled = yes;
        imgview.contentmode = uiviewcontentmodescaleaspectfit;
        imgview.image = img;
        
        uipinchgesturerecognizer *pin = [[uipinchgesturerecognizer alloc] initwithtarget:self action:@selector(pingesclick:)];
        //捏合手势
        [imgview addgesturerecognizer:pin];
        [subscrollview addsubview:imgview];
        [self.scrollview addsubview:subscrollview];
        
        [self.subscrollviewarr addobject:subscrollview];
        [self.imgviewarr addobject:imgview];
        
    }];
    
    [self adjustscrollviewoffset];
    
    //    uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(navhidden)];
    //    [self.scrollview addgesturerecognizer:tap];
}

 

- (void)pingesclick:(uipinchgesturerecognizer *)pinges
{
//    switch (pinges.state) {
//        case uigesturerecognizerstatebegan://scale began
//        case uigesturerecognizerstatechanged://scale changed
//        {
//            cgfloat currentscale = [[self.scrollview.layer valueforkeypath:@"transform.scale"] floatvalue];
//            cgfloat newscale = pinges.scale - self.lastscale + 1;
//
//            newscale = min(newscale, self.maxscale / currentscale);
//            newscale = max(newscale, self.minscale / currentscale);
//
//            self.scrollview.transform = cgaffinetransformscale(self.scrollview.transform, newscale, newscale);
//
//            for (int i = 0; i < self.imgviewarr.count; i++) {
//                uiimageview *subimgview = self.imgviewarr[i];
//                nslog(@"subimgview----%d---%@", i, subimgview);
//            }
//
//            self.lastscale = pinges.scale;
//
//
//        }
//            break;
//        case uigesturerecognizerstateended://scale ended
//            self.lastscale = 1;
//            break;
//        default:
//            break;
//    }

    float newscale = [(uiscrollview*)pinges.view.superview zoomscale];
    cgrect zoomrect = [self zoomrectforscale:newscale  inview:(uiscrollview*)pinges.view.superview withcenter:[pinges locationinview:pinges.view]];
    [(uiscrollview*)pinges.view.superview zoomtorect:zoomrect animated:yes];
}

 

//必须返回当前缩放视图——实现代理方法

//放回当前缩放图片
- (uiview *)viewforzoominginscrollview:(uiscrollview *)scrollview
{
    for (uiview *v in scrollview.subviews){
        return v;
    }
    return nil;
}

 

三、效果

scrollView浏览图片并缩放

scrollView浏览图片并缩放

scrollView浏览图片并缩放

 

github