Bep*_*o66 5 core-animation objective-c cabasicanimation ios
如何在翻译过程中使用一组图像(即在img1和img2之间切换)更改myLayer.contents?谢谢!
UIImage *myImage = [UIImage imageNamed:@"img1.png"];
CALayer *myLayer = [CALayer layer];
myLayer.contents = (id)myImage.CGImage;
myLayer.Position = CGPointMake(0,0);
myLayer.Bounds=CGRectMake(0.0, 0.0, 50, 50);
[self.view.layer addSublayer:myLayer];
//translation1
CGPoint startPt = CGPointMake(10,10);
CGPoint endPt = CGPointMake(100,100);
CABasicAnimation *transl1 = [CABasicAnimation animationWithKeyPath:@"position"];
transl1.removedOnCompletion = FALSE;
transl1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transl1.fromValue = [NSValue valueWithCGPoint:startPt];
transl1.toValue = [NSValue valueWithCGPoint:endPt];
transl1.duration = 3;
transl1.fillMode = kCAFillModeForwards;
transl1.beginTime = 0;
[myLayer addAnimation: transl1 forKey: nil];
Run Code Online (Sandbox Code Playgroud)
行走的人示例:
我已经处理了完全相同的任务,但我必须做一个跑步蜘蛛,它有 6 条腿行走,有 12 个框架。这实际上很难做到,我花了几个月的时间才完善。问题是,醒着的人示例通常是通过将图像帧数组(第一条腿、最后一条腿)设置为 UIImageView 的animationImages 属性来完成的。然后,您可以打开和关闭动画,同时左右移动角色,从而创造出行走的错觉。现在,当你想创造一种加速的错觉时,大问题就出现了。在动画播放期间无法更改动画持续时间,这是一个很难克服的重大挫折。
这是我为解决此问题而编写的代码:
在这里,您可以定义一个带有步行腿框架的数组,每步框架。
animationImagesSpider = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1@2x.png"], [UIImage imageNamed:@"2@2x.png"], [UIImage imageNamed:@"3@2x.png"], [UIImage imageNamed:@"4@2x.png"], [UIImage imageNamed:@"5@2x.png"], [UIImage imageNamed:@"6@2x.png"], [UIImage imageNamed:@"6@2x.png"], [UIImage imageNamed:@"8@2x.png"], [UIImage imageNamed:@"9@2x.png"], [UIImage imageNamed:@"10@2x.png"], [UIImage imageNamed:@"11@2x.png"], [UIImage imageNamed:@"1@2x.png"], nil];
Run Code Online (Sandbox Code Playgroud)
这里将数组附加到 UIImageView:
imgViewSpider = [[UIImageView alloc] initWithFrame:CGRectMake(200,410,100,145)];
imgViewSpider.animationImages = animationImagesSpider;
Run Code Online (Sandbox Code Playgroud)
现在,如果您只需调用 [imgViewSpider startAnimating]; 这将以恒定的速度启动动画,直到您停止它。为了克服这个问题,我使用了递归,为每个步骤播放一个简短的动画,这允许调整每个步骤之间的持续时间:
- (void) spiderRun {
imgViewSpider.animationDuration= 0.51-(accSp/3.5);
[imgViewSpider setAnimationRepeatCount:222]; /// this is a dummy value that has no effect because animtion ends after the first frame
[imgViewSpider startAnimating];
[self performSelector:@selector(spiderRun) withObject:nil afterDelay: 0.5-(accSp/3.5)];
}
Run Code Online (Sandbox Code Playgroud)
通过不断改变accSp值,我可以在行走过程中控制行走速度。
归档时间: |
|
查看次数: |
383 次 |
最近记录: |