我希望像键盘一样在动画上显示日期选择器。有办法实现吗?

到目前为止,这是我的代码,但我仅使容器视图(模拟阴影的视图)产生了怪异的动画。

@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)})
}

最佳答案

由于您的功能用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
            )
        }
    }
}

关于ios - 带有日期选择器上的键盘的动画与Swift iOS一起出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42308566/

10-12 01:58