如何更改UIPageControl的分页点的颜色?

我正在开发一个想要更改UIPageControl分页点的颜色或图像的应用程序。我该如何更改?是否可以UIpageControl在上述情况下进行自定义?



慕村9548890
浏览 876回答 3
3回答

互换的青春

在iOS 6中,您可以设置的色调颜色UIPageControl:有2个新属性:pageIndicatorTintColorcurrentPageIndicatorTintColor您还可以使用外观API更改所有页面指示器的颜色。如果您以iOS 5为目标,请确保它不会崩溃:if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)]) {    pageControl.pageIndicatorTintColor = [UIColor whiteColor];}

慕雪6442864

如果有人想要它的ARC /现代版本(无需将属性重新定义为ivar,无需取消分配,并且可以与Interface Builder一起使用):#import <UIKit/UIKit.h>@protocol PageControlDelegate;@interface PageControl : UIView&nbsp;// Set these to control the PageControl.@property (nonatomic) NSInteger currentPage;@property (nonatomic) NSInteger numberOfPages;// Customize these as well as the backgroundColor property.@property (nonatomic, strong) UIColor *dotColorCurrentPage;@property (nonatomic, strong) UIColor *dotColorOtherPage;// Optional delegate for callbacks when user taps a page dot.@property (nonatomic, weak) NSObject<PageControlDelegate> *delegate;@end@protocol PageControlDelegate<NSObject>@optional- (void)pageControlPageDidChange:(PageControl *)pageControl;@endPageControl.m:#import "PageControl.h"// Tweak these or make them dynamic.#define kDotDiameter 7.0#define kDotSpacer 7.0@implementation PageControl@synthesize dotColorCurrentPage;@synthesize dotColorOtherPage;@synthesize currentPage;@synthesize numberOfPages;@synthesize delegate;- (void)setCurrentPage:(NSInteger)page{&nbsp; &nbsp; currentPage = MIN(MAX(0, page), self.numberOfPages-1);&nbsp; &nbsp; [self setNeedsDisplay];}- (void)setNumberOfPages:(NSInteger)pages{&nbsp; &nbsp; numberOfPages = MAX(0, pages);&nbsp; &nbsp; currentPage = MIN(MAX(0, self.currentPage), numberOfPages-1);&nbsp; &nbsp; [self setNeedsDisplay];}- (id)initWithFrame:(CGRect)frame&nbsp;{&nbsp; &nbsp; if (self = [super initWithFrame:frame])&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Default colors.&nbsp; &nbsp; &nbsp; &nbsp; self.backgroundColor = [UIColor clearColor];&nbsp; &nbsp; &nbsp; &nbsp; self.dotColorCurrentPage = [UIColor blackColor];&nbsp; &nbsp; &nbsp; &nbsp; self.dotColorOtherPage = [UIColor lightGrayColor];&nbsp; &nbsp; }&nbsp; &nbsp; return self;}-(id)initWithCoder:(NSCoder *)aDecoder{&nbsp; &nbsp; if (self = [super initWithCoder:aDecoder])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; self.dotColorCurrentPage = [UIColor blackColor];&nbsp; &nbsp; &nbsp; &nbsp; self.dotColorOtherPage = [UIColor lightGrayColor];&nbsp; &nbsp; }&nbsp; &nbsp; return self;}- (void)drawRect:(CGRect)rect&nbsp;{&nbsp; &nbsp; CGContextRef context = UIGraphicsGetCurrentContext();&nbsp; &nbsp;&nbsp; &nbsp; CGContextSetAllowsAntialiasing(context, true);&nbsp; &nbsp; CGRect currentBounds = self.bounds;&nbsp; &nbsp; CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;&nbsp; &nbsp; CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;&nbsp; &nbsp; CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;&nbsp; &nbsp; for (int i=0; i<self.numberOfPages; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);&nbsp; &nbsp; &nbsp; &nbsp; if (i == self.currentPage)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; CGContextFillEllipseInRect(context, circleRect);&nbsp; &nbsp; &nbsp; &nbsp; x += kDotDiameter + kDotSpacer;&nbsp; &nbsp; }}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{&nbsp; &nbsp; if (!self.delegate) return;&nbsp; &nbsp; CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];&nbsp; &nbsp; CGFloat dotSpanX = self.numberOfPages*(kDotDiameter + kDotSpacer);&nbsp; &nbsp; CGFloat dotSpanY = kDotDiameter + kDotSpacer;&nbsp; &nbsp; CGRect currentBounds = self.bounds;&nbsp; &nbsp; CGFloat x = touchPoint.x + dotSpanX/2 - CGRectGetMidX(currentBounds);&nbsp; &nbsp; CGFloat y = touchPoint.y + dotSpanY/2 - CGRectGetMidY(currentBounds);&nbsp; &nbsp; if ((x<0) || (x>dotSpanX) || (y<0) || (y>dotSpanY)) return;&nbsp; &nbsp; self.currentPage = floor(x/(kDotDiameter+kDotSpacer));&nbsp; &nbsp; if ([self.delegate respondsToSelector:@selector(pageControlPageDidChange:)])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [self.delegate pageControlPageDidChange:self];&nbsp; &nbsp; }}@end
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS