- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在另一个关键帧动画测试中,我结合了沿着贝塞尔曲线路径移动 UIImageView(称为 theImage
)并在移动时将其缩放得更大,从而在结束时将图像放大 2 倍小路。我执行此操作的初始代码包含以下元素以启动动画:
UIImageView* theImage = ....
float scaleFactor = 2.0;
....
theImage.center = destination;
theImage.transform = CGAffineTransformMakeScale(1.0,1.0);
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(theImage.image.size.height*scaleFactor, theImage.image.size.width*scaleFactor)]];
resizeAnimation.fillMode = kCAFillModeBackwards;
resizeAnimation.removedOnCompletion = NO;
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = [jdPath path].CGPath;
pathAnimation.fillMode = kCAFillModeBackwards;
pathAnimation.removedOnCompletion = NO;
CAAnimationGroup* group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:pathAnimation, resizeAnimation, nil];
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
group.removedOnCompletion = NO;
group.duration = duration;
group.delegate = self;
[theImage.layer addAnimation:group forKey:@"animateImage"];
然后,当动画完成时我想保留较大尺寸的图像,所以我实现:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
theImage.transform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
}
这一切都有效..有点。问题是在动画结束时 theImage
会闪烁一小会儿——足以让它看起来很糟糕。我猜这是动画结束时的过渡,我将转换设置为新的大小。
在试验中,我尝试了一种与上述略有不同的形式,但仍然出现相同的闪烁:
CAKeyframeAnimation *resizeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
NSValue* startSizeKey = [NSValue valueWithCATransform3D:CATransform3DScale (theImage.layer.transform, 1.0, 1.0, 1.0)];
NSValue* endSizeKey = [NSValue valueWithCATransform3D:CATransform3DScale (theImage.layer.transform, scaleFactor, scaleFactor, 1.0)];
NSArray* sizeKeys = [NSArray arrayWithObjects:startSizeKey, endSizeKey, nil];
[resizeAnimation setValues:sizeKeys];
....
theImage.transform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
但是当我以与原始尺寸相同的尺寸结束动画时,没有闪烁:
CAKeyframeAnimation *resizeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
NSValue* startSizeKey = [NSValue valueWithCATransform3D:CATransform3DScale (theImage.layer.transform, 1.0, 1.0, 1.0)];
NSValue* middleSizeKey = [NSValue valueWithCATransform3D:CATransform3DScale (theImage.layer.transform, scaleFactor, scaleFactor, 1.0)];
NSValue* endSizeKey = [NSValue valueWithCATransform3D:CATransform3DScale (theImage.layer.transform, 1.0, 1.0, 1.0)];
NSArray* sizeKeys = [NSArray arrayWithObjects:startSizeKey, middleSizeKey, endSizeKey, nil];
[resizeAnimation setValues:sizeKeys];
....
theImage.transform = CGAffineTransformMakeScale(1.0,1.0);
所以我的大问题是如何在没有闪烁的情况下为这个图像制作动画,并在动画结束时以不同的尺寸结束?
编辑 3 月 2 日
我最初的测试是放大图像。我只是尝试缩小它 (IE scaleFactor = 0.4) 并且闪烁更加明显,并且就我所看到的而言更加明显。这是事件的顺序:
所以我看到的闪烁似乎是第 4 步。
编辑 3 月 22 日
我刚刚向 GitHub 上传了一个演示项目,展示了对象沿贝塞尔曲线路径的移动。代码可以在 PathMove 找到
我也在我的博客 Moving objects along a bezier path in iOS 上写过它
最佳答案
使用 Core Animation 为 View 的层设置动画可能会很棘手。有几件事让它变得困惑:
在图层上设置动画不会更改图层的属性。相反,只要应用了动画,它就会更改“表示层”的属性,该“表示层”会替换屏幕上的原始“模型层”。
更改层的属性通常会向层添加隐式动画,并将属性名称作为动画的键。因此,如果您想显式地为某个属性设置动画,您通常需要将该属性设置为它的最终值,然后添加一个以属性名称为键的动画,以覆盖隐式动画。
View 通常会在其图层上禁用隐式动画。它还以其他有些神秘的方式处理其层的属性。
此外,令人困惑的是,您为 View 的边界设置动画以将其放大,但最后切换到缩放转换。
我认为做你想做的最简单的方法是尽可能使用UIView
动画方法,并且只为关键帧动画引入Core Animation。在让 UIView
添加自己的动画后,您可以将关键帧动画添加到 View 层,并且您的关键帧动画将覆盖 UIView
添加的动画。
这对我有用:
- (IBAction)animate:(id)sender {
UIImageView* theImage = self.imageView;
CGFloat scaleFactor = 2;
NSTimeInterval duration = 1;
UIBezierPath *path = [self animationPathFromStartingPoint:theImage.center];
CGPoint destination = [path currentPoint];
[UIView animateWithDuration:duration animations:^{
// UIView will add animations for both of these changes.
theImage.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
theImage.center = destination;
// Prepare my own keypath animation for the layer position.
// The layer position is the same as the view center.
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.path = path.CGPath;
// Copy properties from UIView's animation.
CAAnimation *autoAnimation = [theImage.layer animationForKey:@"position"];
positionAnimation.duration = autoAnimation.duration;
positionAnimation.fillMode = autoAnimation.fillMode;
// Replace UIView's animation with my animation.
[theImage.layer addAnimation:positionAnimation forKey:positionAnimation.keyPath];
}];
}
关于iOS CAKeyFrameAnimation 动画结束时缩放闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500968/
我有一个这样的: CALayer *layer = stripeButton.layer; CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation a
我的类中有几个 CAKeyframeAnimation 对象。 他们都以 self 为代表。 在我的animationDidStop函数中,我如何知道调用来自哪里? 是否有任何变量可以传递给 CAKe
我为图层编写了动画,将图层从一个位置移动到另一个位置: - (void) animateCentreRightTransition: (int) transition { float scaleFac
我有一个菜单,它是一个 CALayer,它将在屏幕上滑动到给定点。我想要的效果是菜单会稍微超出该点,然后在该点之前一点,然后落在该点上。我可以通过应用变换来移动菜单,但我希望让这种弹跳效果发挥作用。我
现在有人如何使用 CAKeyframeAnimation 同时为多个图层设置动画吗?每个层都有自己的 CAKeyframeAnimation 对象。看一下下面的代码: 我有一个接收对象、创建 CAKe
我使用 View 的 CALayer 上的 CAKeyframeAnimation 来对 UIView 的框架属性进行动画处理,并对“位置”属性进行动画处理。我希望能够在用户点击屏幕时将动画停止在当前
这是我的代码: - (void)animateImages { self.emoImageView = [[UIImageView alloc] initWithFrame:CGRectMak
我有一堆动画添加到了 CALayer 中。我需要知道按钮在动画交互时的位置。但框架并没有改变。我如何找到/计算该值?这是代码: let positionY = CAKeyframeAnimat
我正在使用 CAKeyframeAnimation 来制作旋转动画。我正在使用按钮触发图层上的动画。 我想从上一个动画停止弧度开始下一个旋转动画。我用kCAFillModeForwards设置了fil
在制作关键帧动画时,我似乎忽略了明显的内容。我已经查看了许多代码示例,包括 CAKeyframeAnimation 文档中引用的 Apple 的 MoveMe,但是,我找不到会导致我所看到的情况的差异
我正在尝试创建一个动画,其中项目以交错的时间进入 View 。为此,我使用了 CAKeyframeAnimation,但为了实现惊人效果,我需要指定 beginTime。 问题是我还希望动画在完成时不
我需要为 UIImageView 添加从起点到终点以及之后从终点到起点的无限动画。我正在尝试这样: CAKeyframeAnimation *pathAnimation = [CAKeyframeAn
我想用 CAKeyframeAnimation 旋转一个 UIImageView。问题是阴影随物体旋转,产生非真实效果... The shadow must remain under the imag
我正在尝试使用以下代码制作一个简单的 CAKeyframeAnimation: - (void)viewDidLoad { [super viewDidLoad]; // Do any
谁能告诉我在动画运行时是否始终保证 CAKeyframeAnimation 中的关键帧以其准确值命中?或者……他们只是充当插值指南吗?例如如果我指定,比方说,路径上的 3 个点,让一些任意属性遵循 -
我正在尝试使用 CAKeyframeAnimation 为 UIImage 数组设置动画。理论上很简单。 帖子底部的示例代码。 我的问题是,在动画完成后,我有一个无法消除的巨大漏洞。 初始化 CAKe
在我的 iPhone 应用程序中,我有一个 UIButton,当另一个 UIButton 被按下时,我将它旋转 180 度进入 View ,然后当再次单击时,该按钮会进一步旋转 180 度回到它开始的
我有以下代码行来运行带有某些帧的动画。 - (void)setAnimation { /* creating a layer */ CALayer *layer = [[CALayer
目前我正在使用 CAKeyframeAnimation 沿路径移动 png 图像 30 秒。有什么方法可以在点击按钮时在 0-30 秒之间停止此移动图像? 最佳答案 这将删除动画...... [you
我的应用程序中有一个屏幕,用户可以在其中看到沿着弧线飞行的云。此移动是在 CAKeyframeAnimation 的帮助下实现的,并且可以正常工作。但我想改进动画,以便当用户打开屏幕时,云必须看起来已
我是一名优秀的程序员,十分优秀!