为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
慕村9548890 2019-12-07 15:31:56
我正在开发一个想要更改UIPageControl分页点的颜色或图像的应用程序。我该如何更改?是否可以UIpageControl在上述情况下进行自定义?
查看完整描述

3 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

在iOS 6中,您可以设置的色调颜色UIPageControl:


有2个新属性:


pageIndicatorTintColor

currentPageIndicatorTintColor

您还可以使用外观API更改所有页面指示器的颜色。


如果您以iOS 5为目标,请确保它不会崩溃:


if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)]) {

    pageControl.pageIndicatorTintColor = [UIColor whiteColor];

}


查看完整回答
反对 回复 2019-12-07
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

如果有人想要它的ARC /现代版本(无需将属性重新定义为ivar,无需取消分配,并且可以与Interface Builder一起使用):


#import <UIKit/UIKit.h>


@protocol PageControlDelegate;


@interface PageControl : UIView 


// 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;

@end

PageControl.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

{

    currentPage = MIN(MAX(0, page), self.numberOfPages-1);

    [self setNeedsDisplay];

}


- (void)setNumberOfPages:(NSInteger)pages

{

    numberOfPages = MAX(0, pages);

    currentPage = MIN(MAX(0, self.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];

    }

    return self;

}


-(id)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [super initWithCoder:aDecoder])

    {

        self.dotColorCurrentPage = [UIColor blackColor];

        self.dotColorOtherPage = [UIColor lightGrayColor];

    }

    return self;

}


- (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<self.numberOfPages; i++)

    {

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

        if (i == self.currentPage)

        {

            CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);

        }

        else

        {

            CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);

        }

        CGContextFillEllipseInRect(context, circleRect);

        x += kDotDiameter + kDotSpacer;

    }

}



- (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


查看完整回答
反对 回复 2019-12-07
  • 3 回答
  • 0 关注
  • 874 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信