如何在iOS中使用animation概念将一个UIview推送到另一个UIview

嗨,我是非常初学者在iOS和我的项目中,我已经在我的主UIViewController上编程添加了两个UIViews ,在这里我的主要要求是当我点击“下一步”button,在我的firstView ,我想推FirstViewSecondView ,点击“返回”button哪里有在SecondView我想pushBack secondViewfirstView就像如何推一个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 

最初的错误,你创build的button是错误的,并没有把框架设置为可见区域

不喜欢

 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)]; 

为示例animation

 -(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]; } 

对于示例animation,请参阅此链接

请参阅下面的链接。 希望这会帮助你。

http://googleweblight.com/?lite_url=https://stackoverflow.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