ImageVerifierCode 换一换
格式:DOCX , 页数:39 ,大小:47.04KB ,
资源ID:26456348      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/26456348.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(iOS 自定义日历.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

iOS 自定义日历.docx

1、iOS 自定义日历iOS 自定义日历大体说一下思路,核心点在于创建布局每个月的日期,点击年份月份只是刷新数据。每一个日期是一个视图,我这里用的是UIButton,通过循环添加到日期视图。获取日期一定是基于NSDate,通过NSCalendar获取到指定的某一年的某一月有多少天和第一天是星期几,这样就可以在循环中知道当前月份需要设置的初始日期位置以及需要设置的日期个数。其他就是展示和一些逻辑判断。下面贴上代码:ViewController:objc view plain copy 在CODE上查看代码片派生到我的代码片#import interface ViewController : UIVi

2、ewController end /* -分割线- */ #import ViewController.h #import HWCalendar.h interface ViewController () property (nonatomic, weak) HWCalendar *calendar; property (nonatomic, strong) UITextField *textField; end implementation ViewController - (void)viewDidLoad super viewDidLoad; self.view.backgroundCo

3、lor = UIColor blackColor; /创建控件 self creatControl; - (void)creatControl /输入框 _textField = UITextField alloc initWithFrame:CGRectMake(7, 64, 400, 44); _textField.delegate = self; _textField.layer.cornerRadius = 22; _textField.layer.masksToBounds = YES; _textField.placeholder = 请设置日期; _textField.textA

4、lignment = NSTextAlignmentCenter; _textField.backgroundColor = UIColor whiteColor; self.view addSubview:_textField; /日历 HWCalendar *calendar = HWCalendar alloc initWithFrame:CGRectMake(7, UIScreen mainScreen.bounds.size.height, 400, 396); calendar.delegate = self; calendar.showTimePicker = YES; self

5、.view addSubview:calendar; self.calendar = calendar; #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField if (_calendar.frame.origin.y != UIScreen mainScreen.bounds.size.height & _calendar) _calendar dismiss; return NO; else if (textField = _textField) _cal

6、endar show; return NO; return YES; #pragma mark - HWCalendarDelegate - (void)calendar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date _textField.text = date; end HWCalendar:objc view plain copy 在CODE上查看代码片派生到我的代码片#import class HWCalendar; protocol HWCalendarDelegate - (void)calend

7、ar:(HWCalendar *)calendar didClickSureButtonWithDate:(NSString *)date; end interface HWCalendar : UIView property (nonatomic, assign) BOOL showTimePicker; /default is NO. doesnt show timePicker property (nonatomic, weak) id delegate; - (void)show; - (void)dismiss; end /* -分割线- */ #import HWCalendar.

8、h #import HWOptionButton.h #define KCol 7 #define KBtnW 44 #define KBtnH 44 #define KMaxCount 37 #define KBtnTag 100 #define KTipsW 92 #define KShowYearsCount 100 #define KMainColor UIColor colorWithRed:0.0f green:139/255.0f blue:125/255.0f alpha:1.0f #define KbackColor UIColor colorWithRed:173/255.

9、0f green:212/255.0f blue:208/255.0f alpha:1.0f interface HWCalendar () property (nonatomic, strong) NSArray *weekArray; property (nonatomic, strong) NSArray *timeArray; property (nonatomic, strong) NSArray *yearArray; property (nonatomic, strong) NSArray *monthArray; property (nonatomic, strong) UIP

10、ickerView *timePicker; property (nonatomic, weak) UIView *calendarView; property (nonatomic, weak) HWOptionButton *yearBtn; property (nonatomic, weak) HWOptionButton *monthBtn; property (nonatomic, weak) UILabel *weekLabel; property (nonatomic, weak) UILabel *yearLabel; property (nonatomic, weak) UI

11、Label *monthLabel; property (nonatomic, weak) UILabel *dayLabel; property (nonatomic, assign) NSInteger year; property (nonatomic, assign) NSInteger month; property (nonatomic, assign) NSInteger day; property (nonatomic, assign) NSInteger hour; property (nonatomic, assign) NSInteger minute; property

12、 (nonatomic, assign) NSInteger currentYear; property (nonatomic, assign) NSInteger currentMonth; property (nonatomic, assign) NSInteger currentDay; end implementation HWCalendar - (instancetype)initWithFrame:(CGRect)frame if (self = super initWithFrame:frame) /获取当前时间 self getCurrentDate; /获取数据源 self

13、 getDataSource; /创建控件 self creatControl; /初始化设置 self setDefaultInfo; /刷新数据 self reloadData; return self; - (void)getDataSource _weekArray = 日, 一, 二, 三, 四, 五, 六; _timeArray = 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 01, 02, 03, 04, 05, 06, 07, 08

14、, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59; NSInteger firstYear = _year - KShowYearsCount / 2; NSMutableArray *yearArray = NSMutableArray

15、array; for (int i = 0; i KShowYearsCount; i+) yearArray addObject:NSString stringWithFormat:%ld, firstYear + i; _yearArray = yearArray; _monthArray = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; - (void)setDefaultInfo self.backgroundColor = UIColor whiteColor; _timePicker selectRow:_hour inComponent:0 ani

16、mated:NO; _timePicker selectRow:_minute - 1 inComponent:1 animated:NO; _currentYear = _year; _currentMonth = _month; _currentDay = _day; - (void)creatControl /左侧显示视图 UIView *tipsView = UIView alloc initWithFrame:CGRectMake(0, 0, KTipsW, KBtnH * 8); tipsView.backgroundColor = KMainColor; self addSubv

17、iew:tipsView; /星期标签 UILabel *weekLabel = UILabel alloc initWithFrame:CGRectMake(0, 0, KTipsW, KBtnH); weekLabel.backgroundColor = UIColor colorWithRed:0.0f green:130/255.0f blue:116/255.0f alpha:1.0f; weekLabel.textColor = UIColor whiteColor; weekLabel.textAlignment = NSTextAlignmentCenter; tipsView

18、 addSubview:weekLabel; self.weekLabel = weekLabel; /年份标签 UILabel *yearLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(weekLabel.frame) + 20, KTipsW, KBtnH); yearLabel.textColor = KbackColor; yearLabel.textAlignment = NSTextAlignmentCenter; yearLabel.font = UIFont systemFontOfSize:26.

19、0f; tipsView addSubview:yearLabel; self.yearLabel = yearLabel; /月份标签 UILabel *monthLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(yearLabel.frame), KTipsW, 26); monthLabel.textColor = UIColor whiteColor; monthLabel.textAlignment = NSTextAlignmentCenter; monthLabel.font = UIFont syst

20、emFontOfSize:26.0f; tipsView addSubview:monthLabel; self.monthLabel = monthLabel; /日期标签 UILabel *dayLabel = UILabel alloc initWithFrame:CGRectMake(0, CGRectGetMaxY(monthLabel.frame) + 30, KTipsW, 120); dayLabel.textColor = UIColor whiteColor; dayLabel.textAlignment = NSTextAlignmentCenter; dayLabel.

21、font = UIFont systemFontOfSize:76.0f; tipsView addSubview:dayLabel; self.dayLabel = dayLabel; CGFloat yearBtnW = 70.0f; CGFloat monthbtnW = 60.0f; CGFloat todayBtnW = 70.0f; CGFloat padding = (self.bounds.size.width - KTipsW - yearBtnW - monthbtnW - todayBtnW - KBtnW * 2) * 0.25; /年份按钮 HWOptionButto

22、n *yearBtn = HWOptionButton alloc initWithFrame:CGRectMake(KTipsW + padding, 0, yearBtnW, KBtnH); yearBtn.array = _yearArray; yearBtn.row = KShowYearsCount / 2; yearBtn.delegate = self; self addSubview:yearBtn; self.yearBtn = yearBtn; /上一月 UIButton *preBtn = UIButton alloc initWithFrame:CGRectMake(C

23、GRectGetMaxX(yearBtn.frame) + padding, 0, KBtnW, KBtnH); preBtn setImage:UIImage imageNamed:left forState:UIControlStateNormal; preBtn addTarget:self action:selector(preBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:preBtn; /月份按钮 HWOptionButton *monthBtn = HWOptionButton a

24、lloc initWithFrame:CGRectMake(CGRectGetMaxX(preBtn.frame), 0, monthbtnW, KBtnH); monthBtn.array = _monthArray; monthBtn.row = _month - 1; monthBtn.delegate = self; self addSubview:monthBtn; self.monthBtn = monthBtn; /下一月 UIButton *nextBtn = UIButton alloc initWithFrame:CGRectMake(CGRectGetMaxX(month

25、Btn.frame), 0, KBtnW, KBtnH); nextBtn setImage:UIImage imageNamed:right forState:UIControlStateNormal; nextBtn addTarget:self action:selector(nextBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:nextBtn; /返回今天按钮 UIButton *backTodayBtn = UIButton alloc initWithFrame:CGRectMak

26、e(CGRectGetMaxX(nextBtn.frame) + padding, 0, todayBtnW, KBtnH); backTodayBtn.titleLabel.font = UIFont systemFontOfSize:14.0f; backTodayBtn setTitleColor:UIColor blackColor forState:UIControlStateNormal; backTodayBtn setTitle:返回今天 forState:UIControlStateNormal; backTodayBtn addTarget:self action:sele

27、ctor(backTodayBtnOnClick) forControlEvents:UIControlEventTouchUpInside; self addSubview:backTodayBtn; /星期标签 for (int i = 0; i _weekArray.count; i+) UILabel *week = UILabel alloc initWithFrame:CGRectMake(KTipsW + KBtnH * i, KBtnH, KBtnH, KBtnH); week.textAlignment = NSTextAlignmentCenter; week.text = _weekArrayi; self addSubview:week; /日历核心视图 UIView *calendarView = UIView alloc initWithFrame:CGRectMake(KTipsW, KBtnH * 2, KBtnW * 7, KBtnH * 6); self addSubview:calendarView; self.calendarView = calendarView; /每一个日期用一个按钮去创建,当一个月的第一天是星期六并且有31天时为最多个数,5行零2个,共37个 for (i

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1