我怎样才能模拟跳动的心脏?

时间:2014-09-14 02:50:43

标签: ios objective-c animation ios7

我正在制作游戏,我希望心脏看起来像是在跳动。我的方法是有一个心脏的两个图像。一个比另一个大。我有一个作为UIButton(因为开始游戏我想点击心脏这样做),而另一个更大的心脏版本是UIImageView。到目前为止,我已经拥有它,所以心脏每秒都会改变大小,但我希望它更加真实。例如,每一秒,它将变为大心脏和后面(但不是立即,所以它清晰可见)。我怎样才能做到这一点?这是我的代码:

PlayViewController.h

#import <UIKit/UIKit.h>

@interface PlayViewController : UIViewController
{    
    IBOutlet UIButton *heartButton;
    IBOutlet UIImageView *heartBig;
    NSTimer *heartBeat;
}

@end

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

6 个答案:

答案 0 :(得分:24)

试试这个脉冲动画:

enter image description here

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"];

答案 1 :(得分:8)

Animation

/**
 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)
}

基于BCBlanka的回答,更加逼真的动画

答案 2 :(得分:2)

  

到目前为止,我已经拥有它,所以心脏每秒都会改变大小,但我希望它更加真实。

明显的建议是使用一系列图像,给你一个平滑的&#34;动画。

答案 3 :(得分:0)

BCBlanka使用Core Animation,如果有人想使用Facebook的POPAnimation库,这将是实现:

POPBasicAnimation *beatingHeartAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
beatingHeartAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
beatingHeartAnim.fromValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
beatingHeartAnim.toValue = [NSValue valueWithCGSize:CGSizeMake(0.7, 0.7)];
beatingHeartAnim.autoreverses = YES;
beatingHeartAnim.duration = 1.0;
beatingHeartAnim.repeatCount = 120;
[heart  pop_addAnimation:beatingHeartAnim forKey:@"beatingHeartAnim"];

答案 4 :(得分:0)

这是Facebook的POP的另一个例子,具有更“自然”的脉搏。

POPBasicAnimation *pulseAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.fromValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
pulseAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.1, 1.1)];

pulseAnimation.autoreverses = YES;
pulseAnimation.duration = 1.0;
pulseAnimation.repeatForever = YES;
[self pop_addAnimation:pulseAnimation forKey:@"pulseAnimation"];

答案 5 :(得分:0)

Swift 5代码:

    let pulse = CASpringAnimation(keyPath: "transform.scale")
    pulse.duration = 0.4
    pulse.fromValue = 1.0
    pulse.toValue = 1.12
    pulse.autoreverses = true
    pulse.repeatCount = .infinity
    pulse.initialVelocity = 0.5
    pulse.damping = 0.8
    button.layer.add(pulse, forKey: nil)