有时候UIPageControl需要用到白色的背景, 那么会导致上面的点按钮看不见或不清楚,
我们可以通过继承该类重写函数来更换点按钮的图片现实.
实现思路如下.
新建类继承UIPageControl :
复制代码
- @interface MyPageControl : UIPageControl
- {
- UIImage *imagePageStateNormal;
- UIImage *imagePageStateHighlighted;
- }
- - (id)initWithFrame:(CGRect)frame;
- @property (nonatomic, retain) UIImage *imagePageStateNormal;
- @property (nonatomic, retain) UIImage *imagePageStateHighlighted;
- @end
|
声明了初始化该类的函数
用了两个UIImage保存两张图片, 大家知道的, UIPageCotrol的按钮分为两态, 一个是正常, 一个是高亮
接下来实现该类以及重写父类方法:
复制代码
- @interface MyPageControl(private) // 声明一个私有方法, 该方法不允许对象直接使用
- - (void)updateDots;
- @end
-
- @implementation MyPageControl // 实现部分
-
- @synthesize imagePageStateNormal;
- @synthesize imagePageStateHighlighted;
-
- - (id)initWithFrame:(CGRect)frame { // 初始化
- self = [super initWithFrame:frame];
- return self;
- }
-
- - (void)setImagePageStateNormal:(UIImage *)image { // 设置正常状态点按钮的图片
- [imagePageStateNormal release];
- imagePageStateNormal = [image retain];
- [self updateDots];
- }
-
- - (void)setImagePageStateHighlighted:(UIImage *)image { // 设置高亮状态点按钮图片
- [imagePageStateHighlighted release];
- imagePageStateHighlighted = [image retain];
- [self updateDots];
- }
-
- - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { // 点击事件
- [super endTrackingWithTouch:touch withEvent:event];
- [self updateDots];
- }
-
- - (void)updateDots { // 更新显示所有的点按钮
- if (imagePageStateNormal || imagePageStateHighlighted)
- {
- NSArray *subview = self.subviews; // 获取所有子视图
- for (NSInteger i = 0; i < [subview count]; i++)
- {
- UIImageView *dot = [subview objectAtIndex:i]; // 以下不解释, 看了基本明白
- dot.image = self.currentPage == i ? imagePageStateNormal : imagePageStateHighlighted;
- }
- }
- }
-
- - (void)dealloc { // 释放内存
- [imagePageStateNormal release], imagePageStateNormal = nil;
- [imagePageStateHighlighted release], imagePageStateHighlighted = nil;
- [super dealloc];
- }
-
- @end
|
OK, 在添加处加入以下来实例化该对象代码:
复制代码
- MyPageControl *pageControl = [[MyPageControl alloc] initWithFrame:CGRectMake(0,0, 200, 30)];
- pageControl.backgroundColor = [UIColor clearColor];
- pageControl.numberOfPages = 5;
- pageControl.currentPage = 0;
- [pageControl setImagePageStateNormal:[UIImage imageNamed:@"pageControlStateNormal.png"]];
- [pageControl setImagePageStateHighlighted:[UIImage imageNamed:@"pageControlStateHighlighted.png"]];
- [self.view addSubview:pageControl];
- [pageControl release];
|
以上内容摘自:http://www.cocoachina.com/bbs/read.php?tid-51384-page-1.html
************
有时候我们往往需要用到定时器,让UIPageControl自己跑,而不是我们人工控制(比如视频网站常见的广告)。所以需要我们对代码稍微修改一下。
首先,在pageControl初始化的时候加一个点击事件
[pageControladdTarget:selfaction:@selector(pcClick:)forControlEvents:UIControlEventTouchUpInside];
此处说明一下,为了方便,我把MyPageControl *pageControl放在了头文件中。
写一个UIScrollView:
//*****广告动画***
UIScrollView * sv = [[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, 320, 200)];
sv.backgroundColor = [UIColorgrayColor];
sv.showsHorizontalScrollIndicator =NO;
sv.pagingEnabled = YES;//分页属性
sv.delegate = self;
sv.tag = 100;
[self.view addSubview:sv];
[sv release];
//广告里的图片
sv.contentSize =CGSizeMake(320 * 6,200);//320 * 6是一共6张图片。第5张下面再加上第一张
for (int i = 1; i <= 6; i++) {//6张图片。第5张下面是第一张
//广告的imageView
UIImageView* imgView = [[UIImageViewalloc] initWithFrame:CGRectMake( (i -1) * 320, 0,320, 200)];
imgView.image = [UIImageimageNamed:[NSString stringWithFormat:@"iphoto%d.png",i]];
if (i == 6)) {//当i= 6时,显示1.jpg
imgView.image = [UIImageimageNamed:@"iphoto1.png"];
}
[sv addSubview:imgView];
[imgView release];
}
之后起一个NSTimer:
[NSTimerscheduledTimerWithTimeInterval:2.5target:selfselector:@selector(timer:)userInfo:nilrepeats:YES];
点击翻页圆点的事件
- (void)pcClick:(UIPageControl*)pc {
UIScrollView * scrollView = (UIScrollView *)[self.viewviewWithTag:100];
scrollView.contentOffset = CGPointMake(pageControl.currentPage *320, 0);//点击翻页点翻页
}
广告的定时器事件
- (void)timer:(NSTimer*)timer//
{
UIScrollView* sv = (UIScrollView*)[self.viewviewWithTag:100];
if (sv.contentOffset.x ==320 * 5) {//当显示第5张的时候,切换到第一张。(两张是同一张图片)
sv.contentOffset =CGPointMake(0,0);//切换到最开始的第一张
}
[sv setContentOffset:CGPointMake(sv.contentOffset.x +320, 0)animated:YES];//动画显示切换
}
UIScrollView 的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
pageControl.currentPage = scrollView.contentOffset.x /320;
if (scrollView.contentOffset.x > 4*320) {
pageControl.currentPage =0;
}
//这里要重新写一下在两种状下的图片,否则又会回到默认状态
[pageControlsetImagePageStateNormal:[UIImageimageNamed:@"pageControlStateHighlighted.png"]];
[pageControlsetImagePageStateHighlighted:[UIImageimageNamed:@"pageControlStateNormal.png"]];
}
本人水平有限,如有错误,请轻拍