iOS源码之OC相册,可以循环查看图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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];
    //设置UIScrollView的contentSize
    _huaBu.contentSize=CGSizeMake(kuan*3, gao);
    //设置分页
    _huaBu.pagingEnabled=YES;
    //隐藏水平滚动栏和垂直滚动栏
    _huaBu.showsHorizontalScrollIndicator=NO;
    _huaBu.showsVerticalScrollIndicator=NO;
    //设置背景颜色,突出不同的图片
    _huaBu.backgroundColor=[UIColor blackColor];
    //设置代理要遵守协议<UIScrollViewDelegate>
    _huaBu.delegate=self;
    //调用方法添加子视图
    [self tianJiaZiShiTu];
    //调用方法添加图片
    [self tianJiaTuPian];
}
//添加子视图的方法
-(void)tianJiaZiShiTu
{
    //相册的话,是一个大的UIScrollView中放了很多的小UIScrollView,但是为了节省内存空间,所以只是添加了三个UIScrollView(图片不停的变换位置)
    for (int i=0; i<3; i++) {
        //创建YZUIScrollView
        YZUIScrollView * yzuisv=[[YZUIScrollView alloc] initWithFrame:CGRectMake(kuan*i, 0, kuan-20, gao)];
        //添加YZUIScrollView
        [_huaBu addSubview:yzuisv];
        //设置tag值,便于区分
        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;
        }
    }
    //判断左右滑动
    //偏移量的x为0,就是说明向右滑动了,就是看的之前左边的那张图片
    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

  第二个类

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "YZUIScrollView.h"
 
 
@interface YZUIScrollView ()<UIScrollViewDelegate>@property(nonatomic,strong)UIImage *image;//内容视图的图片@property(nonatomic,strong)UIImageView *imageview;
@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

  

 

posted @   会装系统的程序员  阅读(346)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示