【发布时间】:2012-04-27 00:22:00
【问题描述】:
在我的 iOS 5 应用程序中,我有一个自定义的 UIButton,它的图像是一个红色的球体。当点击按钮时,我希望球体开始在红色和绿色之间跳动/褪色。我有红色和绿色图像,并且可以使用以下代码在两者之间成功交叉溶解:
CABasicAnimation *imageSwitchAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
imageSwitchAnimation.fromValue = (id)[UIImage imageNamed:@"red.png"].CGImage;
imageSwitchAnimation.toValue = (id)[UIImage imageNamed:@"green.png"].CGImage;
imageSwitchAnimation.duration = 1.5f;
[self.button.imageView.layer addAnimation:imageSwitchAnimation forKey:@"animateContents"];
但是,我希望动画永远持续下去(嗯,直到我告诉它停止),并且还希望动画反转和循环。换句话说,淡出红色 -> 绿色 -> 红色,然后重复。
我尝试将上面的动画块放入一个无限循环中(连同一些逻辑来确定褪色应该从红色 -> 绿色还是绿色 -> 红色),但这只会锁定整个应用程序。
其他解决方案似乎使用 Cocos2d,它似乎相当重量级,因为这是我在应用程序中唯一需要的动画(所以我不想使用这样的框架,除非绝对需要)。
任何帮助将不胜感激。
【问题讨论】:
标签: ios cocoa-touch ios5 core-animation cabasicanimation