服务器之家

服务器之家 > 正文

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

时间:2021-04-25 18:37     来源/作者:NB_Token

本文实例为大家分享了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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部