2017-02-17 21 views
0

我希望出現在動畫上的日期選擇器就像鍵盤一樣。 Theres實現它的一種方式?像日期選擇器上的鍵盤上的動畫出現與迅速ios

到目前爲止,這是我的代碼,但我只得到容器視圖(模擬陰影的那個)製作一個奇怪的動畫。

@IBAction func ShowPicker(sender: AnyObject) { 

    let ContainerHeight = CGFloat(264) //320, 216 
    let PickerHeight = CGFloat() 
    //container for datepicker and simulates a shadow 
    shadowView = UIView() 
    //Use with transitionWithView 
    //shadowView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height) 
    //Use with animateWithDuration 
    shadowView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5) 
    let tap = UITapGestureRecognizer(target: self, action: #selector(touchOutSide)) 

    shadowView.addGestureRecognizer(tap) 

    let datePicker : UIDatePicker = UIDatePicker() 
    let datePickerContainer = UIView() 

    //datePickerContainer.frame = CGRectMake(0.0, self.view.frame.width, self.view.frame.width, 320.0) 
    datePickerContainer.frame = CGRectMake(0.0, self.view.frame.height - ContainerHeight, self.view.frame.width, ContainerHeight) 

    //Nota ese 320 debe ser la altura natural del picker mas la altura de los botones 

    datePickerContainer.backgroundColor = UIColor.whiteColor() 

    let pickerSize : CGSize = datePicker.sizeThatFits(CGSizeZero) 
    //datePicker.frame = CGRectMake(0.0, 0.0, self.view.frame.width, 216.0) 
    datePicker.frame = CGRectMake(0.0, 0.0, self.view.frame.width, ContainerHeight) 
    //datePicker.clipsToBounds = true 
    datePicker.setDate(NSDate(), animated: true) 
    datePicker.maximumDate = NSDate() 
    //datePicker.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 0.5) 
    datePicker.datePickerMode = UIDatePickerMode.DateAndTime 
    datePicker.addTarget(self, action: "dateChangedInDate:", forControlEvents: UIControlEvents.ValueChanged) 
    //datePickerContainer.addSubview(datePicker) 

    let pickerHolder = UIView() 
    pickerHolder.frame = CGRectMake(0.0, 20, self.view.frame.width, ContainerHeight) 
    pickerHolder.addSubview(datePicker) 

    datePickerContainer.addSubview(pickerHolder) 

    let doneButton = UIButton() 
    doneButton.setTitle("Ok", forState: UIControlState.Normal) 
    doneButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) 
    doneButton.backgroundColor = UIColor.lightGrayColor() 
    doneButton.addTarget(self, action: Selector("dismissPicker:"), forControlEvents: UIControlEvents.TouchUpInside) 
    doneButton.frame = CGRectMake(self.view.frame.width/2, 0.0, self.view.frame.width/2, 37.0) 

    let btnCancel = UIButton() 
    btnCancel.setTitle("Cancel", forState: UIControlState.Normal) 
    btnCancel.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) 
    btnCancel.backgroundColor = UIColor.lightGrayColor() 
    btnCancel.addTarget(self, action: Selector("cancelTap"), forControlEvents: UIControlEvents.TouchUpInside) 
    btnCancel.frame = CGRectMake(0.0, 0.0, self.view.frame.width/2, 37.0) 

    datePickerContainer.addSubview(doneButton) 
    datePickerContainer.addSubview(btnCancel) 

    shadowView.addSubview(datePickerContainer) 
    // Makes weird animations: 
    //UIView.transitionWithView(self.view, duration: 0.9, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {self.view.addSubview(self.shadowView)}, completion: nil) 

    self.view.addSubview(shadowView) 

    UIView.animateWithDuration(2, animations: {() -> Void in self.shadowView.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: ContainerHeight)}) 
} 

回答

0

由於您的函數用IBOutlet表示,我懷疑您使用的是Interface Builder(IB)。我會考慮用IB來構建你的日期選擇器,容器和按鈕,這樣你也可以設計它們。您的日期選擇器和相關視圖的生命週期當前被限定在ShowPicker函數中,但您希望它們超出該函數的範圍,通常它們存在於您的視圖控制器或它的一個子視圖中。以下是一些示例代碼,您可以從中開始並修飾您的具體內容。

import UIKit 

class ViewController: UIViewController { 

    var button: UIButton! 
    let datePicker = UIDatePicker() 
    var isDatePickerShowing = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     button = UIButton(type: .system) 
     button.setTitle("Toggle Date Picker", for: .normal) 
     button.frame = CGRect(x: 10, y: 60, width: 200, height: 32) 
     button.addTarget(self, action:#selector (self.tapped), for: .touchUpInside) 
     view.addSubview(button) 


     let dpSize = datePicker.bounds.size 
     let size = view.bounds.size 
     datePicker.frame = CGRect(
      x: (size.width - dpSize.width)/2.0, 
      y: size.height, 
      width: dpSize.width, 
      height: dpSize.height 
     ) 
     view.addSubview(datePicker) 
    } 

    func tapped(sender: AnyObject) { 
     let dpSize = datePicker.bounds.size 
     let size = view.bounds.size 
     let y = isDatePickerShowing ? size.height : size.height - dpSize.height 
     isDatePickerShowing = !isDatePickerShowing 
     UIView.animate(withDuration: 0.75) { 
      self.datePicker.frame = CGRect(
       x: (size.width - dpSize.width)/2.0, 
       y: y, 
       width: dpSize.width, 
       height: dpSize.height 
      ) 
     } 
    } 
}