嗨,我是 iOS 新手,在我的项目中,我以编程方式在我的主 UIViewController
上添加了两个 UIViews
。
这里我的主要要求是当我单击firstView
中的“Next”按钮时,我想将FirstView
推送到SecondView
和
当我单击 SecondView
中的“后退”按钮时,我想将 secondView
推回 firstView
。
如何将一个 UIViewController
推送到另一个 UIViewController
?
但我想以编程方式使用 UIView
实现同样的要求。
请帮帮我。
我们怎样才能做到这一点?
我的代码:-
#import "ViewController.h"
@interface ViewController ()
{
UIView * FisrtView;
UIView * SecondView;
UIButton * Next;
UIButton * Back;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
FisrtView = [[UIView alloc] init];
FisrtView.backgroundColor=[UIColor lightGrayColor];
FisrtView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:FisrtView];
SecondView = [[UIView alloc] init];
SecondView.backgroundColor=[UIColor orangeColor];
SecondView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:SecondView];
Next = [[UIButton alloc] init];
Next.backgroundColor=[UIColor orangeColor];
Next.translatesAutoresizingMaskIntoConstraints = NO;
[Next setTitle:@"Go Next" forState:UIControlStateNormal];
[Next addTarget:self action:@selector(Next:) forControlEvents:UIControlEventTouchUpInside];
[FisrtView addSubview:Next];
Back = [[UIButton alloc] init];
Back.backgroundColor=[UIColor lightGrayColor];
Back.translatesAutoresizingMaskIntoConstraints = NO;
[Back setTitle:@"Go Back" forState:UIControlStateNormal];
[Back addTarget:self action:@selector(Back:) forControlEvents:UIControlEventTouchUpInside];
[SecondView addSubview:Back];
//Applting Autolayouts for All Fields
NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(FisrtView,SecondView,Next,Back);
//Appliying Autolayouts for FirstView
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[FisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[FisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appying Autolayoust for NextButton
[FisrtView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[Next]-10-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[FisrtView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-100-[Next(30)]"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appliying Autolayouts for secondView
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[SecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[SecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appying Autolayoust for BackButton
[SecondView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[Back]-10-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[SecondView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-100-[Back(30)]"]
options:0
metrics:nil
views:HeaderDictionary]];
SecondView.hidden = YES;
FisrtView.hidden = NO;
}
-(void)Next:(id)sender{
SecondView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //your view start position
[UIView animateWithDuration:0.15f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[SecondView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
}
completion:nil];
SecondView.hidden = NO;
}
-(void)Back:(id)sender{
SecondView.hidden = YES;
FisrtView.hidden = NO;
}
@end
最佳答案
最初的错误,你创建的按钮是错误的,没有将框架设置到可见区域
不喜欢
Next = [[UIButton alloc] init];
喜欢
Next = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Next.frame = CGRectMake(210, 285, 100, 18)];
Next.translatesAutoresizingMaskIntoConstraints = NO;
[Next setTitle:@"Go Next" forState:UIControlStateNormal];
[Next addTarget:self action:@selector(Next:) forControlEvents:UIControlEventTouchUpInside];
[FisrtView addSubview:Next];
并且忘记为 View 中的可见区域添加框架
FisrtView = [[UIView alloc] init];
FisrtView.frame = CGRectMake(210, 285, 100, 18)];
SecondView = [[UIView alloc] init];
SecondView.frame = CGRectMake(210, 285, 100, 18)];
示例动画
-(void)Next:(id)sender{
SecondView.frame=CGRectMake(248, -420, 500, 414); //your view start position
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[SecondView setFrame:CGRectMake(248, 30, 500, 414)]; // final visible position
}
completion:nil];
}
有关示例动画,请参阅此 link
关于ios - 如何使用动画概念将一个 UIview 推送到 ios 中的另一个 UIview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33706790/