【问题标题】:Code to give different color for each pagination dots in ios为ios中的每个分页点赋予不同颜色的代码
【发布时间】:2014-10-30 09:13:51
【问题描述】:

是否可以为每个分页点赋予不同的颜色?假设如果我有 4 个点意味着是否可以为 ios 中的每个点提供 4 种不同的颜色?

【问题讨论】:

  • 很遗憾你不能。
  • 你在分页中使用静态或动态动作
  • 您只能在自定义重新实现中执行此操作,而不是在标准控制中。

标签: ios


【解决方案1】:

http://muthesblog.blogspot.in/2011/11/custompagecontrol.html

通过在博客中使用上面的代码,您将得到不同颜色的点,如下图黄色矩形所示。

为了方便添加代码

自定义页面控件的实现

PageControl *<pageControl> = [[[PageControl alloc] initWithFrame:f] autorelease];
<pageControl>.numberOfPages = 5;
<pageControl>.currentPage = 1;
<pageControl>.delegate = self;
[self.view addSubview:<pageControl>];

/// .h 文件

#import <UIKit/UIKit.h>
@protocol PageControlDelegate;
@interface PageControl : UIView {
@private
    NSInteger _currentPage;
    NSInteger _numberOfPages;
    UIColor *dotColorCurrentPage;
    UIColor *dotColorOtherPage;
    NSObject<PageControlDelegate> *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, assign) NSObject<PageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end

//.m 文件

#import "PageControl.h"
// Tweak these or make them dynamic.
#define kDotDiameter 10.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 greenColor];
        self.dotColorOtherPage = [UIColor whiteColor];
    }
    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<_numberOfPages; i++)
    {
        CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
        if (i == _currentPage)
        {
            CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
        }
        else
        {
            if(i==0) {
                CGContextSetFillColorWithColor(context, [UIColor magentaColor].CGColor);

            } else if(i==1) {
                CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);

            } else if (i==2) {
                CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);

            } else if (i==3) {
                CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
            } else if (i==4) {
                CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
            }
        }
        CGContextFillEllipseInRect(context, circleRect);
        x += kDotDiameter + kDotSpacer;
    }
}
- (void)dealloc {
    [dotColorCurrentPage release];
    [dotColorOtherPage 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

【讨论】:

    【解决方案2】:

    由于 iOS 的限制,没有本地方法可以更改每个页面指示器的颜色。

    但是,您可以使用以下代码来更改当前选定指标的颜色以及其他指标的颜色,这可能是一个不错的折衷方案。

    pageControl.pageIndicatorTintColor = [UIColor purpleColor];
    pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
    

    如果此功能对您的应用至关重要,我建议您创建一个自定义页面指示器来满足您的目的,或者您可以在网上找到一个允许您执行此操作的库。

    我希望能回答你的问题:)

    【讨论】:

      【解决方案3】:
      Check this below code 
      
      pageControl.pageIndicatorTintColor = [UIColor purpleColor];
      pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
      
      or Please refer this link
      
      http://stackoverflow.com/questions/2942636/how-can-i-change-the-color-of-pagination-dots-of-uipagecontrol 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 2018-11-24
        • 2021-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多