2014-09-14 9 views
5

Sto facendo un gioco e voglio che il cuore sembri battere. Il mio approccio è avere due immagini di un cuore. Uno è più grande dell'altro. Ne ho uno come UIButton (perché per iniziare il gioco voglio fare clic sul cuore per farlo), e l'altra versione più grande del cuore è come UIImageView. Finora ce l'ho, quindi il cuore cambia dimensione ogni secondo, ma voglio che sia più realistico. Ad esempio, ogni secondo, cambierà nel cuore grande e indietro (ma non istantaneamente, quindi è chiaramente visibile). Come posso fare questo? Qui è il mio codice:Come posso simulare un cuore pulsante?

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 
+0

Un cuore non diventa semplicemente più grande e più piccolo, ma una parte batte l'altra. Ad esempio, prova 3 immagini - una "normale", una con il lato sinistro più grande, una con il lato destro più grande. –

+0

@HotLicks - Si presume che si stia riferendo a un cuore di mammiferi :-) –

+0

Anche se è un cuore a forma di cuore, l'animazione multifase sembrerà più realistica. –

risposta

22

Prova questa animazione impulso:

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

come mai l'hai capito? niente qui dice "impulso". ECCEZIONALE!!! – bendigi

+0

È come un cuore di questa pagina. Grazie! –

+0

Come posso animare come cuore infranto? – user2526811

2

Finora ho in modo che il cuore cambia dimensione ogni secondo, ma io voglio che sia più realistico.

L'ovvio suggerimento è quello di utilizzare una sequenza di immagini, per darti un'animazione "liscia".

+0

Come posso animare come cuore infranto? – user2526811

+0

Stessa risposta di cui sopra. –

+0

ma sto usando il percorso di bazier per il cuore di dispaly. – user2526811

0

BCBlanka utilizzato Core Animation, se qualcuno vuole usare biblioteca Facebooks POPAnimation questo sarebbe l'attuazione:

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

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

in base alla risposta del BCBlanka, poco più realistica animazione

+0

Come posso animare come cuore infranto? – user2526811

+0

Bella animazione! Grazie mille. –

0

Ecco un altro esempio di Facebook con POP, con un impulso più "naturale".

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