如何使用动画概念将一个 UIview 推送到 ios 中的另一个 UIview?
Posted
技术标签:
【中文标题】如何使用动画概念将一个 UIview 推送到 ios 中的另一个 UIview?【英文标题】:How to Push one UIview to another UIview in ios using Animation concept? 【发布时间】:2015-11-14 08:58:24 【问题描述】:您好,我是 ios 的新手,在我的项目中,我以编程方式在我的主要 UIViewController
上添加了两个 UIViews
。
这里我的主要要求是当我单击firstView
中的“下一步”按钮时,我想将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
【问题讨论】:
你遇到了什么错误 嗨,kartik,我没有遇到任何错误,我想将一个 UIview 推送到另一个 UIview,就像我们将一个 UIviewcontroller 推送到另一个 UIviewcontroller 一样 【参考方案1】:最初的错误,你创建的按钮是错误的,并且没有将框架设置为可见区域
不喜欢
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];
忘记为视图中的可见区域添加框架
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
【讨论】:
但是您的编码是正确的,查看我们不会推送您的概念,仅隐藏和显示 嗨 kartik 我想你无法理解我的要求是我想将一个 UIview 推送到另一个 UIview,就像我们将一个 UIviewcontroller 推送到另一个 UIviewcontroller 一样 在我们的 ipad 上看到你点击设置按钮然后你点击通知按钮 threre 显然将 UIview 推送到另一个 UIview 那么这里怎么不可能? @Krish -- 兄弟在单个视图控制器的 UIView 中,我们不能使用将一个 UIviewcontroller 推送到另一个 UIviewcontroller 概念,在这个地方我们可以显示和隐藏使用一些延迟的动画 这就是我所期待的如何使用延迟动画请帮助我兄弟【参考方案2】:请参考以下链接。 希望这会对你有所帮助。
http://googleweblight.com/?lite_url=https://***.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app&ei=Qc-ODruI&lc=en-IN&s=1&m=775&ts=1447514312&sig=APONPFlwklDRNHSybRHp3xVcrDKmQUeRjg
【讨论】:
以上是关于如何使用动画概念将一个 UIview 推送到 ios 中的另一个 UIview?的主要内容,如果未能解决你的问题,请参考以下文章
将 UIViewController 推送到 UINavigationController 时动画挂起
如何使用 Google Cloud Messaging 将通知推送到 iOS
内存优化 ios 应用程序,在将应用程序推送到后台最小化应用程序运行时内存大小时