自定义 pageControl
来源:互联网 发布:医院数据集成平台 编辑:程序博客网 时间:2024/06/06 09:03
自定义 pageControl
#import <UIKit/UIKit.h>#import <QuartzCore/QuartzCore.h>@protocol PageControlDelegate;@interface PageControl : UIView {@private NSInteger _currentPage; NSInteger _numberOfPages; UIColor *dotColorCurrentPage; UIColor *dotColorOtherPage; NSObject<PageControlDelegate> *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<PageControlDelegate> *delegate;//init with default color;- (void)initData;@end@protocol PageControlDelegate<NSObject>@optional- (void)pageControlPageDidChange:(PageControl *)myPageControl;@end
#import "PageControl.h"// Tweak these or make them dynamic.#define kDotDiameter 7.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); [self setNeedsDisplay];}- (NSInteger)numberOfPages{ return _numberOfPages;}- (void)setNumberOfPages:(NSInteger)pages{ _numberOfPages = MAX(0, pages); _currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1); [self setNeedsDisplay];}- (void)initData{// Default colors.self.backgroundColor = [UIColor clearColor];self.dotColorCurrentPage = [UIColor blackColor];self.dotColorOtherPage = [UIColor lightGrayColor];}- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) {// Default colors.self.backgroundColor = [UIColor clearColor];self.dotColorCurrentPage = [UIColor blackColor];self.dotColorOtherPage = [UIColor lightGrayColor]; } 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<_numberOfPages; i++) { CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter); if (i == _currentPage) {CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor); } else {CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor); } CGContextFillEllipseInRect(context, circleRect); x += kDotDiameter + kDotSpacer; }}- (void)dealloc { [dotColorCurrentPage release]; [dotColorOtherPage release]; [super dealloc];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)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 respondsToSelector:@selector(pageControlPageDidChange:)]) { [self.delegate pageControlPageDidChange:self]; }}@end
- 自定义 pageControl
- 自定义pageControl
- 自定义PageControl图片
- iOS开发之自定义pageControl
- PageControl
- PageControl
- PageControl
- UIScrollView和自定义PageControl的结合使用
- 用自定义图片设置pageControl的圆点
- delphi PageControl
- iOS-PageControl
- Swift PageControl
- Swift_ScrollVoew&&PageControl
- SdaiBuyWebUI PageControl.cs
- ArcEngine 打印PageControl内容
- PageControl 判断选择项
- PageControl分组框学习。
- delphi PageControl显示窗体
- co.cc没落,cu.cc抬头
- 导出Excel
- Java读取jar中的文件
- C# abstract
- 00017.Oracle继续执行上一条SQL
- 自定义 pageControl
- 00018.Oracle获取cpu最多9位用户
- java 截取图片的中央部分
- Struts2上传文件过大解决方案
- 使用fftw3-3进行快速傅里叶变换(二)
- [UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别
- 理解Load Average做好压力测试
- 白话经典算法系列之七 堆与堆排序
- IPhone之UIView中动画