最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS自定義日歷控件的簡單實現過程

 更新時間:2021年08月25日 14:22:10   作者:小姐貧僧光天化日  
這篇文章主要為大家詳細介紹了iOS自定義日歷控件的簡單實現過程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

因為程序要求要插入一個日歷控件,該空間的要求是從當天開始及以后的六個月內的日歷,上網查資料基本上都說只要獲取兩個條件(當月第一天周幾和本月一共有多少天)就可以實現一個簡單的日歷,剩下的靠自己的簡單邏輯就OK了,下面開始自己從開始到完成的整個過程:

1.首先做NSDate類目,擴展一些方法讓日期之間轉換更加方便

#import <Foundation/Foundation.h>

@interface NSDate (LYWCalendar)

#pragma mark - 獲取日
- (NSInteger)day:(NSDate *)date;
#pragma mark - 獲取月
- (NSInteger)month:(NSDate *)date;
#pragma mark - 獲取年
- (NSInteger)year:(NSDate *)date;
#pragma mark - 獲取當月第一天周幾
- (NSInteger)firstWeekdayInThisMonth:(NSDate *)date;
#pragma mark - 獲取當前月有多少天
- (NSInteger)totaldaysInMonth:(NSDate *)date;

@end

下面一一實現這些方法

#import "NSDate+LYWCalendar.h"

@implementation NSDate (LYWCalendar)

/**
 *實現部分
 */
#pragma mark -- 獲取日
- (NSInteger)day:(NSDate *)date{
 NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
 return components.day;
}

#pragma mark -- 獲取月
- (NSInteger)month:(NSDate *)date{
 NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
 return components.month;
}

#pragma mark -- 獲取年
- (NSInteger)year:(NSDate *)date{
 NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
 return components.year;
}

#pragma mark -- 獲得當前月份第一天星期幾
- (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
 NSCalendar *calendar = [NSCalendar currentCalendar];
 //設置每周的第一天從周幾開始,默認為1,從周日開始
 [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
 NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
 [comp setDay:1];
 NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
 NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
 //若設置從周日開始算起則需要減一,若從周一開始算起則不需要減
 return firstWeekday - 1;
}
#pragma mark -- 獲取當前月共有多少天

- (NSInteger)totaldaysInMonth:(NSDate *)date{
 NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
 return daysInLastMonth.length;
}

接下來就要寫邏輯部分了,在ViewController里面實現,先說思想,首先想到的是用collectionView,但是每個月天數不一樣在日歷中的顯示就不一樣,有時候有五行有時候有六行,既然要用自動填充就必須考慮到這一點,下面是代碼實現

#import "ViewController.h"
#import "Macro.h"
#import "NSDate+LYWCalendar.h"
#import "LYWCollectionViewCell.h"
#import "LYWCollectionReusableView.h"

定義一些全局變量

static NSString *cellID = @"cellID";
static NSString *headerID = @"headerID";
static NSString *footerID = @"footerID";

@implementation ViewController
{
 //自動布局
 UICollectionViewFlowLayout *_layout;
 //表格視圖
 UICollectionView *_collectionView;
 //當月第一天星期幾
 NSInteger firstDayInMounthInWeekly;
 NSMutableArray *_firstMounth;
 //容納六個數組的數組
 NSMutableArray *_sixArray;
 
}
//定義星期視圖,若為周末則字體顏色為綠色
 self.automaticallyAdjustsScrollViewInsets = NO;//關閉自動適應
 NSArray *weekTitleArray = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
 for (int i = 0; i < weekTitleArray.count; i++) {
 UILabel *weekTitleLable = [[UILabel alloc]initWithFrame:CGRectMake(i * ((ScreenWidth/(weekTitleArray.count))), 64, ScreenWidth/(weekTitleArray.count ), 30)];
 if (i == 0 || i == 6) {
  weekTitleLable.textColor = [UIColor greenColor];
 }else{
  weekTitleLable.textColor = [UIColor blackColor];
 }
 weekTitleLable.text = [weekTitleArray objectAtIndex:i];
 weekTitleLable.textAlignment = NSTextAlignmentCenter;
 [self.view addSubview:weekTitleLable];
}
//設置collectionView及自動布局,代理方法尤為重要
 _layout = [[UICollectionViewFlowLayout alloc]init];
 //頭部始終在頂端
 _layout.sectionHeadersPinToVisibleBounds = YES;
 //頭部視圖高度
 _layout.headerReferenceSize = CGSizeMake(414, 40);
 _layout.minimumLineSpacing = 0;
 _layout.minimumInteritemSpacing = 0;
 _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + 30, ScreenWidth, ScreenHeight - 64 - 30) collectionViewLayout:_layout];
 _collectionView.backgroundColor = [UIColor whiteColor];
 //注冊表格
 [_collectionView registerClass:[LYWCollectionViewCell class] forCellWithReuseIdentifier:cellID];
 //注冊頭視圖
 [_collectionView registerClass:[LYWCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
 //注冊尾視圖
// [_collectionView registerClass:[UICollectionReusableView class] forCellWithReuseIdentifier:footerID];
   _collectionView.delegate = self;
 _collectionView.dataSource = self;
 [self.view addSubview:_collectionView];

邏輯部分,這里有個比較長的三項表達式

(daysInMounth > 29 && (firstDayInThisMounth == 6 || firstDayInThisMounth ==5) ? 42 : 35)

就是日歷到底是六行還是七行,這就要根據日歷的特性來判斷了,如果當月天數大于29天并且當月第一天星期六(以這個程序的準則)或者星期天是返回六行剩下的返回三行,也有可能返回四行的,但是就這個程序來說是不可能的也就不需要做判斷了

 //NumberMounthes 為宏定義,表示要顯示月的個數,程序要求是六個月,所以宏定義為六
  //#define NumberMounthes 6 //想要展示的月數

//創(chuàng)建六個數組,并將這六個數組裝入大數組中
 _sixArray = [[NSMutableArray alloc]init];
 for (int i = 0; i < NumberMounthes ; i++ ) {
 NSMutableArray *array = [[NSMutableArray alloc]init];
 [_sixArray addObject:array];
 }
 //為六個數組寫入每個月的日歷信息
 for (int i = 0 ; i < NumberMounthes; i++) {
 //獲取月份
 int mounth = ((int)[currentDate month:currentDate] + i)%12;
 NSDateComponents *components = [[NSDateComponents alloc]init];
 //獲取下個月的年月日信息,并將其轉為date
 components.month = mounth;
 components.year = 2016 + mounth/12;
 components.day = 1;
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSDate *nextDate = [calendar dateFromComponents:components];
 //獲取該月第一天星期幾
 NSInteger firstDayInThisMounth = [nextDate firstWeekdayInThisMonth:nextDate];
 //該月的有多少天daysInThisMounth
 NSInteger daysInThisMounth = [nextDate totaldaysInMonth:nextDate];
 NSString *string = [[NSString alloc]init];
 for (int j = 0; j < (daysInMounth > 29 && (firstDayInThisMounth == 6 || firstDayInThisMounth ==5) ? 42 : 35) ; j++) {
  if (j < firstDayInThisMounth || j > daysInThisMounth + firstDayInThisMounth - 1) {
  string = @"";
  [[_sixArray objectAtIndex:i]addObject:string];
  }else{
  string = [NSString stringWithFormat:@"%ld",j - firstDayInThisMounth + 1];
  [[_sixArray objectAtIndex:i]addObject:string];
  }
 }
 }

下面是代理方法

//這兩個不用說,返回cell個數及section個數
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 return [[_sixArray objectAtIndex:section] count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
 return _sixArray.count;
}
//這里是自定義cell,非常簡單的自定義
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
 LYWCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
 UIView *blackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
 blackgroundView.backgroundColor = [UIColor yellowColor];
 cell.dateLable.text = [[_sixArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
 NSDate *date = [[NSDate alloc]init];
 NSInteger day = [date day:date];
  //設置單擊后的顏色
   cell.selectedBackgroundView = blackgroundView;
 return cell;
}

自定義cell  .h

#import <UIKit/UIKit.h>

@interface LYWCollectionViewCell : UICollectionViewCell

@property (nonatomic,strong) UILabel *dateLable;

- (instancetype)initWithFrame:(CGRect)frame;

@end

.m

#import "LYWCollectionViewCell.h"

@implementation LYWCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
 if (self == [super initWithFrame:frame]) {
 _dateLable = [[UILabel alloc] initWithFrame:self.bounds];
 [_dateLable setTextAlignment:NSTextAlignmentCenter];
 [_dateLable setFont:[UIFont systemFontOfSize:17]];
 _dateLable.textColor = [UIColor blackColor];
 [self addSubview:_dateLable];
 }
 return self;
}
@end

接著代理

//cell大小及間距
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
 return CGSizeMake(ScreenWidth/7, ScreenWidth/7);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
 return UIEdgeInsetsMake(0, 0, 0, 0);
}

既然有日歷,總得顯示哪年哪月吧,前面已經注冊表頭視圖了,這里只需要實現以下代理方法即可

collectionView有點不同其頭視圖也有單獨的類,和cell一樣先自定義headCell,也是非常簡單的自定義

.h文件

#import <UIKit/UIKit.h>

@interface LYWCollectionReusableView : UICollectionReusableView

@property (nonatomic,strong) UILabel *dateLable;

- (instancetype)initWithFrame:(CGRect)frame;

@end

.m文件

#import "LYWCollectionReusableView.h"

@implementation LYWCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame{
 if (self == [super initWithFrame:frame]) {
 _dateLable = [[UILabel alloc] initWithFrame:self.bounds];
 [_dateLable setTextAlignment:NSTextAlignmentLeft];
 [_dateLable setFont:[UIFont systemFontOfSize:20]];
 _dateLable.textColor = [UIColor blackColor];
 [self addSubview:_dateLable];
 }
 return self;
}

@end

接著代理方法,這里也有個三項判斷式,和上面的大同小異,主要是防止12月顯示為0月

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
 if (kind == UICollectionElementKindSectionHeader) {
 LYWCollectionReusableView *headerRV = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
 //自定義藍色
 headerRV.backgroundColor = DODGER_BLUE;
 NSDate *currentDate = [[NSDate alloc]init];
 NSInteger year = ([currentDate month:currentDate] + indexPath.section)/12 + 2016;
 NSInteger mounth = ([currentDate month:currentDate] + indexPath.section) % 12 == 0 ? 12 : ([currentDate month:currentDate] + indexPath.section)%12;
 headerRV.dateLable.text = [NSString stringWithFormat:@"%ld年%ld月",year,mounth];
 return headerRV;
 }else{
 return nil;
 }
}

還是代理,處理選中效果,選中的為黃色

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

 LYWCollectionViewCell *cell = [self collectionView:_collectionView cellForItemAtIndexPath:indexPath];

 NSDate *currentDate = [[NSDate alloc]init];

 //打印當前日期
 if (![cell.dateLable.text isEqualToString:@""]) {
 NSInteger year = ([currentDate month:currentDate] + indexPath.section)/12 + 2016;
 NSInteger mounth = ([currentDate month:currentDate] + indexPath.section)%12;
 NSInteger day = [cell.dateLable.text intValue];
 NSLog(@"%ld年%02ld月%02ld日",year,mounth,day);

 }

 //排除空值cell
 //獲取月份
 NSInteger mounth = ([currentDate month:currentDate] + indexPath.section) % 12 == 0 ? 12 : ([currentDate month:currentDate] + indexPath.section)%12;
 NSDateComponents *components = [[NSDateComponents alloc]init];
 components.month = mounth;
 components.year = 2016 + mounth/12;
 components.day = 1;
 NSCalendar *calendar = [NSCalendar currentCalendar];
 NSDate *nextDate = [calendar dateFromComponents:components];

 //獲取該月第一天星期幾
 NSInteger firstDayInThisMounth = [nextDate firstWeekdayInThisMonth:nextDate];
 //該月的有多少天daysInThisMounth
 NSInteger daysInThisMounth = [nextDate totaldaysInMonth:nextDate];
 if ((indexPath.row < firstDayInThisMounth || indexPath.row > daysInThisMounth + firstDayInThisMounth - 1)){
 //如果點擊空表格則單擊無效
 [collectionView cellForItemAtIndexPath:indexPath].userInteractionEnabled = NO;
 [collectionView reloadData];

 }

}

最后展示很爛的效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iOS實現滾動字幕的動畫特效

    iOS實現滾動字幕的動畫特效

    這篇文章給大家?guī)硪豢顟梅浅嵱玫目丶?,滾動字幕,可以應用在新聞、財經、聊天等各類APP上,B格瞬間提升了一個檔次有木有,下面跟著小編一起看看如何實現的吧。
    2016-09-09
  • iOS實現短信驗證碼倒計時

    iOS實現短信驗證碼倒計時

    這篇文章主要介紹了iOS實現短信驗證碼倒計時功能,一種方法是利用NSTimer計時器,另一種方法是利用GCD實現短信驗證碼倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 淺談iOS開發(fā)中static變量的三大作用

    淺談iOS開發(fā)中static變量的三大作用

    下面小編就為大家?guī)硪黄獪\談iOS開發(fā)中static變量的三大作用。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • iOS開發(fā)中不合法的網絡請求地址如何解決

    iOS開發(fā)中不合法的網絡請求地址如何解決

    這篇文章主要介紹了iOS開發(fā)中不合法的網絡請求地址的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • iOS系統(tǒng)和微信中不支持audio自動播放問題的解決方法

    iOS系統(tǒng)和微信中不支持audio自動播放問題的解決方法

    最近在微信端開發(fā)H5的時候,audio標簽在蘋果機上無法進行自動播放,查找相關資料終于解決了,所以下面這篇文章主要給大家介紹了關于iOS系統(tǒng)和微信中不支持audio自動播放問題的解決方法,需要的朋友可以參考下。
    2017-09-09
  • IOS 文件讀寫操作詳解及簡單實例

    IOS 文件讀寫操作詳解及簡單實例

    這篇文章主要介紹了IOS 文件讀寫操作詳解及簡單實例的相關資料,需要的朋友可以參考下
    2017-04-04
  • Android NavigationController 右滑手勢詳解

    Android NavigationController 右滑手勢詳解

    目前蘋果手機在人機交互中盡力做到極致,在ios7中,新增了一個小小功能,用戶不用點擊右上角的返回按鈕,在屏幕左邊一滑,就會返回。下面給大家詳解Android NavigationController 右滑手勢,需要的朋友可以參考下
    2015-08-08
  • iOS CoreData 增刪改查詳解

    iOS CoreData 增刪改查詳解

    這篇文章主要為大家詳細介紹了iOS CoreData 增刪改查的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用

    Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用

    這篇文章主要介紹了Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用,重點講解了EGOCache對緩存過期時間的檢測及處理,需要的朋友可以參考下
    2016-05-05
  • iOS開發(fā)中的幾個手勢操作實例分享

    iOS開發(fā)中的幾個手勢操作實例分享

    這篇文章主要介紹了iOS開發(fā)中的幾個手勢操作實例分享,編寫代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-09-09

最新評論

铜鼓县| 通江县| 蕉岭县| 汝南县| 三原县| 长垣县| 吴忠市| 闵行区| 丰原市| 共和县| 西充县| 盐边县| 易门县| 县级市| 余姚市| 和硕县| 利津县| 土默特右旗| 平凉市| 鹰潭市| 武山县| 龙口市| 德化县| 赫章县| 包头市| 甘泉县| 连平县| 曲松县| 和田市| 定襄县| 寻乌县| 高雄市| 巴马| 平度市| 凤翔县| 民勤县| 平和县| 兴海县| 郑州市| 延吉市| 板桥市|