您的位置:首页 > 产品设计 > UI/UE

iOS 利用UISCrollView作广告自动轮播效果

2016-03-09 15:49 399 查看
NSTimer 使用的好处就是当你在滑动如tableview界面是它任然可以实时轮播,

但是如果是开启一个后台线程让它轮播时,活动如tableview时,它就会暂停轮播

在日常开发中,我们很多的应用都需要用到轮播的效果,有可能是广告也有可能是软件的介绍等等都非常常用,下面我们利用纯代码写一个简单的轮播效果。

首先我们来看一下最后的效果图:

从图片看我们知道,其实这个效果只用了三个控件,第一个当然是我们的UISCrollView,第二个是pagecontrol小点,最后是UIImageView或者uibutton,我们所要做的是把UISCrollView和pageControl加入到当前vc中,然后把图片加入到UISCrollView中进行相关设置就行了,当然要自动轮播定时器是少不了的,下面看看代码:

1.添加我们的控件,还有相关设置:

12345678910111213141516171819202122232425262728293031323334353637383940414243#pragma mark -创建图片数组- (NSMutableArray *)advsList{ if(_advsList == nil){ _advsList = [NSMutableArray array]; } return _advsList;} #pragma mark -添加控件- (void)addView{ //添加图片 for (int i = 1; i < 5; i++) { [self.advsList addObject:[NSString stringWithFormat:@"%d.jpg",i]]; } self.view.backgroundColor = [UIColor whiteColor]; //添加滑动视图 self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 150)]; //设置代理这个必须的 self.scrollView.delegate = self; //设置总大小也就是里面容纳的大小 self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.advsList.count,self.scrollView.frame.size.height); [self.scrollView setBackgroundColor:[UIColor blackColor]]; //里面的子视图跟随父视图改变大小 [self.scrollView setAutoresizesSubviews:YES]; //设置分页形式,这个必须设置 [self.scrollView setPagingEnabled:YES]; //隐藏横竖滑动块 [self.scrollView setShowsVerticalScrollIndicator:NO]; [self.scrollView setShowsHorizontalScrollIndicator:NO]; [self.view addSubview:self.scrollView]; //场景UIPageControl显示控件,并修改小点颜色 self.pagePoint = [[UIPageControl alloc]init]; self.pagePoint.currentPageIndicatorTintColor = [UIColor blackColor]; self.pagePoint.pageIndicatorTintColor = [UIColor grayColor]; self.pagePoint.frame = CGRectMake(self.view.frame.size.width - self.pagePoint.bounds.size.width-self.advsList.count*10, self.scrollView.frame.origin.y+135, self.pagePoint.bounds.size.width, self.pagePoint.bounds.size.height); [self.pagePoint setNumberOfPages:[self.advsList count]]; [self.view addSubview:self.pagePoint]; }
这部分其实只要记得把UIPageControl加到当前视图,而不是UIScrollView中然后设置好各自的frame就好了,关于UIPageControl的更多介绍你可以移步到这篇文章中iOS UIPageControl的使用

2.添加图片到scroview中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

for(int
i
=
0;
i
<
[self.advsList
count];
i
++){

UIButton
*thumbView
=
[[UIButton
alloc]
init];

[thumbView
addTarget:self

action:@selector(myDidSelectAdvAtIndex:)

forControlEvents:UIControlEventTouchUpInside];

thumbView.tag
=
i;

thumbView.frame
=
CGRectMake(self.view.frame.size.width
*
i,
0,
self.view.frame.size.width,
150);

[thumbView
setBackgroundImage:[UIImage
imageNamed:self.advsList[i]]
forState:UIControlStateNormal];

//去掉高亮效果

thumbView.adjustsImageWhenHighlighted
=
NO;

[self.scrollView
addSubview:thumbView];

}

为了方便设置点击事件,我里面添加了UIButton这样打击事件处理也就相应的简单很多了。

3.UIScroView代理事件,求出当前的页数及滑动指定位置,以便设置我们的UIPageControl小圆点的位置

123456789101112131415161718#pragma mark -滑动的距离- (void)scrollToIndex:(NSInteger)index{ CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * index; frame.origin.y = 0; [self.scrollView scrollRectToVisible:frame animated:YES];} #pragma mark -滑动完成时计算滑动到第几页- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat pageWidth = scrollView.frame.size.width; int page = floor((scrollView.contentOffset.x - pageWidth / 2) /pageWidth) +1; [self.pagePoint setCurrentPage:page];}
4.添加一个定时器,让广告动起来

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#pragma mark -添加定时器

-(void)addNSTimer

{

NSTimer
*timer
=
[NSTimer
scheduledTimerWithTimeInterval:2
target:self
selector:@selector(nextPage)
userInfo:nil
repeats:YES];

//添加到runloop中

[[NSRunLoop
mainRunLoop]addTimer:timer
forMode:NSRunLoopCommonModes];

_timer
=
timer;

}

#pragma mark -删除定时器

-(void)removeNSTimer

{

[
_timer
invalidate];

_timer
=nil;

}

5.还有一个重要的一点是,我们手动滑动的时候你必须停止定时器,当手指离开时重新启动,这两个方法都是UIScroView的代理方法:

12345678910#pragma mark -当用户开始拖拽的时候就调用移除计时器-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self removeNSTimer];}#pragma mark -当用户停止拖拽的时候调用添加定时器-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [self addNSTimer];}
好了 一个简单的自动轮播广告已经做好了。如果你懒得自己写代码的话,下面是整个小demo的代码,其实只是一个.m文件而已Objective-C

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

//

// AdViewController.m

// test

//

// Created by GuoMS on 14-10-2.

// Copyright (c) 2014年 GuoMs. All rights reserved.

//

#import "AdViewController.h"

@interface
AdViewController
()<UIScrollViewDelegate>

{

NSTimer
*_timer;

BOOL
isAdd;

}

@end

@implementation
AdViewController

-
(void)viewDidLoad
{

[super
viewDidLoad];

#ifdef __IPHONE_7_0

if
([[UIDevice
currentDevice].systemVersion
floatValue]
>=
7.0)
{

self.edgesForExtendedLayout
=
UIRectEdgeNone;

}

#endif

[self
addView];

[self
showAdvs];

[self
addNSTimer];

}

#pragma mark -创建图片数组

-
(NSMutableArray
*)advsList

{

if(_advsList
==
nil){

_advsList
=
[NSMutableArray
array];

}

return
_advsList;

}

#pragma mark -添加控件

-
(void)addView{

//添加图片

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

[self.advsList
addObject:[NSString
stringWithFormat:@"%d.jpg",i]];

}

self.view.backgroundColor
=
[UIColor
whiteColor];

//添加滑动视图

self.scrollView
=
[[UIScrollView
alloc]initWithFrame:CGRectMake(0,
100,
self.view.frame.size.width,
150)];

//设置代理这个必须的

self.scrollView.delegate
=
self;

//设置总大小也就是里面容纳的大小

self.scrollView.contentSize
=
CGSizeMake(self.view.frame.size.width
*
self.advsList.count,self.scrollView.frame.size.height);

[self.scrollView
setBackgroundColor:[UIColor
blackColor]];

//里面的子视图跟随父视图改变大小

[self.scrollView
setAutoresizesSubviews:YES];

//设置分页形式,这个必须设置

[self.scrollView
setPagingEnabled:YES];

//隐藏横竖滑动块

[self.scrollView
setShowsVerticalScrollIndicator:NO];

[self.scrollView
setShowsHorizontalScrollIndicator:NO];

[self.view
addSubview:self.scrollView];

//场景UIPageControl显示控件,并修改小点颜色

self.pagePoint
=
[[UIPageControl
alloc]init];

self.pagePoint.currentPageIndicatorTintColor
=
[UIColor
blackColor];

self.pagePoint.pageIndicatorTintColor
=
[UIColor
grayColor];

self.pagePoint.frame
=
CGRectMake(self.view.frame.size.width
-
self.pagePoint.bounds.size.width-self.advsList.count*10,
self.scrollView.frame.origin.y+135,
self.pagePoint.bounds.size.width,
self.pagePoint.bounds.size.height);

[self.pagePoint
setNumberOfPages:[self.advsList
count]];

[self.view
addSubview:self.pagePoint];

}

#pragma mark -展示广告位,初始化

-(void)showAdvs{

isAdd
=
true;

for
(UIView
*view
in
[self.scrollView
subviews]){

[view
removeFromSuperview];

}

for(int
i
=
0;
i
<
[self.advsList
count];
i
++){

UIButton
*thumbView
=
[[UIButton
alloc]
init];

[thumbView
addTarget:self

action:@selector(myDidSelectAdvAtIndex:)

forControlEvents:UIControlEventTouchUpInside];

thumbView.tag
=
i;

thumbView.frame
=
CGRectMake(self.view.frame.size.width
*
i,
0,
self.view.frame.size.width,
150);

[thumbView
setBackgroundImage:[UIImage
imageNamed:self.advsList[i]]
forState:UIControlStateNormal];

thumbView.adjustsImageWhenHighlighted
=
NO;

[self.scrollView
addSubview:thumbView];

}

}

#pragma mark -添加定时器

-(void)addNSTimer

{

NSTimer
*timer
=
[NSTimer
scheduledTimerWithTimeInterval:2
target:self
selector:@selector(nextPage)
userInfo:nil
repeats:YES];

//添加到runloop中

[[NSRunLoop
mainRunLoop]addTimer:timer
forMode:NSRunLoopCommonModes];

_timer
=
timer;

}

#pragma mark -删除定时器

-(void)removeNSTimer

{

[
_timer
invalidate];

_timer
=nil;

}

#pragma mark -定时器下一页

-
(void)nextPage{

int
num
=
self.pagePoint.currentPage;

if(isAdd){

num++;

if(num
==
self.advsList.count
-1){

isAdd
=
false;

}

}else{

num--;

if(num
==
0){

isAdd
=
true;

}

}

[self
scrollToIndex:num];

}

#pragma mark -滑动的距离

-
(void)scrollToIndex:(NSInteger)index

{

CGRect
frame
=
self.scrollView.frame;

frame.origin.x
=
frame.size.width
*
index;

frame.origin.y
=
0;

[self.scrollView
scrollRectToVisible:frame
animated:YES];

}

#pragma mark -滑动完成时计算滑动到第几页

-
(void)scrollViewDidScroll:(UIScrollView
*)scrollView

{

CGFloat
pageWidth
=
scrollView.frame.size.width;

int
page
=
floor((scrollView.contentOffset.x
-
pageWidth
/
2)
/pageWidth)
+1;

[self.pagePoint
setCurrentPage:page];

}

#pragma mark -当用户开始拖拽的时候就调用移除计时器

-(void)scrollViewWillBeginDragging:(UIScrollView
*)scrollView

{

[self
removeNSTimer];

}

#pragma mark -当用户停止拖拽的时候调用添加定时器

-(void)scrollViewDidEndDragging:(UIScrollView
*)scrollView
willDecelerate:(BOOL)decelerate

{

[self
addNSTimer];

}

#pragma mark -点击广告

-
(void)myDidSelectAdvAtIndex:(id)
index

{

UIButton
*thumbView
=
(UIButton
*)index;

NSLog(@"你点击了第个%d广告",thumbView.tag);

}

@end

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: