在 UIImageView Up & 中为 UIImage 设置动画向下(就像悬停一样)循环 [英] Animate UIImage in UIImageView Up & Down (Like it's hovering) Loop

查看:28
本文介绍了在 UIImageView Up & 中为 UIImage 设置动画向下(就像悬停一样)循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一张图像,我想上下移动(上移 10 像素和下移 10 像素),以便我的图像看起来是悬停的.我怎么能用简单的动画做到这一点非常感谢!!!

Hello I have an image that I'd like to move up and down (Up 10 pixels and down 10 pixels) so that my image appears to be hovering. How can I do this with simple animation thanks so much!!!

推荐答案

您可以使用 Core Animation 为视图层的位置设置动画.如果您将动画配置为 additive,您就不必费心计算新的绝对位置,只需计算变化(相对位置).

You could use Core Animation to animate the position of the views layer. If you configure the animation to be additive you won't have to bother calculating the new absolute position, just the change (relative position).

CABasicAnimation *hover = [CABasicAnimation animationWithKeyPath:@"position"];
hover.additive = YES; // fromValue and toValue will be relative instead of absolute values
hover.fromValue = [NSValue valueWithCGPoint:CGPointZero];
hover.toValue = [NSValue valueWithCGPoint:CGPointMake(0.0, -10.0)]; // y increases downwards on iOS
hover.autoreverses = YES; // Animate back to normal afterwards
hover.duration = 0.2; // The duration for one part of the animation (0.2 up and 0.2 down)
hover.repeatCount = INFINITY; // The number of times the animation should repeat
[myView.layer addAnimation:hover forKey:@"myHoverAnimation"];

由于这是使用 Core Animation,您需要将 QuartzCore.framework 和 #import <QuartzCore/QuartzCore.h> 添加到您的代码中.

Since this is using Core Animation you will need to add QuartzCore.framework and #import <QuartzCore/QuartzCore.h> into your code.

这篇关于在 UIImageView Up &amp; 中为 UIImage 设置动画向下(就像悬停一样)循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆