Xcode 如何上下移动图像动画 [英] Xcode How to move an image up and down animation
问题描述
嘿,我试图将图像视图向下移动到屏幕中间,然后再回到顶部.这是我的代码,现在它只是移动到中间并停留在那里.
Hey im trying to move an imageview down to the middle of the screen then back up to the top . here is my code , right now it just moves to the middle and stays there .
-(void)lineGoBack
{
//move diagonally down
line.center = CGPointMake(line.center.x,
line.center.y-10);
//start time for first method
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(LineMovement) userInfo:nil repeats:NO];
}
//The LINE
-(void)LineMovement
{
lineMovementOffset = CGPointMake(line.center.x, screenSizeY/2);
//move diagonally up
line.center = CGPointMake(line.center.x,
line.center.y+10);
//start time for second method
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(lineGoBack) userInfo:nil repeats:NO];
}
推荐答案
我不喜欢将基于块的动画用于链式动画 - 它们往往变得难以阅读.我建议使用 关键帧动画.这也使您能够轻松地为动画定义许多中间点,或为动画添加更多高级功能,例如曲线.
I don't like using blocks based animations for chained animations - they tend to grow difficult to read. I would suggest using keyframe animations. This also enables you to easily define a number of intermediary points to your animation, or add more advanced features to your animation, e.g. curves.
这里有一个示例,说明如何在三个点之间线性地为 UIView
设置动画.
Here is an example of how you can animate a UIView
linearly between three points.
CGPoint startPoint = (CGPoint){100.f, 100.f};
CGPoint middlePoint = (CGPoint){400.f, 400.f};
CGPoint endPoint = (CGPoint){600.f, 100.f};
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, middlePoint.x, middlePoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 3.f;
animation.path = thePath;
[self.animatedView.layer addAnimation:animation forKey:@"position"];
self.animatedView.layer.position = endPoint;
要在两点之间制作无限动画,您可以使用以下代码.将它应用到 UIImageView
会起到同样的作用.
To do an infinite animation between two points, you could use the code below. Applying it to a UIImageView
would work the same.
CGPoint startPoint = (CGPoint){100.f, 100.f};
CGPoint endPoint = (CGPoint){400.f, 400.f};
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[self.animatedView.layer addAnimation:animation forKey:@"position"];
这篇关于Xcode 如何上下移动图像动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!