- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的 iPhone 应用程序中,我有一个 UIButton,当另一个 UIButton 被按下时,我将它旋转 180 度进入 View ,然后当再次单击时,该按钮会进一步旋转 180 度回到它开始的位置。
这在第一次完整的 360 度过程发生时一切正常,但如果我尝试再次从头开始,它会捕捉 180 度,然后尝试从该点旋转它。谁能指出我正确的方向?到目前为止,这是我的代码...
showAnimation= [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
showAnimation.duration = self.showAnimationDuration;
showAnimation.repeatCount = 1;
showAnimation.fillMode = kCAFillModeForwards;
showAnimation.removedOnCompletion = NO;
showAnimation.cumulative = YES;
showAnimation.delegate = self;
float currentAngle =[[[rotateMe.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue];
//Rotate 180 degrees from current rotation
showAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:currentAngle],
[NSNumber numberWithFloat:currentAngle + (0.5 * M_PI)],
[NSNumber numberWithFloat:currentAngle + M_PI], nil];
[rotateMe.layer addAnimation:showAnimation forKey:@"show"];
动画完成后,我将 rotateMe.transform 旋转更新为图层的旋转,使其可用。
- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
float currentAngle =[[[self.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"] floatValue];
NSLog(@"End: %f", currentAngle);
rotateMe.transform = CGAffineTransformMakeRotation(0);
}
我已经用
实现了完整的工作效果 [UIView animateWithDuration:1.0f]
animations:^{
CGAffineTransform transform = CGAffineTransformRotate(rotateMe.transform, DEGREES_TO_RADIANS(179.999f));
rotateMe.transform = transform;
}
];
但我想让动画更复杂,因此需要 CAKeyframeAnimation。
最佳答案
如果您需要所有关键帧,您可以将动画配置为相加
并在 0 到 180 度之间设置动画。如果您不需要不同的关键帧,您可以简单地使用基本动画和 byValue
属性来完成。下次添加动画时,它会比 View 上的任何旋转多旋转 180 度。
如果您在委托(delegate)回调中设置实际值,则不需要填充模式,也不需要在完成时移除动画。
CABasicAnimation *showAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.byValue = M_PI;
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;
[rotateMe.layer addAnimation:showAnimation forKey:@"show"];
或使用关键帧动画(如上所述:使其从 0 度到 180 度添加动画)。
CAKeyframeAnimation *showAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
showAnimation.additive = YES; // Make the values relative to the current value
showAnimation.values = @[0, /*all your intermediate values here... ,*/ M_PI];
showAnimation.duration = self.showAnimationDuration;
showAnimation.delegate = self;
[rotateMe.layer addAnimation:showAnimation forKey:@"show"];
关于ios - CAKeyframeAnimation 从最后一点开始重复旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297675/
我有一个这样的: 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 的帮助下实现的,并且可以正常工作。但我想改进动画,以便当用户打开屏幕时,云必须看起来已
我是一名优秀的程序员,十分优秀!