iOS 简单日历制作

本文介绍了如何在iOS中创建一个简单的日历视图。通过使用UICollectionView,展示了一个月的日历布局,包括周几的标题和日期单元格。代码中实现了滑动切换月份的功能,并提供了详细的日期计算方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"

#import "CollectionViewCell.h"

#define screenSize [UIScreen mainScreen].bounds.size

@interface ViewController ()<</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) NSInteger monthDay;

@property (nonatomic, assign) NSInteger month;

@property (nonatomic, assign) NSInteger year;

@property (nonatomic, assign) NSInteger firstWeekdayInThisMonth;

@property (nonatomic, assign) NSInteger totaldaysInMonth;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]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 = [[UICollectionView alloc]initWithFrame:CGRectMake(0,50, screenSize.width, screenSize.height-50) collectionViewLayout:flowLayout];

    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    self.collectionView.backgroundColor = [UIColor whiteColor];

    

    [self.view addSubview:self.collectionView];

    

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

    

    self.date = [NSDate date];

    

}

#pragma 日历的方法

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

    

    self.todayDate = date;

    

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

    

    NSCalendar *calendar = [NSCalendar currentCalendar];

    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];

    NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];

    

    self.firstWeekdayInThisMonth = firstWeekday -1;

    NSRange daysInMonth = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];

    self.totaldaysInMonth = daysInMonth.length;

    [self.collectionView reloadData];

}

#pragma collectionView的方法

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

    return 2;

}

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

    if (section == 0) {

        return self.weekdayArray.count;

    } else {

        return 42;

    }

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(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];

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

            cell.textLabel.text = @"";

            [cell setUserInteractionEnabled:NO];

        } else {

            [cell.textLabel setText:[NSString stringWithFormat:@"%ld",(long)indexPath.row - self.firstWeekdayInThisMonth + 1]];

        }

    }

    

    return cell;

}

- (IBAction)priviousBtn:(id)sender {

    NSDateComponents *dateComp = [NSDateComponents new];

    dateComp.month = -1;

    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComp toDate:self.todayDate options:0];

    NSLog(@"%@",newDate);

    [UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{

        self.date = newDate;

        

    } completion:nil];

    

}

- (IBAction)nextBtn:(id)sender {

    NSDateComponents *dateComp = [NSDateComponents new];

    dateComp.month = 1;

    

    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComp toDate:self.todayDate options:0];

    NSLog(@"%@",newDate);

    [UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{

        self.date = newDate;

    } completion:nil];

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值