类似于快速ios出现的日期选择器上的键盘的动画

时间:2017-02-17 22:37:37

标签: ios swift animation uidatepicker

我希望像键盘一样在动画上显示日期选择器。这是实现它的一种方式吗?

到目前为止,这是我的代码,但我只获得容器视图(模拟阴影的视图)制作一个奇怪的动画。

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

1 个答案:

答案 0 :(得分: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
            )
        }
    }
}