[英]Animation like keyboard on datepicker appearing with swift 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)})
}
由於您的功能用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
)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.