34

iOS自定义PageControl

 5 years ago
source link: http://www.cocoachina.com/ios/20190131/26281.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

自定义PageControl

我们经常会用到PageControl,但是系统的PageControl只有一种样式,不支持多样化,这里简单介绍一个简单的自定义PageControl.

主要功能:

  1. 可以自定义间隙

  2. 可以自定义高度

  3. 可以自定义选中点的宽度

  4. 每个点有点击事件

  5. 可以自定义选中和非选中的图片

  6. 可以设置居左、居中、居右样式

  7. 支持长条形状

实现:

将自定义PageControl类拖到项目中,并导入头文件.

//创建pageControl
XHPageControl  *_pageControl = [[XHPageControl alloc] initWithFrame:CGRectMake(0, 300,[UIScreen mainScreen].bounds.size.width, 30)];
//设置点的总个数
_pageControl.numberOfPages = 7;
//设置非选中点的宽度是高度的倍数(设置长条形状)
_pageControl.otherMultiple = 2;
//设置选中点的宽度是高度的倍数(设置长条形状)
_pageControl.currentMultiple = 4;
//设置样式.默认居中显示
_pageControl.type = PageControlLeft;
//非选中点的颜色  
_pageControl.otherColor=[UIColor grayColor];
//选中点的颜色  
_pageControl.currentColor=[UIColor orangeColor];
//代理    
_pageControl.delegate = self;
//标记    
_pageControl.tag = 902;

[self.view addSubview:_pageControl];

代理实现:

#pragma mark - 代理
-(void)xh_PageControlClick:(XHPageControl*)pageControl index:(NSInteger)clickIndex{

    NSLog(@"%ld",clickIndex);
    if(pageControl.tag == 902){
        CGPoint position = CGPointMake([UIScreen mainScreen].bounds.size.width * clickIndex, 0);
        [_scrollView2 setContentOffset:position animated:YES];
    }
}

代码demo: XHPageControl-Demo


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK