自定义UIPageControl

当需要改变UIPageControl的小圆点图片的时候,需要继承UIPageConrtol并重写几个方法。

设置好两个状态对应的两张图片,之后再写一个更新函数,再重写setCurrentPage函数

//
//  MyUIPageControl.h
//  MicroCourse
//
//  Created by kay_sprint on 12-11-29.
//  Copyright (c) 2012年 cn.edu.scnu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyUIPageControl : UIPageControl

@property (strong, nonatomic) UIImage *normalImage;
@property (strong, nonatomic) UIImage *highlightedImage;

@end

//
//  MyUIPageControl.m
//  MicroCourse
//
//  Created by kay_sprint on 12-11-29.
//  Copyright (c) 2012年 cn.edu.scnu. All rights reserved.
//

#import "MyUIPageControl.h"

@interface MyUIPageControl()

- (void)uodateDots;

@end

@implementation MyUIPageControl
@synthesize normalImage = _normalImage;
@synthesize highlightedImage = _highlightedImage;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.normalImage = [UIImage imageNamed:@"roll@2x.png"];
        self.highlightedImage = [UIImage imageNamed:@"roll_highlight@2x.png"];
    }
    return self;
}

- (void)setNormalImage:(UIImage *)normalImage
{
    _normalImage = normalImage;
    [self updateDots];
}

- (void)setHighlightedImage:(UIImage *)highlightedImage
{
    _highlightedImage = highlightedImage;
    [self updateDots];
}

- (void)updateDots
{
    if(_normalImage || _highlightedImage)
    {
        NSArray *subviews = self.subviews;
        for(NSInteger i = 0;i<subviews.count;i++)
        {
            UIImageView *dot = [subviews objectAtIndex:i];
            dot.image = self.currentPage == i ? _highlightedImage : _normalImage ;
        }
    }
}

- (void)setCurrentPage:(NSInteger)currentPage
{
    [super setCurrentPage:currentPage];
    [self updateDots];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

用的时候很简单 下面这样初始化一下就ok了

- (void)initPageControl
{
    //加载自定义UIPageControl
    _pageControl = [[MyUIPageControl alloc] initWithFrame:CGRectMake(104, 370, 300, 50)];
    _pageControl.userInteractionEnabled = NO;
    _pageControl.backgroundColor = [UIColor clearColor];
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = self.recCourseImgPath.count;
    [self.view addSubview:_pageControl];
    [_pageControl setCurrentPage:0];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值