我在X = 0和Y = 0位置有UIView
,高度为110,在屏幕底部有UIButton
,高度为50,我想通过动画来显示,但UIView
必须从顶部到其高度设置动画,并从底部到其高度设置动画UIButton
,我对此是全新的,请帮助我该怎么做。
以下代码对我有用,以便在顶部显示搜索视图。
if (!isSearchActive) {
[UIView animateWithDuration:0.4
delay:0.1
options: UIViewAnimationOptionCurveEaseIn
animations:^{
searchView.frame = CGRectMake(0, 56, mainWidth, 50);
}
completion:^(BOOL finished){
}];
isSearchActive=YES;
}
else{
[UIView animateWithDuration:0.3
delay:0.1
options: UIViewAnimationOptionCurveEaseIn
animations:^{
searchView.frame = CGRectMake(0, 56, mainWidth, 0);
}
completion:^(BOOL finished){
}];
isSearchActive=NO;
}
单击任何按钮以显示和隐藏视图时调用此代码。
尝试从上到下的动画:
- (CAAnimation*)movedown;
{
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.y";
animation.fromValue = @600;
animation.toValue = @50;
animation.duration = 0.25;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[trashView.layer addAnimation:animation forKey:@"basic"];
trashView.layer.position =CGPointMake(0,-20);
return animation;
}
和自下而上的代码:
- (CAAnimation*)moveup;
{
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.y";
animation.fromValue = @-500;
animation.toValue = @10;
animation.duration = 0.25;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[trashView.layer addAnimation:animation forKey:@"basic"];
trashView.layer.position =CGPointMake(0,20);
return animation;
}
对于第一段代码,您可以使用它来制作视图动画,但是必须在代码中的viewcontroller
处替换trashview
。
和按钮的第二块用法,但您必须在trashview
处替换按钮。
但是您可以根据需要设置动画的值。
可能符合您要求的基本动画
[UIView transitionWithView:view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^{
//update your frame here;
}
completion:nil];
private func startAnimation() {
let animation = CABasicAnimation(keyPath: "position.y")
animation.fromValue = 10 //Top Y
animation.toValue = 204 //Bottom Y
animation.duration = 15 * 0.10 //duration * speed
animation.repeatCount = 100
animation.autoreverses = false
animation.isRemovedOnCompletion = true
animation.fillMode = .forwards
self.animateUIView.layer.add(animation, forKey: "position.y")
}