代码为ios中的每个分页点赋予不同的颜色 [英] Code to give different color for each pagination dots in ios

查看:90
本文介绍了代码为ios中的每个分页点赋予不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is that possible to give each pagination dots with different colors? Suppose if i have 4 dots means is that possible to give 4 different color for each dots in ios?

推荐答案

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

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

by using the above code in the blog you will get different color dots as shown below image with yellow rectangle.

For方便的添加代码

For your convenience adding the code

custompagecontrol的实现

implemetation of the custompagecontrol

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

/// .h file

/// .h file

#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 file

//.m file

#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

这篇关于代码为ios中的每个分页点赋予不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆