-
Notifications
You must be signed in to change notification settings - Fork 49
/
AdvertisingColumn.m
139 lines (125 loc) · 5.84 KB
/
AdvertisingColumn.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
//
// AdvertisingColumn.m
// CustomTabBar
//
// Created by shikee_app05 on 14-12-30.
// Copyright (c) 2014年 chan kaching. All rights reserved.
//
#import "AdvertisingColumn.h"
@implementation AdvertisingColumn
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
_scrollView.backgroundColor = [UIColor purpleColor];
_scrollView.delegate = self;//设置代理UIscrollViewDelegate
_scrollView.showsVerticalScrollIndicator = NO;//是否显示竖向滚动条
_scrollView.showsHorizontalScrollIndicator = NO;//是否显示横向滚动条
_scrollView.pagingEnabled = YES;//是否设置分页
[self addSubview:_scrollView];
/*
***容器,装载
*/
UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-20, CGRectGetWidth(self.frame), 20)];
containerView.backgroundColor = [UIColor clearColor];
[self addSubview:containerView];
UIView *alphaView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(containerView.frame), CGRectGetHeight(containerView.frame))];
alphaView.backgroundColor = [UIColor orangeColor];
alphaView.alpha = 0.7;
[containerView addSubview:alphaView];
//分页控制
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)];
_pageControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//貌似不起作用呢
_pageControl.currentPage = 0; //初始页码为0
// _pageControl.backgroundColor = [UIColor greenColor];
[containerView addSubview:_pageControl];
//图片张数
_imageNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, CGRectGetWidth(containerView.frame)-20, 20)];
_imageNum.font = [UIFont boldSystemFontOfSize:15];
_imageNum.backgroundColor = [UIColor clearColor];
_imageNum.textColor = [UIColor whiteColor];
_imageNum.textAlignment = NSTextAlignmentRight;
[containerView addSubview:_imageNum];
/*
***配置定时器,自动滚动广告栏
*/
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
}
return self;
}
//-------------------------------------------------------------------------------------------
-(void)timerAction:(NSTimer *)timer{
if (_totalNum>1) {
CGPoint newOffset = _scrollView.contentOffset;
newOffset.x = newOffset.x + CGRectGetWidth(_scrollView.frame);
// NSLog(@"newOffset.x = %f",newOffset.x);
if (newOffset.x > (CGRectGetWidth(_scrollView.frame) * (_totalNum-1))) {
newOffset.x = 0 ;
}
int index = newOffset.x / CGRectGetWidth(_scrollView.frame); //当前是第几个视图
newOffset.x = index * CGRectGetWidth(_scrollView.frame);
_imageNum.text = [NSString stringWithFormat:@"%d / %ld",index+1,_totalNum];
[_scrollView setContentOffset:newOffset animated:YES];
}else{
[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
}
}
#pragma mark- PageControl绑定ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{//滚动就执行(会很多次)
if ([scrollView isMemberOfClass:[UITableView class]]) {
}else {
int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width; //当前是第几个视图
_pageControl.currentPage = index;
for (UIView *view in scrollView.subviews) {
if(view.tag == index){
}else{
}
}
}
// NSLog(@"string%f",scrollView.contentOffset.x);
}
- (void)setArray:(NSArray *)imgArray{
_totalNum = [imgArray count];
if (_totalNum>0) {
for (int i = 0; i<_totalNum; i++) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*CGRectGetWidth(_scrollView.frame), 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
img.contentMode = UIViewContentModeScaleAspectFill;
img.image = [UIImage imageNamed:imgArray[i]];
//img.backgroundColor = imgArray[i];
[img setTag:i];
[_scrollView addSubview:img];
}
_imageNum.text = [NSString stringWithFormat:@"%ld / %ld",_pageControl.currentPage+1,_totalNum];
_pageControl.numberOfPages = _totalNum; //设置页数 //滚动范围 600=300*2,分2页
CGRect frame;
frame = _pageControl.frame;
frame.size.width = 15*_totalNum;
_pageControl.frame = frame;
}else{
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_scrollView.frame), CGRectGetHeight(_scrollView.frame))];
[img setImage:[UIImage imageNamed:@"comment_gray"]];
img.userInteractionEnabled = YES;
[_scrollView addSubview:img];
_imageNum.text = @"提示:滚动栏无数据。";
}
_scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame)*_totalNum,CGRectGetHeight(_scrollView.frame));//滚动范围 600=300*2,分2页
}
- (void)openTimer{
[_timer setFireDate:[NSDate distantPast]];//开启定时器
}
- (void)closeTimer{
[_timer setFireDate:[NSDate distantFuture]];//关闭定时器
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end