Unity调用IOS时间日期控件UIDatePicker

原创 2016年05月31日 16:49:06

没什么好解析的上代码:

//***************************************日历、时间
UIDatePicker* datePicker;
-(void)DP_ShowPicker{
    if (datePicker!=nil) {
        [self DP_removeViews:nil];
    }
    UIViewController *vc =  UnityGetGLViewController();
    
    CGRect toolbarTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216-44, [self GetW], 44);
    CGRect datePickerTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216, [self GetW], 216);
    CGRect darkViewTargetFrame = CGRectMake(0, vc.view.bounds.size.height-216-44, [self GetW], 260);
    
    UIView *darkView = [[UIView alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height, [self GetW], 260)];
    darkView.alpha = 1;
    darkView.backgroundColor = [UIColor whiteColor];
    darkView.tag = 9;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DP_dismissDatePicker:)];
    [darkView addGestureRecognizer:tapGesture];
    [vc.view addSubview:darkView];
    
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height+44, [self GetW], 216)];
    datePicker.tag = 10;
    [datePicker addTarget:self action:@selector(DP_changeDate:) forControlEvents:UIControlEventValueChanged];
    datePicker.datePickerMode = UIDatePickerModeDateAndTime;//日历时间样式,可以是单独的时间或者日历
    [vc.view addSubview:datePicker];
    
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, vc.view.bounds.size.height, [self GetW], 44)];
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleDefault;
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DP_dismissDatePicker:)];
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [vc.view addSubview:toolBar];
    [UIView beginAnimations:@"MoveIn" context:nil];
    toolBar.frame = toolbarTargetFrame;
    
    
    datePicker.frame = datePickerTargetFrame;
    darkView.frame = darkViewTargetFrame;
    
    [UIView commitAnimations];
}


-(CGFloat) GetW
{
    UIViewController *vc = UnityGetGLViewController();
    
    bool IsLandscape;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft ||
       orientation == UIInterfaceOrientationLandscapeRight)
    {
        IsLandscape = true;
    }
    else
    {
        IsLandscape = false;
    }
    
    CGFloat w;
    if(IsLandscape)
    {
        w = vc.view.frame.size.height;
    }
    else
    {
        w = vc.view.frame.size.width;
    }
    
    //NSLog(@"--- System Version: %@", [UIDevice currentDevice].systemVersion);
    NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    if ([[vComp objectAtIndex:0] intValue] >= 8)
    {
        w = vc.view.frame.size.width;
    }
    
    return w;
}

- (void)DP_changeDate:(UIDatePicker *)sender
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd hh:mm"];
    NSString *dateString = [dateFormatter stringFromDate:sender.date];
    NSLog(@"--- DateChangedEvent: %@", dateString);
    UnitySendMessage( [m_pstrObjectName UTF8String], [m_pstrFuncName UTF8String], dateString.UTF8String);
}
-(void) DP_PickerClosed:(UIDatePicker *)sender
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd hh:mm"];
    NSString *dateString = [dateFormatter stringFromDate:sender.date];
    
    NSLog(@"--- DateClosedEvent: %@", dateString);
    UnitySendMessage( [m_pstrObjectName UTF8String], [m_pstrFuncName UTF8String], dateString.UTF8String);
    
    }
- (void)DP_dismissDatePicker:(id)sender
{
     NSLog(@"--- DismissDatPicker");
    UIViewController *vc =  UnityGetGLViewController();
    
    [self DP_PickerClosed:datePicker];
    
    CGRect toolbarTargetFrame = CGRectMake(0, vc.view.bounds.size.height, [self GetW], 44);
    CGRect datePickerTargetFrame = CGRectMake(0, vc.view.bounds.size.height+44, [self GetW], 216);
    CGRect darkViewTargetFrame = CGRectMake(0, vc.view.bounds.size.height, [self GetW], 260);
    
    [UIView beginAnimations:@"MoveOut" context:nil];
    [vc.view viewWithTag:9].frame = darkViewTargetFrame;
    [vc.view viewWithTag:10].frame = datePickerTargetFrame;
    [vc.view viewWithTag:11].frame = toolbarTargetFrame;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DP_removeViews:)];
    [UIView commitAnimations];
}
- (void)DP_removeViews:(id)object
{
     NSLog(@"--- removeViews");
    if(datePicker==nil)
    {
        return;
    }
    
    UIViewController *vc =  UnityGetGLViewController();
    [[vc.view viewWithTag:9] removeFromSuperview];
    [[vc.view viewWithTag:10] removeFromSuperview];
    [[vc.view viewWithTag:11] removeFromSuperview];
}
//*********************

UnitySendMessage( [m_pstrObjectName UTF8String], [m_pstrFuncName UTF8String], dateString.UTF8String);
参数参考文章:Unity Android/IOS 打开图片库和相机,并加载图片

相关文章推荐

unity3d的日历控件

开发了一个日历控件,在unity3dli
  • xtxy
  • xtxy
  • 2014年09月23日 14:00
  • 4906

【Unity】unity 日历制作

关于日历的制作 其实是非常简单的 博主一开始 想的很复杂 后来经过研究发现  核心代码 就一句 day_Temp = DateTime.DaysInMonth (year, month); Dat...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Unity自定义UI组件(六)日历、日期拾取器

前言考虑到工业项目中可能会利用到类似日历的工具,就比如选取某个时间节点,所以我结合UGUI源码开发了日历工具和日期拾取器工具,简单易用,接口齐全,可中文显示,外观可自定义。只需要导入脚本,即可在Hie...

时间选择器:DatePicker 和TimePicker

DatePicker和TimePicker两种实现动态输入日期和时间的功能 DataPickerDialog和TimePickerDialog两种实现动态输入日期和时间的对话框...

Unity Chronos - Time Control 时间控制器

Chronos - Time Control 是一个 Unity 时间控制插件,易于使用,而且为性能做了优化。...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

android基本 UI(四)—时间选择器TimePicker和日期选择器 DatePicker

时间选择器TimePicker,是Android的时间设置控件,继承自android.widget.FrameLayout,在android.widget包中。 TimePicker控件向用户显示时间...

UIDatePicker (时间、日期选择器使用)

#define k_datePicker_height 200 @interface ViewController (){ UIDatePicker *datePickerView;...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Unity调用IOS时间日期控件UIDatePicker
举报原因:
原因补充:

(最多只允许输入30个字)