关闭

Unity调用IOS时间日期控件UIDatePicker

946人阅读 评论(0) 收藏 举报
分类:

没什么好解析的上代码:

//***************************************日历、时间
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 打开图片库和相机,并加载图片

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:486881次
    • 积分:5381
    • 等级:
    • 排名:第5015名
    • 原创:40篇
    • 转载:190篇
    • 译文:4篇
    • 评论:37条