
-(IBAction)moveDown:(ID)sender{ CGRect position = [[container.layer presentationLayer] frame]; [movePath movetoPoint:CGPointMake(container.frame.origin.x,position.y)]; [movePath addlinetoPoint:CGPointMake(container.frame.origin.x,310)]; CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; moveAnim.path = movePath.CGPath; moveAnim.removedOnCompletion = NO; moveAnim.fillMode = kCAFillModeForwards; CAAnimationGroup *animgroup = [CAAnimationGroup animation]; animgroup.animations = [NSArray arrayWithObjects:moveAnim,nil]; animgroup.duration = 2.0; animgroup.removedOnCompletion = NO; animgroup.fillMode = kCAFillModeForwards; [container.layer addAnimation:animgroup forKey:nil];}-(IBAction)moveUp:(ID)sender{ CGRect position = [[container.layer presentationLayer] frame]; UIBezIErPath *movePath = [UIBezIErPath bezIErPath]; [movePath movetoPoint:CGPointMake(container.frame.origin.x,115)]; CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; moveAnim.path = movePath.CGPath; moveAnim.removedOnCompletion = NO; moveAnim.fillMode = kCAFillModeForwards; CAAnimationGroup *animgroup = [CAAnimationGroup animation]; animgroup.animations = [NSArray arrayWithObjects:moveAnim,nil]; animgroup.duration = 2.0; animgroup.removedOnCompletion = NO; animgroup.fillMode = kCAFillModeForwards; [container.layer addAnimation:animgroup forKey:nil];} 但行
CGRect position = [[container.layer presentationLayer] frame];
只返回目的位置而不是当前位置.一旦我释放按钮,我需要基本上给我一个动画的容器的当前位置,所以我可以执行下一个动画.我现在没有工作.
解决方法 我没有分析你的代码足够100%确定为什么[[container.layer presentationLayer] frame]可能不会返回你所期望的.但我看到几个问题.一个明显的问题是moveDown:不声明movePath.如果movePath是一个实例变量,你可能想要清除它,或者在每次moveDown被调用时创建一个新的实例,但是我看不到你这样做.
一个不太明显的问题是(从使用removeOnCompletion和fillMode判断,尽管使用presentationLayer)您显然不了解Core Animation的工作原理.事实证明是令人惊讶的常见问题,所以原谅我,如果我错了.无论如何,请阅读,因为我会解释核心动画如何工作,然后如何解决您的问题.
在Core Animation中,您通常使用的图层对象是模型图层.当将动画附加到图层时,Core Animation将创建一个名为表示层的模型图层的副本,并且动画随着时间的推移更改表示层的属性.动画永远不会更改模型图层的属性.
当动画结束并且(默认情况下)被删除时,表示层被破坏,并且模型层的属性的值再次生效.因此,屏幕上的图层似乎会“回d”到其原始位置/颜色/任何内容.
解决这个问题的常见但错误的方法是将动画的removeOnCompletion设置为NO,将其fillMode设置为kCAFillModeForwards.执行此 *** 作时,表示层会挂起,所以屏幕上没有“快退”.问题在于,现在您将表现层挂在不同于模型层的值上.如果您要求模型层(或拥有它的视图)为动画属性的值,您将获得一个不同于屏幕上的值.如果您尝试再次动画该动画,动画可能会从错误的地方开始.
要动画化图层属性并使其“粘贴”,您需要更改模型图层的属性值,然后应用该动画.这样,当动画被移除并且表现层消失时,屏幕上的图层将看起来完全相同,因为模型层与动画结束时的表现层具有相同的属性值.
现在,我不知道为什么要使用关键帧来动画直线运动,或者为什么要使用动画组.在这里似乎都不需要.您的两种方法几乎相同,所以我们来解释一下常见的代码:
- (voID)animateLayer:(CALayer *)layer toY:(CGfloat)y { CGPoint fromValue = [layer.presentationLayer position]; CGPoint tovalue = CGPointMake(fromValue.x,y); layer.position = tovalue; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; animation.fromValue = [NSValue valueWithCGPoint:fromValue]; animation.tovalue = [NSValue valueWithCGPoint:tovalue]; animation.duration = 2; [layer addAnimation:animation forKey:animation.keyPath];} 请注意,当我将动画添加到图层时,我们给动画一个关键.由于我们每次都使用相同的关键字,所以如果先前的动画尚未完成,每个新的动画将替换(删除)先前的动画.
当然,一旦你玩这个,你会发现,如果你moveUp:当moveDown:只有一半完成,moveUp:动画将看起来是一半的速度,因为它仍然有2秒的持续时间,但只有一半的旅行.我们应该根据要旅行的距离来计算持续时间:
- (voID)animateLayer:(CALayer *)layer toY:(CGfloat)y withBaseY:(CGfloat)baseY { CGPoint fromValue = [layer.presentationLayer position]; CGPoint tovalue = CGPointMake(fromValue.x,y); layer.position = tovalue; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; animation.fromValue = [NSValue valueWithCGPoint:fromValue]; animation.tovalue = [NSValue valueWithCGPoint:tovalue]; animation.duration = 2.0 * (tovalue.y - fromValue.y) / (y - baseY); [layer addAnimation:animation forKey:animation.keyPath];} 如果您真的需要它作为一个动画组中的关键字动画,您的问题应该告诉我们为什么你需要这些东西.无论如何,它也适用于这些东西:
- (voID)animateLayer:(CALayer *)layer toY:(CGfloat)y withBaseY:(CGfloat)baseY { CGPoint fromValue = [layer.presentationLayer position]; CGPoint tovalue = CGPointMake(fromValue.x,y); layer.position = tovalue; UIBezIErPath *path = [UIBezIErPath bezIErPath]; [path movetoPoint:fromValue]; [path addlinetoPoint:tovalue]; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animation.path = path.CGPath; animation.duration = 2.0 * (tovalue.y - fromValue.y) / (y - baseY); CAAnimationGroup *group = [CAAnimationGroup animation]; group.duration = animation.duration; group.animations = @[animation]; [layer addAnimation:group forKey:animation.keyPath];} 您可以找到我的测试程序in this gist的完整代码.只需创建一个新的单视图应用程序项目,并将其内容替换为VIEwController.m的内容.
总结以上是内存溢出为你收集整理的ios – 在动画过程中无法获取CALayer的当前位置全部内容,希望文章能够帮你解决ios – 在动画过程中无法获取CALayer的当前位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)