tpagecontrol属性_如何更改UIPageControl的分页点的颜色?

此博客介绍了如何在iOS 6.0及以上版本创建一个可自定义颜色的UIPageControl替代品,通过Core Graphics实现彩色点状指示器,并提供 delegate 接口以响应用户点击。作者分享了详细的代码实现和使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更新:

这个答案已有6年历史,而且已经过时了,但是仍然吸引着投票和评论。从iOS 6.0开始,您应该在上使用pageIndicatorTintColor和currentPageIndicatorTintColor属性UIPageControl。

原始答案:

我今天遇到了这个问题,决定编写自己的简单替换类。

这是一个细分的UIView,它使用Core Graphics以您指定的颜色渲染点。

您可以使用公开的属性来自定义和控制它。

如果需要,您可以注册一个委托对象,以在用户点击其中一个小页面点时获得通知。如果未注册任何委托,则视图将不响应触摸输入。

它完全是烤箱里新鲜的,但似乎可以用。让我知道您是否遇到任何问题。

未来的改进:

如果太多,请调整点的大小以适合当前范围。

不要在drawRect中重绘整个视图:

使用示例:

CGRect f = CGRectMake(0, 0, 320, 20);

PageControl *pageControl = [[[PageControl alloc] initWithFrame:f] autorelease];

pageControl.numberOfPages = 10;

pageControl.currentPage = 5;

pageControl.delegate = self;

[self addSubview:pageControl];

头文件:

//

//  PageControl.h

//

//  Replacement for UIPageControl because that one only supports white dots.

//

//  Created by Morten Heiberg on November 1, 2010.

//

#import

@protocol PageControlDelegate;

@interface PageControl : UIView

{

@private

NSInteger _currentPage;

NSInteger _numberOfPages;

UIColor *dotColorCurrentPage;

UIColor *dotColorOtherPage;

NSObject *delegate;

//If ARC use __unsafe_unretained id delegate;

}

// Set these to control the PageControl.

@property (nonatomic) NSInteger currentPage;

@property (nonatomic) NSInteger numberOfPages;

// Customize these as well as the backgroundColor property.

@property (nonatomic, retain) UIColor *dotColorCurrentPage;

@property (nonatomic, retain) UIColor *dotColorOtherPage;

// Optional delegate for callbacks when user taps a page dot.

@property (nonatomic, retain) NSObject *delegate;

@end

@protocol PageControlDelegate

@optional

- (void)pageControlPageDidChange:(PageControl *)pageControl;

@end

实施文件:

//

//  PageControl.m

//

//  Replacement for UIPageControl because that one only supports white dots.

//

//  Created by Morten Heiberg on November 1, 2010.

//

#import "PageControl.h"

// Tweak these or make them dynamic.

#define kDotDiameter 7.0

#define kDotSpacer 7.0

@implementation PageControl

@synthesize dotColorCurrentPage;

@synthesize dotColorOtherPage;

@synthesize delegate;

- (NSInteger)currentPage

{

return _currentPage;

}

- (void)setCurrentPage:(NSInteger)page

{

_currentPage = MIN(MAX(0, page), _numberOfPages-1);

[self setNeedsDisplay];

}

- (NSInteger)numberOfPages

{

return _numberOfPages;

}

- (void)setNumberOfPages:(NSInteger)pages

{

_numberOfPages = MAX(0, pages);

_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);

[self setNeedsDisplay];

}

- (id)initWithFrame:(CGRect)frame

{

if ((self = [super initWithFrame:frame]))

{

// Default colors.

self.backgroundColor = [UIColor clearColor];

self.dotColorCurrentPage = [UIColor blackColor];

self.dotColorOtherPage = [UIColor lightGrayColor];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];

[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

[self addGestureRecognizer:swipeRight];

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];

[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];

[self addGestureRecognizer:swipe];

}

return self;

}

-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer

{

self.currentPage++;

}

-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer

{

self.currentPage--;

}

- (void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetAllowsAntialiasing(context, true);

CGRect currentBounds = self.bounds;

CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;

CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;

CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;

for (int i=0; i<_numberofpages i>

{

CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);

if (i == _currentPage)

{

CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);

}

else

{

CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);

}

CGContextFillEllipseInRect(context, circleRect);

x += kDotDiameter + kDotSpacer;

}

}

- (void)dealloc

{

[dotColorCurrentPage release];

[dotColorOtherPage release];

[delegate release];

[super dealloc];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

if (!self.delegate) return;

CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];

CGFloat dotSpanX = self.numberOfPages*(kDotDiameter + kDotSpacer);

CGFloat dotSpanY = kDotDiameter + kDotSpacer;

CGRect currentBounds = self.bounds;

CGFloat x = touchPoint.x + dotSpanX/2 - CGRectGetMidX(currentBounds);

CGFloat y = touchPoint.y + dotSpanY/2 - CGRectGetMidY(currentBounds);

if ((x<0) || (x>dotSpanX) || (y<0) || (y>dotSpanY)) return;

self.currentPage = floor(x/(kDotDiameter+kDotSpacer));

if ([self.delegate respondsToSelector:@selector(pageControlPageDidChange:)])

{

[self.delegate pageControlPageDidChange:self];

}

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值