Iphone 按下按钮后如何设置显示UIPickerView的动画?
我想在按下按钮后设置UIPickerView的动画。我已经将UIPickerView编码为在viewDidLoad上隐藏,而不是在按下按钮后隐藏,但它的动画效果与默认情况下ModalViewController的动画效果不同。我只希望我的UIPickerView像默认情况下的ModalViewController一样设置动画 我已经在网站上和网络上进行了研究,但似乎做得不好 这是我的密码:Iphone 按下按钮后如何设置显示UIPickerView的动画?,iphone,xcode,uipickerview,Iphone,Xcode,Uipickerview,我想在按下按钮后设置UIPickerView的动画。我已经将UIPickerView编码为在viewDidLoad上隐藏,而不是在按下按钮后隐藏,但它的动画效果与默认情况下ModalViewController的动画效果不同。我只希望我的UIPickerView像默认情况下的ModalViewController一样设置动画 我已经在网站上和网络上进行了研究,但似乎做得不好 这是我的密码: #pragma mark - Picker View - (NSInteger)numberOfCom
#pragma mark - Picker View
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 4;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
timersArray = [[NSMutableArray alloc] init];
[timersArray addObject:@"No timer"];
[timersArray addObject:@"15 seconds"];
[timersArray addObject:@"30 seconds"];
[timersArray addObject:@"60 seconds"];
return [timersArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([[timersArray objectAtIndex:row] isEqual:@"No timer"])
{
timerIndication.text = @"No timer selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"15 seconds"])
{
timerIndication.text = @"15 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"30 seconds"])
{
timerIndication.text = @"30 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"60 seconds"])
{
timerIndication.text = @"60 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
}
#pragma mark - Delay method
// This is where Send button should be enabled
- (IBAction)selectTimer
{
timersPickerView.hidden = NO;
// Animation code to present picker view should go here
}
您可以创建viewcontroller,然后在控制器内添加UIPickerView,然后使用: [self-presentModalViewController:ViewControllerWith PickerView animated:YES]
或者,您可以添加未隐藏但y值大于屏幕的UIPickerView,如480.0或更大,然后使用UIView动画将UIPickerView从该位置移动到屏幕上可见的位置,该位置将模拟ModalViewController动画。当您按下按钮时,执行该操作[self.view addSubVuew:yourPickerView];。它是无工作的吗?按下按钮后,可以使用以下代码设置选择器视图的动画:
-(IBAction)button:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
PickerView.transform = transfrom;
PickerView.alpha = PickerView.alpha * (-1) + 1;
[UIView commitAnimations];
}
不要忘记将以下代码添加到viewDidLoad方法中
PickerView.alpha = 0;
[self.view addSubview:PickerView];
它的作用是在第一次单击时使选取器视图从屏幕顶部落下,要使选取器视图消失,只需再次单击按钮即可。从下一次单击开始,选取器视图就会出现并消失。希望它能起到帮助和作用:)您可以使用以下功能来执行选取器视图的动画
-(void)hideShowPickerView {
if (!isPickerDisplay) {
[UIView animateWithDuration:0.25 animations:^{
CGRect temp = self.categoryPicker.frame;
temp.origin.y = self.view.frame.size.height - self.categoryPicker.frame.size.height;
self.categoryPicker.frame = temp;
} completion:^(BOOL finished) {
NSLog(@"picker displayed");
isPickerDisplay = YES;
}];
}else {
[UIView animateWithDuration:0.25 animations:^{
CGRect temp = self.categoryPicker.frame;
temp.origin.y = self.view.frame.size.height;
self.categoryPicker.frame = temp;
} completion:^(BOOL finished) {
NSLog(@"picker hide");
isPickerDisplay = NO;
}];
}
}
将isPickerDisplay定义为BOOL,并将其初始化为ViewDidLoad,如下所示-
isPickerDisplay = YES;
[self hideShowPickerView];
这将管理UIPickerView的隐藏和显示功能。Swift 3解决方案
我同意@EshwarChaitanya,alpha属性是可设置动画的,这是一个合适的用法
override func viewDidLoad() {
super.viewDidLoad()
timersPickerView.alpha = 0
}
@IBAction func selectTimer() {
UIView.animate(withDuration: 0.3, animations: {
self.timersPickerView.alpha = 1
})
}
我不知道你想如何隐藏它,所以我将留给你。Swift 5:向上滑动动画
1-将高度设置为216(PickerView标准)
2-设置引导尾随至安全区域
3-将底部设置为“SuperViewBottom”,设置为-216
4-将第3行的IBOutlet作为NSLayoutConstraint选项
然后:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
bottom.constant = -216
}
@IBOutlet weak var bottom: NSLayoutConstraint!
-(IBAction)button:(id)sender
{
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
{
self.bottom.constant = 0
self.view.layoutIfNeeded()
}) { (AnimationComplete ) in }
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
{
self.bottom.constant = -216
self.view.layoutIfNeeded()
}) { (AnimationComplete ) in }
}
}
导入UIKit
类ViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
bottom.constant=-216
}
@IBMOutlet弱var底部:NSLayoutConstraint!
-(iAction)按钮:(id)发送者
{
UIView.animate(持续时间:0.2,延迟:0.0,选项:.curveEaseIn,动画:
{
self.bottom.constant=0
self.view.layoutifneed()
}){(AnimationComplete)in}
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
UIView.animate(持续时间:0.2,延迟:0.0,选项:.curveEaseIn,动画:
{
self.bottom.constant=-216
self.view.layoutifneed()
}){(AnimationComplete)in}
}
}
它应该像苹果标准动画一样工作
祝你好运,当我加载带有pickerview的modalviewcontroller时,有没有办法使视图变暗?你可以使用黑色背景的隐藏UIView,alpha为0.5,然后听你的UIPickerView动画,当它完成后,你会显示UIView。这个UIView应该放在UIPickerView之外的所有东西之上。事实并非如此。当我按下按钮时,它只会使PickerView出现,因为它在viewDidLoad中默认隐藏。在PickerView上选择一行后,它会再次隐藏。@Andronienn很高兴我的答案有效,感谢您的补充:)