Unity調用IOS時間日期控件UIDatePicker

沒什麼好解析的上代碼:

//***************************************日曆、時間
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 打開圖片庫和相機,並加載圖片

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章