- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我正在尝试实现一个动画,当你按住一个按钮时,它会向下移动一个 block ,当你松开它时,它会将它动画回原来的位置,但我无法获得动画 block 的当前位置否有什么关系。这是我的代码:
-(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, position.y)];
[movePath addLineToPoint: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:
时清除它或创建一个新实例,但我没有看到您这样做。
一个不太明显的问题是(从你对 removedOnCompletion
和 fillMode
的使用来看,尽管你使用了 presentationLayer
)你显然不明白 Core Animation 是如何工作的。事实证明这是非常普遍的,所以如果我错了请原谅我。不管怎样,请继续阅读,因为我将解释 Core Animation 的工作原理以及如何解决您的问题。
在 Core Animation 中,您通常使用的层对象是一个模型层。当您将动画附加到层时,Core Animation 会创建模型层的副本,称为表示层,动画会随时间改变表示层的属性。动画永远不会改变模型层的属性。
当动画结束并且(默认情况下)被移除时,表示层被销毁并且模型层的属性值再次生效。因此,屏幕上的图层似乎“弹回到”其原始位置/颜色/其他任何内容。
解决此问题的一种常见但错误方法是将动画的 removedOnCompletion
设置为 NO
及其 fillMode
到 kCAFillModeForwards
。当您执行此操作时,表示层将悬空,因此屏幕上不会出现“回弹”。问题是现在您的表示层与模型层具有不同的值。如果您向模型层(或拥有它的 View )询问动画属性的值,您将得到一个与屏幕上显示的值不同的值。如果您再次尝试为该属性设置动画,动画可能会从错误的位置开始。
要为图层属性设置动画并使其“粘附”,您需要更改模型图层的属性值,然后应用动画。这样,当动画被移除并且表示层消失时,屏幕上的层将看起来完全相同,因为模型层的属性值与其表示层在动画结束时的属性值相同。
现在,我不知道您为什么要使用关键帧来为直线运动设置动画,或者为什么要使用动画组。这里似乎都没有必要。而且您的两种方法实际上是相同的,所以让我们分解出通用代码:
- (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 .只需创建一个新的单 View 应用程序项目并将 ViewController.m
的内容替换为要点的内容即可。
关于ios - 动画期间无法获取 CALayer 的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888926/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!