第一种。利用运行时,容易奔溃,审核被拒。
// 初始化一个属性列表数组
var ivarName_pageControl: [String] = []
var count: uint = 0
// 获取属性列表
let list = class_copyIvarList(UIPageControl.classForCoder(), &count)
for index in 0 ... count-1 {
// 获取属性名称,ivar_getTypeEncoding 可获取属性类型
let ivarName = ivar_getName( list![Int(index)] )
let name = String.init(cString: ivarName!)
ivarName_pageControl.append(name)
}
// 判断是否包含这两个属性
if ivarName_pageControl.contains("_pageImage") && ivarName_pageControl.contains("_currentPageImage")
{
pageControl.setValue(UIImage.init(named: "lunbo_dian_gray"), forKey: "_pageImage")
pageControl.setValue(UIImage.init(named: "lunbo_dian_white"), forKey: "_currentPageImage")
}
第二种(推荐)可随意更改形状和颜色,但需要设计切图给你
class FangPageControl: UIView {
var numberOfPages:Int!//数量
var currentPage:Int!//当前
var PointSize:CGFloat!//大小
var distanceOfPoint:CGFloat! //间距
var inactiveImage:String!//激活状态图片
var activeImage:String! //未激活状态图片
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func sizeForNumberOfPages(pages:NSInteger)->CGFloat
{
return distanceOfPoint*CGFloat(pages + 1)+CGFloat(PointSize*CGFloat(pages))
}
func setNumberOfPages(pages:NSInteger)
{
for i in 0.. let pointImageView = UIImageView.init(frame: CGRect.init(x: (frame.size.width - CGFloat(pages)*(PointSize+distanceOfPoint))/2 + CGFloat((distanceOfPoint+PointSize)*CGFloat(i)), y: (self.frame.size.height-PointSize)/2, width: PointSize, height: PointSize)) pointImageView.image = UIImage.init(named: self.activeImage) pointImageView.tag = 5555 + i self.addSubview(pointImageView) } } func setCurrentPage(page:NSInteger) { let countOfPages = self.subviews.count for i in 0.. let subview = self.subviews[i]as!UIImageView if i == page { subview.image = UIImage.init(named: self.inactiveImage) }else { subview.image = UIImage.init(named: self.activeImage) } } } }