iOS 简单日历制作

来源:互联网 发布:赵云人气 知乎 编辑:程序博客网 时间:2024/06/06 03:42

#import"ViewController.h"

#import"CollectionViewCell.h"

#definescreenSize [UIScreen mainScreen].bounds.size

@interfaceViewController()<</span>UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView*collectionView;

@property(nonatomic,strong)NSArray*weekdayArray;

@property(nonatomic,strong)NSDate*date;

@property(nonatomic,strong)NSDate*todayDate;

@property(nonatomic,assign)NSIntegermonthDay;

@property(nonatomic,assign)NSIntegermonth;

@property(nonatomic,assign)NSIntegeryear;

@property(nonatomic,assign)NSIntegerfirstWeekdayInThisMonth;

@property(nonatomic,assign)NSIntegertotaldaysInMonth;

@end


@implementationViewController


-(void)viewDidLoad{

   [superviewDidLoad];

   UICollectionViewFlowLayout*flowLayout= [[UICollectionViewFlowLayoutalloc]init];

   flowLayout.itemSize=CGSizeMake(screenSize.width/7,screenSize.height/7);

   flowLayout.minimumInteritemSpacing=0;

   flowLayout.minimumLineSpacing=0;

   flowLayout.sectionInset=UIEdgeInsetsMake(0,0,0,0);

   self.collectionView=[[UICollectionViewalloc]initWithFrame:CGRectMake(0,50,screenSize.width,screenSize.height-50)collectionViewLayout:flowLayout];

   [self.collectionViewregisterNib:[UINibnibWithNibName:@"CollectionViewCell"bundle:nil]forCellWithReuseIdentifier:@"cell"];

   self.collectionView.delegate=self;

   self.collectionView.dataSource=self;

   self.collectionView.backgroundColor=[UIColorwhiteColor];

   

   [self.viewaddSubview:self.collectionView];

   

   self.weekdayArray=@[@"",@"",@"",@"",@"",@"",@""];

   

   self.date=[NSDatedate];

   

}

#pragma日历的方法

-(void)setDate:(NSDate*)date{

   

   self.todayDate=date;

   

   NSDateComponents*dateComp= [[NSCalendarcurrentCalendar]components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay)fromDate:date];

   

   NSCalendar*calendar= [NSCalendarcurrentCalendar];

   calendar.firstWeekday=1;//1.Sun.2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.

   dateComp.day=1;

   NSDate*firstDayOfMonthDate= [calendar dateFromComponents:dateComp];

   NSUIntegerfirstWeekday= [calendar ordinalityOfUnit:NSCalendarUnitWeekdayinUnit:NSCalendarUnitWeekOfMonthforDate:firstDayOfMonthDate];

   

   self.firstWeekdayInThisMonth=firstWeekday -1;

   NSRangedaysInMonth= [calendar rangeOfUnit:NSCalendarUnitDayinUnit:NSCalendarUnitMonthforDate:date];

   self.totaldaysInMonth=daysInMonth.length;

   [self.collectionViewreloadData];

}

#pragmacollectionView的方法

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{

   return2;

}

-(NSInteger)collectionView:(UICollectionView*)collectionViewnumberOfItemsInSection:(NSInteger)section{

   if(section== 0){

      returnself.weekdayArray.count;

   } else{

      return42;

   }

}

-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionViewcellForItemAtIndexPath:(NSIndexPath*)indexPath{

   CollectionViewCell*cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];

   if(indexPath.section==0){

      cell.textLabel.text=self.weekdayArray[indexPath.row];

   } else{

      if(indexPath.row<self.firstWeekdayInThisMonth){

          cell.textLabel.text=@"";

          [cell setUserInteractionEnabled:NO];

       }elseif(indexPath.row>self.totaldaysInMonth+self.firstWeekdayInThisMonth-1){

          cell.textLabel.text=@"";

          [cell setUserInteractionEnabled:NO];

       }else{

          [cell.textLabelsetText:[NSStringstringWithFormat:@"%ld",(long)indexPath.row-self.firstWeekdayInThisMonth+1]];

      }

   }

   

   returncell;

}

-(IBAction)priviousBtn:(id)sender{

   NSDateComponents*dateComp= [NSDateComponentsnew];

   dateComp.month=-1;

   NSDate*newDate= [[NSCalendarcurrentCalendar]dateByAddingComponents:dateComptoDate:self.todayDateoptions:0];

   NSLog(@"%@",newDate);

   [UIViewtransitionWithView:self.viewduration:0.5options:UIViewAnimationOptionTransitionCurlUpanimations:^{

      self.date=newDate;

       

   } completion:nil];

   

}

-(IBAction)nextBtn:(id)sender{

   NSDateComponents*dateComp= [NSDateComponentsnew];

   dateComp.month=1;

   

   NSDate*newDate= [[NSCalendarcurrentCalendar]dateByAddingComponents:dateComptoDate:self.todayDateoptions:0];

   NSLog(@"%@",newDate);

   [UIViewtransitionWithView:self.viewduration:0.5options:UIViewAnimationOptionTransitionCurlDownanimations:^{

      self.date=newDate;

   } completion:nil];

   

   

}


-(void)didReceiveMemoryWarning{

   [superdidReceiveMemoryWarning];

   // Dispose of anyresources that can be recreated.

}


@end

Demo下载:https://pan.baidu.com/s/1jHXVe9C

原创粉丝点击