iOS簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼

本文介紹了iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼,分享給大家,具體如下:

創(chuàng)新互聯(lián)公司專注于吳中企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城開(kāi)發(fā)。吳中網(wǎng)站建設(shè)公司,為吳中等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

EBCalendarView日歷控件,調(diào)用簡(jiǎn)單,代碼簡(jiǎn)潔。

github地址:https://github.com/woheduole/EBCalendarView

效果圖

iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼

調(diào)用示例

EBCalendarView *calendarView = [[EBCalendarView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), 0)];
  calendarView.delegate = self;
  //calendarView.maxLastMonths = 0; 
  //calendarView.maxNextMonths = 0;
  [self.view addSubview:calendarView];
- (void)calendarView:(EBCalendarView*)calendarView didSelectedDate:(NSDate*)date {
  NSLog(@"選中日期:%@", [date stringWithFormat:@"yyyy-MM-dd"]);
}

代碼目錄

iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼

思路

EBCalendarView

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[EBCalendarDayCell class] forCellWithReuseIdentifier:kEBCalendarViewReuseIdentifier];

復(fù)制代碼 代碼如下:
_flowLayout.itemSize = CGSizeMake(viewWidth / kEBCalendarViewCellColumn, kEBCalendarViewCellHeight);

通過(guò)UICollectionView控件去顯示日期數(shù)據(jù),設(shè)置UICollectionViewFlowLayout的itemSize,高度可以固定,寬度就是用視圖的總寬度去除以7。

// 小數(shù)向上取整
  NSInteger rows = ceilf(_dates.count / kEBCalendarViewCellColumn);
  self.frame = ({
    CGRect frame = self.frame;
    frame.size.height = kEBCalendarViewWeekViewHeight + kEBCalenderNavigationViewHeight + (rows * kEBCalendarViewCellHeight);
    frame;
  });

切換月份的時(shí)候,由于每月的1號(hào)所在星期是不一致的,會(huì)導(dǎo)致行數(shù)不一樣,比如一個(gè)月是31天,它的1號(hào)是星期日,這時(shí)候日期會(huì)有6行,如果它的1號(hào)是星期一,那么它會(huì)顯示5行,這里會(huì)根據(jù)行數(shù)去動(dòng)態(tài)的改變其高度。

- (NSDate *)dateByAddingMonths:(NSInteger)months {
  NSCalendar *calendar = [NSCalendar currentCalendar];
  NSDateComponents *components = [[NSDateComponents alloc] init];
  [components setMonth:months];
  return [calendar dateByAddingComponents:components toDate:self options:0];
}

月份在累加或累減的時(shí)候,通過(guò)NSCalendar類直接增加月數(shù),這樣就不用自己去處理2018-12點(diǎn)擊下個(gè)月切換到2019-01或者2019-01點(diǎn)擊上個(gè)月切換到2018-12的操作了。

EBCalendarModel 數(shù)據(jù)模型

@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, assign) NSInteger day;
// 記錄選中狀態(tài)
@property (nonatomic, assign, getter=isSelected) BOOL selected;
// 是否為當(dāng)天
@property (nonatomic, assign, getter=isToday) BOOL today;
// 將year,month,day轉(zhuǎn)換成NSDate
@property (nonatomic, strong, readonly) NSDate *date;
- (NSDate*)date {
  if (_year == 0 || _month == 0 || _day == 0) {
    return nil;
  }
  return [NSDate dateWithString:[NSString stringWithFormat:@"%zd-%zd-%zd"
              , _year
              , _month
              , _day] format:@"yyyy-MM-dd"];
}

EBCalenderWeekView 周視圖

- (void)layoutSubviews {
  [super layoutSubviews];
  [self createWeekView];
}

- (void)setWeeks:(NSArray *)weeks {
  _weeks = weeks;
  [self createWeekView];
}

- (void)createWeekView {
  CGFloat viewWidth = CGRectGetWidth(self.bounds)
  , viewHeight = CGRectGetHeight(self.bounds);
  if (_weeks.count == 0 || viewHeight == 0) return;
  [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  NSInteger weekCount = _weeks.count;
  CGFloat weekWidth = viewWidth / weekCount;
  for (int n = 0; n < weekCount; n ++ ) {
    NSString *week = _weeks[n];
    UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(weekWidth * n, 0, weekWidth, viewHeight)];
    weekLabel.font = [UIFont systemFontOfSize:14];
    weekLabel.textColor = [UIColor colorWithHexString:@"333333"];
    weekLabel.textAlignment = NSTextAlignmentCenter;
    weekLabel.text = week;
    [self addSubview:weekLabel];
  }
}

根據(jù)傳入的參數(shù)weeks動(dòng)態(tài)添加UILabel顯示周數(shù)據(jù)。

EBCalenderNavigationView 月份導(dǎo)航視圖

- (void)changeMonthAction:(UIButton*)button {
  BOOL isNextMonth = NO;
  if ([button isEqual:_nextMonthButton]) {
    // 下個(gè)月
    isNextMonth = YES;
  }
  if ([self.delegate respondsToSelector:@selector(calenderNavigationViewDidChangeMonth:isNextMonth:)]) {
    [self.delegate calenderNavigationViewDidChangeMonth:self isNextMonth:isNextMonth];
  }
}

這里面主要就顯示左右箭頭和中間的年月顯示,左右箭頭是兩個(gè)UIButton,在點(diǎn)擊它們的時(shí)候通過(guò)代理把動(dòng)作給傳到EBCalendarView視圖。

UIColor+EBAdd 顏色輔助類

+ (UIColor *)colorWithHexString:(NSString *)hexString {
  NSScanner *scanner = [NSScanner scannerWithString:hexString];
  unsigned hexNum;
  if (![scanner scanHexInt:&hexNum]) return nil;
  return [UIColor colorWithRGBHex:hexNum];
}

+ (UIColor *)colorWithRGBHex:(UInt32)hex {
  int r = (hex >> 16) & 0xFF;
  int g = (hex >> 8) & 0xFF;
  int b = (hex) & 0xFF;
  
  return [UIColor colorWithRed:r / 255.0f
              green:g / 255.0f
              blue:b / 255.0f
              alpha:1.0f];
}

代碼中顏色都是用的16進(jìn)制的顏色值,純屬個(gè)人習(xí)慣。

NSDate+EBAdd 日期輔助類

// 該方法來(lái)源自YYKit
- (NSInteger)year;

// 該方法來(lái)源自YYKit
- (NSInteger)month;

// 該方法來(lái)源自YYKit
- (NSInteger)day;

// 該方法來(lái)源自YYKit
- (NSInteger)weekday;

// 該方法來(lái)源自YYKit
- (BOOL)isToday;

// 當(dāng)前月有多少天
- (NSUInteger)numberOfDaysInMonth;

// 該方法來(lái)源自YYKit
- (NSString *)stringWithFormat:(NSString *)format;

// 該方法來(lái)源自YYKit
- (NSDate *)dateByAddingMonths:(NSInteger)months;

// 該方法來(lái)源自YYKit
+ (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;

小結(jié):UICollectionView很強(qiáng)大的一個(gè)控件,通過(guò)UICollectionViewFlowLayout去重寫(xiě)布局,可以實(shí)現(xiàn)很多酷炫的功能。這里的日歷控件只是設(shè)置了item的寬高,屬于很基礎(chǔ)的使用。其中需要注意兩點(diǎn):1.每個(gè)月的1號(hào)是屬于周幾,然后去設(shè)置它的起始位置;2.每個(gè)月有多少天。app類型不一樣也會(huì)導(dǎo)致日歷控件實(shí)際呈現(xiàn)方式不一樣,基本邏輯都一樣,無(wú)非就是一些細(xì)微的控制。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前題目:iOS簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼
網(wǎng)頁(yè)URL:http://www.muchs.cn/article26/jiohjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、小程序開(kāi)發(fā)、微信小程序、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司