30個常用動畫機制 ios
阿新 • • 發佈:2018-12-24
#import
@interface
CoreAnimationEffect : NSObject
#pragma mark -
Custom Animation
+
(void)showAnimationType:(NSString *)type
withSubType:(NSString *)subType
duration:(CFTimeInterval)duration
timingFunction:(NSString *)timingFunction
view:(UIView *)theView;
#pragma mark -
Preset Animation
// reveal
+
(void)animationRevealFromBottom:(UIView *)view;
+
(void)animationRevealFromTop:(UIView *)view;
+
(void)animationRevealFromLeft:(UIView *)view;
+
(void)animationRevealFromRight:(UIView *)view;
// 漸隱漸消
+
(void)animationEaseIn:(UIView *)view;
+
(void)animationEaseOut:(UIView *)view;
// 翻轉
+
(void)animationFlipFromLeft:(UIView *)view;
+
(void)animationFlipFromRigh:(UIView *)view;
// 翻頁
+
(void)animationCurlUp:(UIView *)view;
+
(void)animationCurlDown:(UIView *)view;
// push
+
(void)animationPushUp:(UIView *)view;
+
(void)animationPushDown:(UIView *)view;
+
(void)animationPushLeft:(UIView *)view;
+
(void)animationPushRight:(UIView *)view;
// move
+
(void)animationMoveUp:(UIView *)view
duration:(CFTimeInterval)duration;
+
(void)animationMoveDown:(UIView *)view
duration:(CFTimeInterval)duration;
+
(void)animationMoveLeft:(UIView *)view;
+
(void)animationMoveRight:(UIView *)view;
// 旋轉縮放
//
各種旋轉縮放效果
+
(void)animationRotateAndScaleEffects:(UIView *)view;
//
旋轉同時縮小放大效果
+
(void)animationRotateAndScaleDownUp:(UIView *)view;
#pragma mark -
Private API
+
(void)animationFlipFromTop:(UIView *)view;
+
(void)animationFlipFromBottom:(UIView *)view;
+
(void)animationCubeFromLeft:(UIView *)view;
+
(void)animationCubeFromRight:(UIView *)view;
+
(void)animationCubeFromTop:(UIView *)view;
+
(void)animationCubeFromBottom:(UIView *)view;
+
(void)animationSuckEffect:(UIView *)view;
+
(void)animationRippleEffect:(UIView *)view;
+
(void)animationCameraOpen:(UIView *)view;
+
(void)animationCameraClose:(UIView *)view;
@end
//
#import
"CoreAnimationEffect.h"
#import
@implementation
CoreAnimationEffect
#pragma mark -
Custom Animation
+
(void)showAnimationType:(NSString *)type
withSubType:(NSString *)subType
duration:(CFTimeInterval)duration
timingFunction:(NSString *)timingFunction
view:(UIView *)theView
{
CATransition *animation =
[CATransition animation];
animation.delegate =
self;
animation.duration =
duration;
animation.timingFunction =
[CAMediaTimingFunction functionWithName:timingFunction];
animation.fillMode =
kCAFillModeForwards;
//
animation.removedOnCompletion
= NO;
animation.type = type;
animation.subtype =
subType;
[theView.layer
addAnimation:animation forKey:nil];
}
#pragma mark -
Preset Animation
+
(void)animationRevealFromBottom:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setType:kCATransitionReveal];
[animation
setSubtype:kCATransitionFromBottom];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn]];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationRevealFromTop:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setType:kCATransitionReveal];
[animation
setSubtype:kCATransitionFromTop];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationRevealFromLeft:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setType:kCATransitionReveal];
[animation
setSubtype:kCATransitionFromLeft];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationRevealFromRight:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setType:kCATransitionReveal];
[animation
setSubtype:kCATransitionFromRight];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationEaseIn:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setType:kCATransitionFade];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn]];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationEaseOut:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setType:kCATransitionFade];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationFlipFromLeft:(UIView *)view
{
[UIView beginAnimations:nil
context:NULL];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView
setAnimationDuration:0.35f];
[UIView
setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:view cache:NO];
[UIView
commitAnimations];
}
+
(void)animationFlipFromRigh:(UIView *)view
{
[UIView beginAnimations:nil
context:NULL];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView
setAnimationDuration:0.35f];
[UIView
setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:view cache:NO];
[UIView
commitAnimations];
}
+
(void)animationCurlUp:(UIView *)view
{
[UIView beginAnimations:nil
context:NULL];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView
setAnimationDuration:0.35f];
[UIView
setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view
cache:NO];
[UIView
commitAnimations];
}
+
(void)animationCurlDown:(UIView *)view
{
[UIView beginAnimations:nil
context:NULL];
[UIView
setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView
setAnimationDuration:0.35f];
[UIView
setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:view cache:NO];
[UIView
commitAnimations];
}
+
(void)animationPushUp:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:kCATransitionPush];
[animation
setSubtype:kCATransitionFromTop];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationPushDown:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:kCATransitionPush];
[animation
setSubtype:kCATransitionFromBottom];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationPushLeft:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:kCATransitionPush];
[animation
setSubtype:kCATransitionFromLeft];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationPushRight:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:kCATransitionPush];
[animation
setSubtype:kCATransitionFromRight];
[view.layer
addAnimation:animation forKey:nil];
}
//
presentModalViewController
+
(void)animationMoveUp:(UIView *)view
duration:(CFTimeInterval)duration
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:duration];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation
setType:kCATransitionMoveIn];
[animation
setSubtype:kCATransitionFromTop];
[view.layer
addAnimation:animation forKey:nil];
}
//
dissModalViewController
+
(void)animationMoveDown:(UIView *)view
duration:(CFTimeInterval)duration
{
CATransition *transition =
[CATransition animation];
transition.duration
=0.4;
transition.timingFunction =
[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type =
kCATransitionReveal;
transition.subtype =
kCATransitionFromBottom;
[view.layer
addAnimation:transition forKey:nil];
}
+
(void)animationMoveLeft:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:kCATransitionMoveIn];
[animation
setSubtype:kCATransitionFromLeft];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationMoveRight:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:kCATransitionMoveIn];
[animation
setSubtype:kCATransitionFromRight];
[view.layer
addAnimation:animation forKey:nil];
}
+(void)animationRotateAndScaleEffects:(UIView *)view
{
[UIView
animateWithDuration:0.35f animations:^
{
view.transform =
CGAffineTransformMakeScale(0.001, 0.001);
CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform"];
//
向右旋轉45°縮小到最小,然後再從小到大推出.
animation.toValue = [NSValue
valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40,
0.80)];
animation.duration =
0.45;
animation.repeatCount =
1;
[view.layer
addAnimation:animation forKey:nil];
}
completion:^(BOOL
finished)
{
[UIView
animateWithDuration:0.35f animations:^
{
view.transform = CGAffineTransformMakeScale(1.0,
1.0);
}];
}];
}
+
(void)animationRotateAndScaleDownUp:(UIView *)view
{
CABasicAnimation
*rotationAnimation = [CABasicAnimation
animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber
numberWithFloat:(2 * M_PI) * 2];
rotationAnimation.duration = 0.35f;
rotationAnimation.timingFunction =
[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CABasicAnimation *scaleAnimation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.toValue = [NSNumber
numberWithFloat:0.0];
scaleAnimation.duration = 0.35f;
scaleAnimation.timingFunction =
[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CAAnimationGroup *animationGroup =
[CAAnimationGroup animation];
animationGroup.duration = 0.35f;
animationGroup.autoreverses = YES;
animationGroup.repeatCount = 1;
animationGroup.animations =[NSArray
arrayWithObjects:rotationAnimation, scaleAnimation, nil];
[view.layer addAnimation:animationGroup
forKey:@"animationGroup"];
}
#pragma mark -
Private API
+
(void)animationFlipFromTop:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"oglFlip"];
[animation
setSubtype:@"fromTop"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationFlipFromBottom:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"oglFlip"];
[animation
setSubtype:@"fromBottom"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationCubeFromLeft:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"cube"];
[animation
setSubtype:@"fromLeft"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationCubeFromRight:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"cube"];
[animation
setSubtype:@"fromRight"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationCubeFromTop:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"cube"];
[animation
setSubtype:@"fromTop"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationCubeFromBottom:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"cube"];
[animation
setSubtype:@"fromBottom"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationSuckEffect:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"suckEffect"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationRippleEffect:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"rippleEffect"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationCameraOpen:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"cameraIrisHollowOpen"];
[animation
setSubtype:@"fromRight"];
[view.layer
addAnimation:animation forKey:nil];
}
+
(void)animationCameraClose:(UIView *)view
{
CATransition *animation =
[CATransition animation];
[animation
setDuration:0.35f];
[animation
setFillMode:kCAFillModeForwards];
[animation
setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation
setType:@"cameraIrisHollowClose"];
[animation
setSubtype:@"fromRight"];
[view.layer
addAnimation:animation forKey:nil];
}
@end