iOS之OC源碼,相冊循環(huán)查看功能的實現


網站建設哪家好,找創(chuàng)新互聯!專注于網頁設計、網站建設、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網站建設等服務項目。為回饋新老客戶創(chuàng)新互聯還提供了玉州免費建站歡迎大家使用!

#import"ViewController.h"

#import"YZUIScrollView.h"

#define kuan ([UIScreen mainScreen].bounds.size.width+20)

#define gao [UIScreen mainScreen].bounds.size.height

@interfaceViewController ()<UIScrollViewDelegate>

@property (weak,nonatomic)IBOutletUIScrollView *huaBu;

@property(nonatomic,strong)NSArray *images;

@property(nonatomic)NSInteger currentIndex;

@end

@implementation ViewController

//懶加載,調用get方法時對屬性進行初始化

-(NSArray *)images

{

   if(_images==nil)

    {

       NSMutableArray *imagearray=[NSMutableArrayarray];

       for (int i=1; i<7; i++) {

           NSString *imageName=[NSStringstringWithFormat:@"new_feature_%d",i];

           UIImage *image=[UIImageimageNamed:imageName];

            [imagearrayaddObject:image];

        }

       _images=imagearray;

    }

   return_images;

}

- (void)viewDidLoad {

    [superviewDidLoad];

   //設置UIScrollView的contentSize

   _huaBu.contentSize=CGSizeMake(kuan*3,gao);

   //設置分頁

   _huaBu.pagingEnabled=YES;

   //隱藏水平滾動欄和垂直滾動欄

   _huaBu.showsHorizontalScrollIndicator=NO;

   _huaBu.showsVerticalScrollIndicator=NO;

   //設置背景顏色,突出不同的圖片

   _huaBu.backgroundColor=[UIColorblackColor];

   //設置代理要遵守協議<UIScrollViewDelegate>

   _huaBu.delegate=self;

   //調用方法添加子視圖

    [selftianJiaZiShiTu];

   //調用方法添加圖片

    [selftianJiaTuPian];

}

//添加子視圖的方法

-(void)tianJiaZiShiTu

{

   //相冊的話,是一個大的UIScrollView中放了很多的小UIScrollView,但是為了節(jié)省內存空間,所以只是添加了三個UIScrollView(圖片不停的變換位置)

   for (int i=0; i<3; i++) {

       //創(chuàng)建YZUIScrollView

       YZUIScrollView * yzuisv=[[YZUIScrollViewalloc]initWithFrame:CGRectMake(kuan*i, 0,kuan-20,gao)];

       //添加YZUIScrollView

        [_huaBuaddSubview:yzuisv];

       //設置tag值,便于區(qū)分

        yzuisv.tag=1000+i;

    }

}

//添加方法的圖片

-(void)tianJiaTuPian

{

   YZUIScrollView *leftSC=(YZUIScrollView *)[_huaBuviewWithTag:1000];

   YZUIScrollView *middleSC=(YZUIScrollView *)[_huaBuviewWithTag:1001];

   YZUIScrollView *rightSC=(YZUIScrollView *)[_huaBuviewWithTag:1002];

    leftSC.image=self.images[[selfindexFofEnable:_currentIndex-1]];

    middleSC.image=self.images[[selfindexFofEnable:_currentIndex]];

    rightSC.image=self.images[[selfindexFofEnable:_currentIndex+1]];

   //設置偏移量,這步很重要

   _huaBu.contentOffset=CGPointMake(kuan, 0);

}

//確保索引可用

-(NSInteger)indexFofEnable:(NSInteger)index

{

   if(index<0)

    {

       returnself.images.count-1;

    }

   elseif (index>self.images.count-1)

    {

       return 0;

    }

   else

       return index;

}

//滾動結束后,把所有的縮放視圖比例還原為1.0

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

   for (id objin_huaBu.subviews) {

       if([objisKindOfClass:[UIScrollViewclass]])

        {

           UIScrollView *scaleSC=(UIScrollView *)obj;

            scaleSC.zoomScale=1.0;

        }

    }

   //判斷左右滑動

   //偏移量的x為0,就是說明向右滑動了,就是看的之前左邊的那張圖片

   if(scrollView.contentOffset.x==0)

    {

       //對應的圖像應該是變成左邊的圖像

       _currentIndex--;

    }

   //偏移量的x為兩個屏幕的寬,就是說明向左滑動了,就是看的之前右邊的那張圖片

   elseif(scrollView.contentOffset.x==kuan*2)

    {

       //對應的圖像應該是變成右邊的圖像

       _currentIndex++;

    }

   _currentIndex=[selfindexFofEnable:_currentIndex];

    [selftianJiaTuPian];

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

@end

//第二個類

#import"YZUIScrollView.h"

@interfaceYZUIScrollView ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIImageView *imageview;

@property(nonatomic,strong)UIImage *image;//內容視圖的圖片

@end

@implementation YZUIScrollView

-(instancetype)initWithFrame:(CGRect)frame

{

   if(self =[superinitWithFrame:frame])

    {

       //添加內容視圖

       UIImageView *imageview1=[[UIImageViewalloc]initWithFrame:self.bounds];

        [selfaddSubview:imageview1];

       _imageview=imageview1;

       //設置最大最小倍數和代理

       self.minimumZoomScale=0.5;

       self.maximumZoomScale=1.5;

       self.delegate=self;

       //雙擊事件

       UITapGestureRecognizer *tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(shuangJi:)];

        tap.numberOfTapsRequired=2;

        [selfaddGestureRecognizer:tap];

    }

   returnself;

}

-(void)shuangJi:(UITapGestureRecognizer *)tap

{

   //當縮放比例不為1.0,還原縮放比例

   if (self.zoomScale !=1.0) {

        [selfsetZoomScale:1.0animated:YES];

       return ;

    }

   CGPoint location =[taplocationInView:self];

   CGRect rect =CGRectMake(location.x-100, location.y-100,200,200);

    [selfzoomToRect:rectanimated:YES];

}

//重寫setImg方法

-(void)setImage:(UIImage *)image

{

   //set本身的方法要完成的事必須完成

   _image=image;

   //設置內容視圖的圖片

   _imageview.image=image;

}

//UIScrollViewDelegate代理方法

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

   return_imageview;

}

@end

網頁名稱:iOS之OC源碼,相冊循環(huán)查看功能的實現
當前鏈接:http://muchs.cn/article18/pieedp.html

成都網站建設公司_創(chuàng)新互聯,為您提供響應式網站、域名注冊、企業(yè)建站、品牌網站建設、品牌網站設計、定制開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

微信小程序開發(fā)