Chaining Animations

- (void)right {
-// photo1 is going to move to where photo2 is
NSPoint newOrigin = [photo2 frame].origin;
CABasicAnimation *animation = [self basicAnimationNamed:@"photo1" duration:1.0f];
animation.delegate = self;
 [photo1 setAnimations:
[NSDictionary dictionaryWithObject:animation forKey:@"frameOrigin" ]];
 [[photo1 animator] setFrameOrigin:newOrigin];
}

- (void) reset {
[photo1 setAnimations:nil];
 [photo2 setAnimations:nil];
 NSPoint newPhoto1Origin = NSMakePoint(0.0f, NSMidY([self frame]) -
- (NSHeight([photo1 bounds]) / 2.0f));
NSPoint newPhoto2Origin = NSMakePoint(NSMidX([self frame]) - (NSWidth([photo2 bounds]) / 2.0f), NSMidY([self frame]) - (NSHeight([photo2 bounds]) / 2.0f));

 [[photo1 animator] setFrameOrigin:newPhoto1Origin];
[[photo2 animator] setFrameOrigin:newPhoto2Origin];
 }

 

animation.delegate = self;

 

 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {
  if(flag && [[animation valueForKey:@"name" ] isEqual:@"photo1" ]) {
  CABasicAnimation *photo2Animation =
 [self basicAnimationNamed:@"photo2" duration:animation.duration];
[photo2 setAnimations:[NSDictionary dictionaryWithObject:photo2Animation forKey:@"frameOrigin" ]];
NSPoint newPhoto2Origin = NSMakePoint(NSMaxX([self frame]) - [photo2 frame].size.width,
[photo2 frame].origin.y);
[[photo2 animator] setFrameOrigin:newPhoto2Origin];
   }
}

 

你可能感兴趣的:(animation)