• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 为ios中的每个分页点赋予不同颜色的代码

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:54:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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



Best Answer-推荐答案


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<ageControlDelegate> *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<ageControlDelegate> *delegate;
@end
@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChangePageControl *)pageControl;
@end

//.m 文件

#import "ageControl.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)setCurrentPageNSInteger)page{
    _currentPage = MIN(MAX(0, page), _numberOfPages-1);
    [self setNeedsDisplay];
}
- (NSInteger)numberOfPages{
    return _numberOfPages;
}
- (void)setNumberOfPagesNSInteger)pages{
    _numberOfPages = MAX(0, pages);
    _currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
    [self setNeedsDisplay];
}
- (id)initWithFrameCGRect)frame {
    if ((self = [super initWithFrame:frame]))
    {
        // Default colors.
        self.backgroundColor = [UIColor clearColor];
        self.dotColorCurrentPage = [UIColor greenColor];
        self.dotColorOtherPage = [UIColor whiteColor];
    }
    return self;
}
- (void)drawRectCGRect)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)touchesBeganNSSet *)touches withEventUIEvent *)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 respondsToSelectorselector(pageControlPageDidChange])
    {
        [self.delegate pageControlPageDidChange:self];
    }
}
@end

enter image description here

关于ios - 为ios中的每个分页点赋予不同颜色的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26648988/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap