UIPickerView高度约束动画跳转

时间:2016-10-13 12:02:18

标签: ios swift uipickerview uiviewanimation

我正在制作UIPickerView的高度限制。 视图跳转到一个小高度,仍然显示1行,然后动画直到高度为0.

UIView.animate(withDuration: 0.5, animations: {
        self.timePickerHeightConstraint.constant =    self.pickerIsClosed ? 216 : 0
        self.view.layoutIfNeeded()
    }) { compilation in
        self.pickerIsClosed = !self.pickerIsClosed
    }

有什么建议吗? 唐卡

4 个答案:

答案 0 :(得分:1)

您应该在动画块之外更改约束常量值

self.timePickerHeightConstraint.constant =    self.pickerIsClosed ? 216 : 0

UIView.animate(withDuration: 0.5, animations: {
    self.view.layoutIfNeeded()
}) { compilation in
    self.pickerIsClosed = !self.pickerIsClosed
}

另外,请确保不设置“上限”和“下限”约束,因为不允许自动布局将高度设置为0磅。

答案 1 :(得分:1)

尝试这样的事情,

func showPickerView(_ animated: Bool) {
    weak var weakSelf = self
    UIView.animate(withDuration: (animated ? kPickerView_AppearanceAnimationDuration : 0.0), delay: (animated ? kPickerView_AppearanceAnimationDelay : 0.0), options: (animations as! UIViewAnimationOptionCurveEaseInOut), {() -> Void in
        weakSelf!.pickerViewContainerView.transform = CGAffineTransform(translationX: 0, y: 0)
    }, completion: {(finished: Bool) -> Void in
        weakSelf!.view.layoutIfNeeded()
    })
}

func hidePickerView(_ animated: Bool) {
    weak var weakSelf = self
    UIView.animate(withDuration: (animated ? kPickerView_DisappearanceAnimationDuration : 0.0), delay: (animated ? kPickerView_DisappearanceAnimationDelay : 0.0), options: (animations as! UIViewAnimationOptionCurveEaseInOut), {() -> Void in
        weakSelf!.pickerViewContainerView.transform = CGAffineTransform(translationX: 0, y: kPickerView_Height)
    }, completion: {(finished: Bool) -> Void in

它对我有用。

答案 2 :(得分:0)

拾取器组件行的高度是多少。签入

  

func pickerView(pickerView:UIPickerView,rowHeightForComponent组件:Int)

当self.pickerIsClosed == true时返回0。

答案 3 :(得分:-1)

我遇到了同样的问题。看起来UIPickerView的高度并没有真正有效,或者至少以我希望的方式生成。

我找到的解决方法是将UIPickerView包装在UIView中,将UIView的clipsToBounds设置为true,然后在UIView包装器的高度约束上执行动画,而不是直接使用UIPickerView。需要注意的一点是,要使其工作,UIPicker视图的高度必须独立于包装器视图的高度进行约束,否则它将以与没有包装器相同的方式作出反应。