如何滑动两个 UIView。保持背景不变
Posted
技术标签:
【中文标题】如何滑动两个 UIView。保持背景不变【英文标题】:How to slide two UIView's. Keeping the background unchanged 【发布时间】:2016-10-20 10:17:24 【问题描述】:如何使整个应用程序的背景相同。而不是去另一个ViewController
有一些动画(推,弹出,翻滚等)。 我只想用一些动画改变前景视图。下一个视图应该在另一个 ViewController 中
例如让两个彩色屏幕成为 UIView。单击第一个视图上的按钮时,我想移动 view1 幻灯片 view2。
Anyway the background need to remain unchanged Note: With different view controllers
【问题讨论】:
UINavigationController - Keep Background Image Across All Views的可能重复 【参考方案1】:试试这个代码:
- (IBAction)btnExit:(id)sender
[self setFrame:CGRectMake(-self.frame.size.width
,200
,self.frame.size.width
, self.frame.size.height
)];
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setDuration:.50];
[animation setDelegate:self];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
CALayer *layer = [self layer];
[layer addAnimation:animation forKey:nil];
- (IBAction)btnEnter:(id)sender
[self setFrame:CGRectMake(10
,200
,self.frame.size.width
, self.frame.size.height
)];
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setDuration:.50];
[animation setDelegate:self];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
CALayer *layer = [self layer];
[layer addAnimation:animation forKey:nil];
通过这个简单的代码,我在屏幕内移动(使用btnEnter
)myView
(开始时在屏幕外)使用动画,然后(btnExit
)使用另一个动画移动。
你可以使用很多动画,我选择了一个。
记得在你的类定义中添加CAAnimationDelegate
。
背景总是一样的。 (UIViewController 的视图)
【讨论】:
【参考方案2】:您可以更改动画持续时间 2
[UIView animateWithDuration:1 animations:^
[view1 setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
];
[UIView animateWithDuration:1 animations:^
[view2 setFrame:CGRectMake( view1.frame.size.width,0, self.view.frame.size.width, self.view.frame.size.height)];
];
或
[UIView animateWithDuration:1 animations:^
[view2 setFrame:CGRectMake( view1.frame.size.width,0, self.view.frame.size.width, self.view.frame.size.height)];
[view1 setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
];
【讨论】:
以上是关于如何滑动两个 UIView。保持背景不变的主要内容,如果未能解决你的问题,请参考以下文章