嘿我试图将图像视图向下移动到屏幕中间然后返回到顶部。这是我的代码,现在它只是移动到中间并保持在那里。
-(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];
}
答案 0 :(得分:8)
我不喜欢将基于块的动画用于链式动画 - 它们往往难以阅读。我建议使用keyframe animations。这使您可以轻松地为动画定义多个中间点,或者为动画添加更多高级功能,例如:曲线。
以下是如何在三点之间线性地设置UIView
动画的示例。
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
也可以。
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"];
答案 1 :(得分:4)
我建议使用[UIView -animateWithDuration:animations:completion]
:
[UIView animateWithDuration:0.5f
animations:^{
imageView.center = CGPointMake(imageView.center.x, imageView.center.y + 10.0f);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f
animations:^{
imageView.center = CGPointMake(imageView.center.x, imageView.center.y - 10.0f);
} completion:^(BOOL finished) {
}];
}];
除此之外:方法名称不应以大写字母
开头