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