iOS圖片瀏覽器控制項 放大,縮小,UIScrollView

來源:互聯網
上載者:User

iOS圖片瀏覽器控制項 放大,縮小,UIScrollView
圖片瀏覽器主要通過 UIScrollView 實現  在一個大的ScollView裡面套 n個ScollView UIScrollView裡主要是有兩個屬性,contentSize和contentoffset , contentSize 是設定UIScrollView的可互動的大小,contentoffset位移量  

設定第一層 UIScollView 主要是設定 contentSize 和 contentoffset , contentSize 是根據圖片的個數來設定,contentoffset根據當前圖片編號設定    wholeScoll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, myScreenWidth, myScreenHeight)];    wholeScoll.contentOffset = CGPointMake(wholeScoll.frame.size.width*mcurpage, 0);   wholeScoll.contentSize = CGSizeMake(myScreenWidth*mImgLocationArr.count, myScreenHeight);        // 主要代碼是通過for 迴圈添加第二層UIScrollView      for (int i = 0; i<mImgLocationArr.count; i++) {        //設定 imageview          UIImageView * imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, myScreenWidth, myScreenHeight)];          imgview.contentMode = UIViewContentModeScaleAspectFit;        //設定 scrollview          UIScrollView * singleview = [[UIScrollView alloc]initWithFrame:CGRectMake(myScreenWidth*i,0,myScreenWidth, myScreenHeight)];          [wholeScoll addSubview:singleview];          [singleview addSubview:imgview];        // 添加手勢          [self addGestureRecognizer];    } 

 

   圖片瀏覽器中圖片可以左右滑動,可以雙擊放大,可以手勢捏合放大縮小 圖片瀏覽器中主要有三種手勢分別是單擊 singleTap 雙擊doubleTap 捏合 pinchGesture 和UIScrollView內建的滑動手勢 singleTap 手勢主要是返回上一頁面,如果觸發singleTap的時候圖片已經放大,那麼先將圖片初始化大小和座標 因為singleTap 和 doubleTap 是屬於同一手勢類,會產生手勢衝突,所以需要  [singleTap requireGestureRecognizerToFail:doubleTap];在執行doubleTap的時候登出掉singleTap   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~然後高潮來了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我是可愛的分割線~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   最主要的操作是 doubleTap 和pinchGesture,這也是花了我最多時間的兩個問題 首先講一下doubleTap的思想,大家注意觀察的圖片瀏覽器,doubleTap在執行的時候會得到一個在View的點   CGPoint tapPoint = [doubletap locationInView:doubletap.view]; 然後雙擊後,放大UIImageView,然後通過設定contentoffset位移量將 tapPoint 移到螢幕最中間,這是雙擊的點包含在圖片裡的情況,另外一種情況,是雙擊的點在圖片外,然後點在圖片外又有 左上,右上,左下,右下四種情況。分別將圖片放大然後,圖片左上,右上,左下,右下四個方向貼邊顯示 然後根據tapPoint的x,y判斷四種不同的情況     
tapX,tapY是tapPoint的x,y  imgY是圖片的y值,imgHeight是圖片的Heightif (tapY<imgY) {        //上面        pointType = XJAlbumOutImgViewPointLeftUp;        if (tapX>myScreenWidth/2) {            pointType = XJAlbumOutImgViewPointRightUp;        }        isOutImgView = YES;    }    else if(tapY>imgY+imgHeight)    {        //下面        pointType = XJAlbumOutImgViewPointLeftDown;        if (tapX>myScreenWidth/2) {            pointType = XJAlbumOutImgViewPointRightDown;        }        isOutImgView = YES;    }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.