gpt4 book ai didi

ios - Xcode 如何上下移动图像动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:13 24 4
gpt4 key购买 nike

嘿,我正在尝试将 ImageView 向下移动到屏幕中间,然后再回到顶部。这是我的代码,现在它只是移动到中间并留在那里。

-(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];
}

最佳答案

我不喜欢将基于 block 的动画用于链式动画——它们往往变得难以阅读。我建议使用 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"];

关于ios - Xcode 如何上下移动图像动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22425910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com