服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现带有缩放效果的自动轮播图

iOS实现带有缩放效果的自动轮播图

2021-04-25 18:37NB_Token IOS

这篇文章主要为大家详细介绍了iOS带有缩放效果的自动轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ios带有缩放效果的自动轮播图,供大家参考,具体内容如下

可直接设置frame然后加载到视图上使用。

效果就是这样的,图片切换的过程中还是有卡顿,不够流畅,后续更新。

iOS实现带有缩放效果的自动轮播图

直接上代码。

.h文件包含:

?
1
2
3
4
5
6
#import <uikit/uikit.h>
 
@interface customscrollview : uiview
@property (strong,nonatomic) nsarray *imagearr;
 
@end

.m文件包含:

?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#import "customscrollview.h"
 
@interface customscrollview ()<uiscrollviewdelegate>
 
@property (strong,nonatomic) uiscrollview *scrollview;
@property (strong,nonatomic) uiimageview *backimageview;
 
@property (strong,nonatomic) uiimageview *leftiamgeview;
@property (strong,nonatomic) uiimageview *middleimageview;
@property (strong,nonatomic) uiimageview *rightimageview;
 
@property (strong,nonatomic) uipagecontrol *pagecontrol;
 
//高度
@property (assign,nonatomic) cgfloat scrollviewheight;
 
//手动造成的偏移量
@property (assign,nonatomic) cgfloat offsetx;
 
@property (strong,nonatomic) nstimer *timer;
 
//属于计时器方法动画持续时间 ?
@property (assign,nonatomic) bool timeranimation;
 
@end
 
//左右内容的最大偏移量
#define offset_max   ([uiscreen mainscreen].bounds.size.width-64)*0.9
 
 
@implementation customscrollview
 
- (instancetype)initwithframe:(cgrect)frame {
 if (self = [super initwithframe:frame]) {
 
  self.scrollviewheight = frame.size.height;
   
  [self addsubview:self.backimageview];
  [self addsubview:self.scrollview];
  [self addsubview:self.pagecontrol];
   
  [self.backimageview mas_makeconstraints:^(masconstraintmaker *make) {
   make.edges.with.mas_equalto(self);
  }];
  [self.scrollview mas_makeconstraints:^(masconstraintmaker *make) {
   make.edges.with.mas_equalto(self);
  }];
  [self.pagecontrol mas_makeconstraints:^(masconstraintmaker *make) {
   make.left.right.bottom.mas_equalto(self);
   make.height.mas_equalto(@20);
  }];
   
  self.timer = [nstimer scheduledtimerwithtimeinterval:5 target:self selector:@selector(timeraction) userinfo:nil repeats:yes];
  [[nsrunloop mainrunloop] addtimer:self.timer formode:nsdefaultrunloopmode];
  [self.timer setfiredate:[nsdate datewithtimeintervalsincenow:5]];
 }
 return self;
}
 
- (nsarray *)imagearr {
 if (!_imagearr) {
  _imagearr = @[@"timg-0",@"timg-1",@"timg-2"];
 }
 return _imagearr;
}
 
- (uiimageview *)backimageview {
 if (!_backimageview) {
  _backimageview = [[uiimageview alloc] init];
  _backimageview.image = [uiimage imagenamed:@"bg_home"];
 }
 return _backimageview;
}
 
- (uiscrollview *)scrollview {
 if (!_scrollview) {
  _scrollview = [[uiscrollview alloc] init];
  _scrollview.showsverticalscrollindicator = no;
  _scrollview.showshorizontalscrollindicator = no;
  _scrollview.contentsize = cgsizemake(kscreenwidth+2*offset_max, self.scrollviewheight);
  [_scrollview setcontentoffset:cgpointmake(offset_max, 0)];
  _scrollview.bounces = no;
  _scrollview.delegate = self;
   
  _leftiamgeview = [[uiimageview alloc] init];
  _leftiamgeview.image = [uiimage imagenamed:self.imagearr[0]];
  _leftiamgeview.layer.cornerradius = 5;
  _leftiamgeview.layer.maskstobounds = yes;
 
   
  _middleimageview = [[uiimageview alloc] init];
  _middleimageview.image = [uiimage imagenamed:self.imagearr[1]];
  _middleimageview.layer.cornerradius = 5;
  _middleimageview.layer.maskstobounds = yes;
 
   
  _rightimageview = [[uiimageview alloc] init];
  _rightimageview.image = [uiimage imagenamed:self.imagearr[2]];
  _rightimageview.layer.cornerradius = 5;
  _rightimageview.layer.maskstobounds = yes;
   
   
  [_scrollview addsubview:_leftiamgeview];
  [_scrollview addsubview:_middleimageview];
  [_scrollview addsubview:_rightimageview];
  [_leftiamgeview mas_makeconstraints:^(masconstraintmaker *make) {
   make.size.mas_equalto(cgsizemake((kscreenwidth-64)*0.9, self.scrollviewheight*0.9));
   make.centery.mas_equalto(_scrollview);
   make.right.mas_equalto(_middleimageview.mas_left).offset(-20);
  }];
  [_middleimageview mas_makeconstraints:^(masconstraintmaker *make) {
   make.size.mas_equalto(cgsizemake(kscreenwidth-64, self.scrollviewheight));
   make.centery.mas_equalto(_scrollview);
   make.left.mas_equalto(_scrollview).offset(offset_max+64/2);
  }];
  [_rightimageview mas_makeconstraints:^(masconstraintmaker *make) {
   make.size.mas_equalto(_leftiamgeview);
   make.centery.mas_equalto(_scrollview);
   make.left.mas_equalto(_middleimageview.mas_right).offset(20);
  }];
 }
 return _scrollview;
}
 
- (uipagecontrol *)pagecontrol {
 if (!_pagecontrol) {
  _pagecontrol = [[uipagecontrol alloc] init];
  _pagecontrol.numberofpages = self.imagearr.count;
  _pagecontrol.enabled = no;
  _pagecontrol.currentpage = 0;
  _pagecontrol.hidesforsinglepage = yes;
  _pagecontrol.pageindicatortintcolor = [uicolor whitecolor];
  _pagecontrol.currentpageindicatortintcolor = maincolor;
 }
 return _pagecontrol;
}
 
 
#pragma mark ------------ uiscrollviewdelegate --------
 
- (void)scrollviewdidscroll:(uiscrollview *)scrollview {
 if (self.timeranimation) {
  return;
 }
 cgfloat offscale = scrollview.contentoffset.x - offset_max;
 if (offscale < 0) {
  //右滑
  self.leftiamgeview.layer.transformscale = 1 + 1.0/9*(fabs(offscale)/offset_max);
  self.middleimageview.layer.transformscale = 1 - 0.1*(fabs(offscale)/offset_max);
 } else {
  //左滑
  self.rightimageview.layer.transformscale = 1 + 1.0/9*(fabs(offscale)/offset_max);
  self.middleimageview.layer.transformscale = 1 - 0.1*(fabs(offscale)/offset_max);
 }
  
}
 
- (void)scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:(bool)decelerate {
 cgpoint offset = scrollview.contentoffset;
  
 self.offsetx = offset.x;
  
 if ((fabs(self.offsetx-offset_max) >= offset_max*0.5)) {
  if (!decelerate) {
   //替换图片
   [self exchangeimage];
  }
 } else {
  //回弹复位
  [scrollview setcontentoffset:cgpointmake(offset_max, 0) animated:yes];
  self.leftiamgeview.layer.transform = catransform3didentity;
  self.middleimageview.layer.transform = catransform3didentity;
  self.rightimageview.layer.transform = catransform3didentity;
 }
}
 
- (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview {
 //替换图片
 [self exchangeimage];
}
 
#pragma mark ---------- exchangeimage ------
//替换图片
- (void)exchangeimage {
 if (self.offsetx-offset_max < 0) {
  //右滑
  uiimage *rightimage = self.rightimageview.image;
  self.rightimageview.image = self.middleimageview.image;
  self.middleimageview.image = self.leftiamgeview.image;
  self.leftiamgeview.image = rightimage;
   
  if (self.pagecontrol.currentpage - 1 < 0) {
   self.pagecontrol.currentpage = self.pagecontrol.numberofpages-1;
  } else {
   self.pagecontrol.currentpage -= 1;
  }
   
 } else {
  //左滑
  uiimage *leftimage = self.leftiamgeview.image;
  self.leftiamgeview.image = self.middleimageview.image;
  self.middleimageview.image = self.rightimageview.image;
  self.rightimageview.image = leftimage;
   
  if (self.pagecontrol.currentpage + 1 >= self.pagecontrol.numberofpages) {
   self.pagecontrol.currentpage = 0;
  } else {
   self.pagecontrol.currentpage += 1;
  }
 }
 [self.scrollview setcontentoffset:cgpointmake(offset_max, 0)];
 self.leftiamgeview.layer.transform = catransform3didentity;
 self.middleimageview.layer.transform = catransform3didentity;
 self.rightimageview.layer.transform = catransform3didentity;
}
 
#pragma mark ---------- timer --------
- (void)timeraction {
 self.offsetx = kscreenwidth+offset_max-64;
 
 self.timeranimation = yes;
 [uiview animatewithduration:0.8 animations:^{
  [self.scrollview setcontentoffset:cgpointmake(self.offsetx, 0)];
  self.rightimageview.layer.transformscale = 10/9.0;
  self.middleimageview.layer.transformscale = 0.9;
 } completion:^(bool finished) {
  self.timeranimation = no;
  [self exchangeimage];
  self.rightimageview.layer.transform = catransform3didentity;
  self.middleimageview.layer.transform = catransform3didentity;
 }];
}
 
@end

最后调用:

?
1
2
self.scrollview = [[customscrollview alloc] initwithframe:cgrectmake(0, 0, kscreenwidth, 170)];
[self.view addsubview:self.scrollview];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/NB_Token/article/details/73649517

延伸 · 阅读

精彩推荐
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01