Ios UIPickerView高度约束动画跳跃
我正在为UIPickerView的高度约束设置动画。 视图跳到一个小高度,仍然显示1行,然后设置动画,直到高度为0Ios UIPickerView高度约束动画跳跃,ios,swift,uipickerview,uiviewanimation,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
UIView.animate(withDuration: 0.5, animations: {
self.timePickerHeightConstraint.constant = self.pickerIsClosed ? 216 : 0
self.view.layoutIfNeeded()
}) { compilation in
self.pickerIsClosed = !self.pickerIsClosed
}
有什么建议吗?
谢谢您应该在动画块之外更改约束常量值
self.timePickerHeightConstraint.constant = self.pickerIsClosed ? 216 : 0
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
}) { compilation in
self.pickerIsClosed = !self.pickerIsClosed
}
另外,请确保不要设置“顶部”和“底部”约束,因为这将不允许“自动布局”将“高度”设置为0点。您应该在动画块之外更改约束常量值
self.timePickerHeightConstraint.constant = self.pickerIsClosed ? 216 : 0
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
}) { compilation in
self.pickerIsClosed = !self.pickerIsClosed
}
另外,请确保不要设置“顶部”和“底部”约束,因为这将不允许“自动布局”将高度设置为0点。尝试类似的操作
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
这对我很有用。试试这样的东西
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
它适合我。选择器组件行的高度是多少。签入
func pickerView(pickerView:UIPickerView,rowHeightForComponent:Int)
当self.pickerIsClosed==true时返回0。选择器组件行的高度是多少。签入
func pickerView(pickerView:UIPickerView,rowHeightForComponent:Int)
当self.pickerIsClosed==true时返回0。我遇到了相同的问题。看起来UIPickerView的高度并没有很好的动画效果,或者至少没有像我希望的那样动画效果 我找到的解决方法是将UIPickerView包装在UIView中,将UIView的clipsToBounds设置为true,然后在UIView包装的高度约束上执行动画,而不是直接使用UIPickerView。需要注意的一点是,要使其工作,UIPicker视图的高度必须独立于包装器视图的高度进行约束,因为否则它的反应将与没有包装器时的反应相同
// create and add UIPicker to view
UIPickerView *picker = [[UIPickerView alloc] init];
picker.translatesAutoresizingMaskIntoConstraints = NO;
picker.delegate = self;
picker.dataSource = self;
picker.alpha = 0.0;
_myUiPicker = picker; // weak reference property
// make wrapper view
UIView *wrapper = [UIView new];
wrapper.translatesAutoresizingMaskIntoConstraints = NO;
wrapper.clipsToBounds = YES;
[wrapper addSubview:picker];
[self.view addSubview:wrapper];
// Picker constraints
[picker.leadingAnchor constraintEqualToAnchor:wrapper.leadingAnchor].active = YES;
[picker.trailingAnchor constraintEqualToAnchor:wrapper.trailingAnchor].active = YES;
// ***** Note this height is not determined by wrapper view ***
[picker.heightAnchor constraintEqualToConstant:130.0].active = YES;
// ****
[picker.topAnchor constraintEqualToAnchor:wrapper.topAnchor].active = YES;
// wrapper constraints
[wrapper.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[wrapper.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
// **** this is the constraint you will animate ***
NSLayoutConstraint *heightConstraint = [wrapper.heightAnchor constraintEqualToConstant:0.0];
heightConstraint.active = YES;
// ****
NSLayoutConstraint *topConstraint = [wrapper.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:8.0];
topConstraint.active = YES;
[self.view layoutIfNeeded]; // initial layout
// animate picker height and reminderBtn position
[UIView animateWithDuration:JAPPickerAnimationTime animations:^{
heightConstraint.constant = 130.0; // from 0 to 130
picker.alpha = 1.0; // make is show
[self.view layoutIfNeeded];
}];
我也有同样的问题。看起来UIPickerView的高度并没有很好的动画效果,或者至少没有像我希望的那样动画效果 我找到的解决方法是将UIPickerView包装在UIView中,将UIView的clipsToBounds设置为true,然后在UIView包装的高度约束上执行动画,而不是直接使用UIPickerView。需要注意的一点是,要使其工作,UIPicker视图的高度必须独立于包装器视图的高度进行约束,因为否则它的反应将与没有包装器时的反应相同
// create and add UIPicker to view
UIPickerView *picker = [[UIPickerView alloc] init];
picker.translatesAutoresizingMaskIntoConstraints = NO;
picker.delegate = self;
picker.dataSource = self;
picker.alpha = 0.0;
_myUiPicker = picker; // weak reference property
// make wrapper view
UIView *wrapper = [UIView new];
wrapper.translatesAutoresizingMaskIntoConstraints = NO;
wrapper.clipsToBounds = YES;
[wrapper addSubview:picker];
[self.view addSubview:wrapper];
// Picker constraints
[picker.leadingAnchor constraintEqualToAnchor:wrapper.leadingAnchor].active = YES;
[picker.trailingAnchor constraintEqualToAnchor:wrapper.trailingAnchor].active = YES;
// ***** Note this height is not determined by wrapper view ***
[picker.heightAnchor constraintEqualToConstant:130.0].active = YES;
// ****
[picker.topAnchor constraintEqualToAnchor:wrapper.topAnchor].active = YES;
// wrapper constraints
[wrapper.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[wrapper.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
// **** this is the constraint you will animate ***
NSLayoutConstraint *heightConstraint = [wrapper.heightAnchor constraintEqualToConstant:0.0];
heightConstraint.active = YES;
// ****
NSLayoutConstraint *topConstraint = [wrapper.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:8.0];
topConstraint.active = YES;
[self.view layoutIfNeeded]; // initial layout
// animate picker height and reminderBtn position
[UIView animateWithDuration:JAPPickerAnimationTime animations:^{
heightConstraint.constant = 130.0; // from 0 to 130
picker.alpha = 1.0; // make is show
[self.view layoutIfNeeded];
}];
谢谢,但同样的问题。您为PickerView设置了哪些约束?尾随、前导、高度、顶部、底部在这种情况下,将高度更改为0将不起作用,因为自动布局引擎需要打破约束。尝试设置垂直和水平中心约束,设置宽度和高度约束,只需设置高度约束的出口并进行更新。根据需要进行更改。谢谢,但问题相同。您为PickerView设置了哪些约束?尾随、前导、高度、顶部、,在这种情况下,将“高度”更改为0将不起作用,因为自动布局引擎需要打破约束。尝试设置垂直和水平中心约束,并设置宽度和高度约束,只需导出高度约束并进行更新。根据需要进行更改。在动画块完成时也重新加载选择器视图。在动画块完成时也重新加载选择器视图。听起来是一个不错的解决方案-可以显示一些代码吗?我不明白你的最后一句话(即独立约束需求)。你能详细说明一下你是如何做到这一点的吗?@iKK我添加了一个代码示例。关于独立高度,我的意思是,设置UIPickerView高度的约束与充当包装器的UIView的高度无关或不相关。听起来是个不错的解决方案-您能展示一些代码吗?我不明白你的最后一句话(即独立约束需求)。你能详细说明一下你是如何做到这一点的吗?@iKK我添加了一个代码示例。关于独立高度,我的意思是,设置UIPickerView高度的约束与充当包装器的UIView的高度无关或不相关。