2013年7月1日 星期一

Ios animation sample (CABasicAnimation)

To start animation:

ImageView wave1,wave2 //basic sample CABasicAnimation *wave1ScaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; wave1ScaleAnimation.toValue = [NSNumber numberWithFloat:1.5]; wave1ScaleAnimation.duration = 1; //infinity loop wave1ScaleAnimation.repeatCount = HUGE_VALF; //totally 2 second per cycle wave1ScaleAnimation.autoreverses = YES; wave1ScaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; [wave1.layer addAnimation:wave1ScaleAnimation forKey:@"RecordingAnimation"]; //group animation sample CABasicAnimation *wave2ScaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; wave2ScaleAnimation.toValue = [NSNumber numberWithFloat:2.1]; CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; alphaAnimation.toValue = [NSNumber numberWithFloat:0.0]; //begin time and duration relative to group alphaAnimation.beginTime = 0.5; alphaAnimation.duration =0.5; CAAnimationGroup *group = [CAAnimationGroup animation]; //use CACurrentMediaTime() to get absolute begin Time group.beginTime = CACurrentMediaTime()+ 1; group.duration = 1; group.repeatCount = HUGE_VALF; group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; group.animations = [NSArray arrayWithObjects:wave2ScaleAnimation, alphaAnimation,nil]; [wave2.layer addAnimation:group forKey:@"groupAnimation"];

To stop animation:

[wave1.layer removeAllAnimations]; [wave2.layer removeAnimationForKey:@"groupAnimation"];

[CABasicAnimation animationWithKeyPath:@"transform.scale"]中的keyPath如下表:


另一種較舊的animation用法:

[UIView animateWithDuration:0.5 delay:1.0 options: UIViewAnimationCurveEaseOut animations:^{ self.basketTop.frame = basketTopFrame; self.basketBottom.frame = basketBottomFrame; } completion:^(BOOL finished){ NSLog(@"Done!"); }]; }

順便附上很方便的alpha transition animation:

[UIView transitionWithView:myImageView duration:0.25f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [rotateView setImage:[UIImage imageNamed:@"record_rotate_red.png"]]; } completion:nil];

沒有留言:

張貼留言