gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 31 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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 。

最后此篇关于iOS实现带有缩放效果的自动轮播图的文章就讲到这里了,如果你想了解更多关于iOS实现带有缩放效果的自动轮播图的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com