我有两个UIViews
我想在屏幕上关闭动画。两者都应该从屏幕开始定位。顶部向下动画,向下动画。没有约束,它按预期工作。但是,使用UIViews
在应用启动时可见(带有轻微的动画可见)。但它们会在屏幕上制作动画。
对于顶部UIView
,我有一个前导空间,尾随空间,顶部空间和高度。
对于底部UIView
,我有一个前导空间,尾随空间,底部空间和高度。
我应该如何调整代码和/或约束以获得所需的动画?
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self animateViewsIn];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.topView.frame = CGRectMake(16.0, -self.topView.frame.size.height, self.topView.frame.size.width, self.topView.frame.size.height);
self.bottomView.frame = CGRectMake(16.0, self.view.bounds.size.height, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)animateViewsIn {
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
self.topView.frame = CGRectMake(16.0,
self.topView.frame.size.height/8.0, self.topView.frame.size.width,
self.topView.frame.size.height);
self.bottomView.frame = CGRectMake(16.0, (self.view.bounds.size.height-self.bottomView.frame.size.height)-20.0, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
- (IBAction)animateViewsOut:(id)sender {
CGRect basketTopFrame = self.topView.frame;
basketTopFrame.origin.y = -basketTopFrame.size.height;
CGRect basketBottomFrame = self.bottomView.frame;
basketBottomFrame.origin.y = self.view.bounds.size.height;
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
[self.view layoutIfNeeded];
self.topView.frame = basketTopFrame;
self.bottomView.frame = basketBottomFrame;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
答案 0 :(得分:0)
使用约束时,不应调整帧。您应该修改约束的constant
属性(或者取消一个约束并激活另一个约束)。
以下是从顶部为顶部视图设置动画的示例,假设您在视图@IBOutlet
的顶部约束中添加了一个名为topConstraint
的{{1}}:
在Objective-C中:
topView
在斯威夫特:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.topConstraint.constant = -self.topView.frame.size.height; // or whatever you want
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5 delay:1 options:0 animations:^{
self.topConstraint.constant = 0;
[self.topView layoutIfNeeded];
} completion:nil];
}