use*_*030 7 animation objective-c ios ios7
我正在制作游戏,我希望心脏看起来像是在跳动.我的方法是有一个心脏的两个图像.一个比另一个大.我有一个作为UIButton(因为开始游戏我想点击心脏这样做),而另一个更大的心脏版本是UIImageView.到目前为止,我已经拥有它,所以心脏每秒都会改变大小,但我希望它更加真实.例如,每一秒,它将变为大心脏和后面(但不是立即,所以它清晰可见).我怎样才能做到这一点?这是我的代码:
PlayViewController.h
#import <UIKit/UIKit.h>
@interface PlayViewController : UIViewController
{
IBOutlet UIButton *heartButton;
IBOutlet UIImageView *heartBig;
NSTimer *heartBeat;
}
@end
Run Code Online (Sandbox Code Playgroud)
PlayViewController.m
#import "PlayViewController.h"
@interface PlayViewController ()
@end
@implementation PlayViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
heartBig.hidden = YES;
heartBeat = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(beatHeart) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)beatHeart
{
if(heartBig.hidden == true)
heartBig.hidden = false;
else
heartBig.hidden = true;
}
@end
Run Code Online (Sandbox Code Playgroud)
BCB*_*nka 25
试试这个脉冲动画:

Run Code Online (Sandbox Code Playgroud)CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"]; theAnimation.duration=0.7; theAnimation.repeatCount=HUGE_VALF; theAnimation.autoreverses=YES; theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; theAnimation.toValue=[NSNumber numberWithFloat:0.7]; theAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; [self.heart.layer addAnimation:theAnimation forKey:@"animateOpacity"];
/**
Heart beating animation
*/
func addHeartBeatAnimation () {
let beatLong: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
beatLong.fromValue = NSValue(CGSize: CGSizeMake(1, 1))
beatLong.toValue = NSValue(CGSize: CGSizeMake(0.7, 0.7))
beatLong.autoreverses = true
beatLong.duration = 0.5
beatLong.beginTime = 0.0
let beatShort: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
beatShort.fromValue = NSValue(CGSize: CGSizeMake(1, 1))
beatShort.toValue = NSValue(CGSize: CGSizeMake(0.5, 0.5))
beatShort.autoreverses = true
beatShort.duration = 0.7
beatShort.beginTime = beatLong.duration
beatLong.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn )
let heartBeatAnim: CAAnimationGroup = CAAnimationGroup()
heartBeatAnim.animations = [beatLong, beatShort]
heartBeatAnim.duration = beatShort.beginTime + beatShort.duration
heartBeatAnim.fillMode = kCAFillModeForwards
heartBeatAnim.removedOnCompletion = false
heartBeatAnim.repeatCount = FLT_MAX
self.layer.addAnimation(heartBeatAnim, forKey: nil)
}
Run Code Online (Sandbox Code Playgroud)
基于BCBlanka的答案,更加逼真的动画
| 归档时间: |
|
| 查看次数: |
5429 次 |
| 最近记录: |