ios日历视图实现日期输入

在视图控制器上,触摸textfield,打开的不是虚拟键盘,也不是datepicker,也不要actionsheet,要一个类似html上的日历输入框。
这类控件有很多开源的,但目标不是我想要的。参考kal,自己实现了一个这类功能的日历视图。

日历视图以自定义视图的方式实现,再add到视图控制器的view上,最后通过日历视图的delegate将自身remove掉。


在textFiled的- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField的方法中增加一个操作,打开日历输入视图。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    GTMLoggerDebug(@"textField tag is %d", textField.tag);

    // [self performSegueWithIdentifier:@"calendarViewCtl" sender:self];
    [self testCalView];

    return NO;

}


日历视图所需要的日期数据,通过一个方法获取,主要还是从系统自带的NSCalendar上获取。

- (void)testCalView
{
    self.logicDao = [[XYCalendarDao alloc] initForDate:[NSDate date]];

    calView = [[XYCalView alloc] initWithFrame:self.view.frame logicDao:self.logicDao];
    calView.delegate = self;

    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromTop;
    transition.duration = 0.6f;
    transition.fillMode = kCAFillModeForwards;
    transition.removedOnCompletion = YES;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [calView.layer addAnimation:transition forKey:@"transition"];

    NSLog(@"calView is %@", NSStringFromCGRect(calView.frame));

    [self.view addSubview:calView];
}

在日历视图类上,调用它的delegate方法,关闭自己。

这是一个XYCalView的delegate的方法。

- (void)slideOutCalView:(XYCalDate *)selectedDate
{
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        calView.frame = CGRectMake(0, calView.frame.size.height, calView.frame.size.width, calView.frame.size.height);
    } completion:^(BOOL finished) {
        [calView removeFromSuperview];
    }];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strDate = [dateFormatter stringFromDate:[selectedDate NSDate]];

    self.testCalendar.text = strDate;
}

在这里,我用两种方式实现视图的动画效果,分别是CATransition和 UIView的animation。


效果图





原文地址:https://www.cnblogs.com/dyllove98/p/3174603.html