【发布时间】:2016-03-29 14:32:27
【问题描述】:
我在 X=0 和 Y=0 位置有一个 UIView,高度为 110,在屏幕底部有一个 UIButton,高度为 50,我想通过动画显示两者,但 @987654323 @ 必须从顶部动画到它的高度,UIButton 从底部到它的高度,我对此完全陌生,请帮助我怎么做。
【问题讨论】:
标签: ios objective-c uiview uibutton
我在 X=0 和 Y=0 位置有一个 UIView,高度为 110,在屏幕底部有一个 UIButton,高度为 50,我想通过动画显示两者,但 @987654323 @ 必须从顶部动画到它的高度,UIButton 从底部到它的高度,我对此完全陌生,请帮助我怎么做。
【问题讨论】:
标签: ios objective-c 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.yourView.layer.add(animation, forKey: "position.y")
}
private func stopAnimation() {
self.yourView.layer.removeAllAnimations()
}
【讨论】: