菜鸟教程小白 发表于 2022-12-13 02:54:00

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


                                            <p><p>是否可以为每个分页点赋予不同的颜色?假设如果我有 4 个点意味着是否可以为 ios 中的每个点提供 4 种不同的颜色?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p> <a href="http://muthesblog.blogspot.in/2011/11/custompagecontrol.html" rel="noreferrer noopener nofollow">http://muthesblog.blogspot.in/2011/11/custompagecontrol.html</a> </p>

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

<p>为了方便添加代码</p>

<p>自定义页面控件的实现</p>

<pre><code>PageControl *&lt;pageControl&gt; = [[ initWithFrame:f] autorelease];
&lt;pageControl&gt;.numberOfPages = 5;
&lt;pageControl&gt;.currentPage = 1;
&lt;pageControl&gt;.delegate = self;
;
</code></pre>

<p>///.h 文件</p>

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

<p>//.m 文件</p>

<pre><code>#import &#34;PageControl.h&#34;
// 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);
    ;
}
- (NSInteger)numberOfPages{
    return _numberOfPages;
}
- (void)setNumberOfPages:(NSInteger)pages{
    _numberOfPages = MAX(0, pages);
    _currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
    ;
}
- (id)initWithFrame:(CGRect)frame {
    if ((self = ))
    {
      // Default colors.
      self.backgroundColor = ;
      self.dotColorCurrentPage = ;
      self.dotColorOtherPage = ;
    }
    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&lt;_numberOfPages; i++)
    {
      CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
      if (i == _currentPage)
      {
            CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
      }
      else
      {
            if(i==0) {
                CGContextSetFillColorWithColor(context, .CGColor);

            } else if(i==1) {
                CGContextSetFillColorWithColor(context, .CGColor);

            } else if (i==2) {
                CGContextSetFillColorWithColor(context, .CGColor);

            } else if (i==3) {
                CGContextSetFillColorWithColor(context, .CGColor);
            } else if (i==4) {
                CGContextSetFillColorWithColor(context, .CGColor);
            }
      }
      CGContextFillEllipseInRect(context, circleRect);
      x += kDotDiameter + kDotSpacer;
    }
}
- (void)dealloc {
    ;
    ;
    ;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.delegate) return;

    CGPoint touchPoint = [[ 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&lt;0) || (x&gt;dotSpanX) || (y&lt;0) || (y&gt;dotSpanY)) return;

    self.currentPage = floor(x/(kDotDiameter+kDotSpacer));
    if ()
    {
      ;
    }
}
@end
</code></pre>

<p> <img src="/image/s7Cmk.png" alt="enter image description here"/> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为ios中的每个分页点赋予不同颜色的代码,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26648988/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26648988/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为ios中的每个分页点赋予不同颜色的代码