溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-06-24 01:01:05 來源:網絡 閱讀:709 作者:591XT_XYZ 欄目:移動開發(fā)


#import "ViewController.h"

#import "YZUIScrollView.h"

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

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

@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *huaBu;

@property(nonatomic,strong)NSArray *images;

@property(nonatomic)NSInteger currentIndex;

@end


@implementation ViewController

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

-(NSArray *)images

{

    if(_images==nil)

    {

        NSMutableArray *imagearray=[NSMutableArray array];

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

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

            UIImage *image=[UIImage imageNamed:imageName];

            [imagearray addObject:image];

        }

        _images=imagearray;

    }

    return _images;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //設置UIScrollViewcontentSize

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

    //設置分頁

    _huaBu.pagingEnabled=YES;

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

    _huaBu.showsHorizontalScrollIndicator=NO;

    _huaBu.showsVerticalScrollIndicator=NO;

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

    _huaBu.backgroundColor=[UIColor blackColor];

    //設置代理要遵守協(xié)議<UIScrollViewDelegate>

    _huaBu.delegate=self;

    //調用方法添加子視圖

    [self tianJiaZiShiTu];

    //調用方法添加圖片

    [self tianJiaTuPian];

}

//添加子視圖的方法

-(void)tianJiaZiShiTu

{

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

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

        //創(chuàng)建YZUIScrollView

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

        //添加YZUIScrollView

        [_huaBu addSubview:yzuisv];

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

        yzuisv.tag=1000+i;

    }

}

//添加方法的圖片

-(void)tianJiaTuPian

{

    YZUIScrollView *leftSC=(YZUIScrollView *)[_huaBu viewWithTag:1000];

    YZUIScrollView *middleSC=(YZUIScrollView *)[_huaBu viewWithTag:1001];

    YZUIScrollView *rightSC=(YZUIScrollView *)[_huaBu viewWithTag:1002];

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

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

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

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

    _huaBu.contentOffset=CGPointMake(kuan, 0);

}

//確保索引可用

-(NSInteger)indexFofEnable:(NSInteger)index

{

    if(index<0)

    {

        return self.images.count-1;

    }

    else if (index>self.images.count-1)

    {

        return 0;

    }

    else

        return index;

}

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

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    for (id obj in _huaBu.subviews) {

        if([obj isKindOfClass:[UIScrollView class]])

        {

            UIScrollView *scaleSC=(UIScrollView *)obj;

            scaleSC.zoomScale=1.0;

        }

    }

    //判斷左右滑動

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

    if(scrollView.contentOffset.x==0)

    {

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

        _currentIndex--;

    }

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

    else if(scrollView.contentOffset.x== kuan*2)

    {

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

        _currentIndex++;

    }

    _currentIndex=[self indexFofEnable:_currentIndex];

    [self tianJiaTuPian];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

//第二個類

#import "YZUIScrollView.h"



@interface YZUIScrollView ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIImageView *imageview;

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

@end


@implementation YZUIScrollView


-(instancetype)initWithFrame:(CGRect)frame

{

    if(self =[super initWithFrame:frame])

    {

        //添加內容視圖

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

        [self addSubview:imageview1];

        _imageview=imageview1;

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

        self.minimumZoomScale=0.5;

        self.maximumZoomScale=1.5;

        self.delegate=self;

        //雙擊事件

        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shuangJi:)];

        tap.numberOfTapsRequired=2;

        [self addGestureRecognizer:tap];

    }

    return self;

}

-(void)shuangJi:(UITapGestureRecognizer *)tap

{

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

    if (self.zoomScale !=1.0) {

        [self setZoomScale:1.0 animated:YES];

        return ;

    }

    CGPoint location =[tap locationInView:self];

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

    [self zoomToRect:rect animated:YES];

}

//重寫setImg方法

-(void)setImage:(UIImage *)image

{

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

    _image=image;

    //設置內容視圖的圖片

    _imageview.image=image;

}

//UIScrollViewDelegate代理方法

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

{

    return _imageview;

}

@end


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI