前段時間產品經理讓給加入購物車的商品加一個動畫,我說:”什么樣子的“。然后產品默默的打開京東說:“就這個樣子的”。我:”。。。。“
內容很簡單,只是希望可以給大家使用的時候方便一點,廢話不多說,直接上代碼。
1.首先創建一個繼承于NSObject的工具類:AddCartTool
//AddCartTool.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//必須導入QuartzCore包
#import <QuartzCore/QuartzCore.h>
@protocol AddCartToolDelegate <NSObject>
/** 添加購物車結束的回調 */
- (void)addCartFinished;
@end
@interface AddCartTool : NSObject
@property (nonatomic, weak) id <AddCartToolDelegate> delegate;
/*需要移動的自定義view,這里是為了在添加動畫和在結束時移除*/
@property (nonatomic, strong) UIView * moveView;
/**
* 將自定義view從起點移動到終點
*
* @param moveView 需要移動的自定義view
* @param start 起點的坐標
* @param end 終點的坐標
*/
- (void)addView:(UIView *)moveView from:(CGPoint)start to:(CGPoint)end;
@end
2.具體的實現過程
#import "AddCartTool.h"
@interface AddCartTool ()<CAAnimationDelegate>
@end
@implementation AddCartTool
- (void)addView:(UIView *)moveView from:(CGPoint)start to:(CGPoint)end;
{
self.moveView = moveView;
/*
首先根據傳入的點畫三次貝塞爾曲線
畫三次貝塞爾曲線的關鍵方法,以三個點畫一段曲線,一般和moveToPoint:配合使用
我這里偷懶了,可以傳入4個點去畫的更詳細
其組成是起始端點(moveToPoint:)+控制點1(controlPoint1:)+控制點2(controlPoint2:)+終止端點(addCurveToPoint:)
*/
UIBezierPath *path= [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(start.x, start.y)];
[path addCurveToPoint:CGPointMake(end.x, end.y)
controlPoint1:CGPointMake(start.x, start.y)
controlPoint2:CGPointMake(start.x - 50, start.y - 200)];
//創建動畫
[self addAnimationWithPath:path];
}
#pragma mark - 創建動畫
-(void)addAnimationWithPath:(UIBezierPath *)path
{
//關鍵幀動畫支持傳入一套數值或一個路徑來完成動畫,先根據貝塞爾曲線創建一個關鍵幀動畫確認動畫路徑
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//傳入路徑
keyAnimation.path = path.CGPath;
//設置動畫速度設置
keyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
/*
這是的動畫速度的不同設置,可以嘗試一下每個不同設置的區別
kCAMediaTimingFunctionLinear 線性(勻速)
kCAMediaTimingFunctionEaseIn 先慢
kCAMediaTimingFunctionEaseOut 后慢
kCAMediaTimingFunctionEaseInEaseOut 先慢 后慢 中間快
kCAMediaTimingFunctionDefault 默認
*/
//設置moveView的尺寸大小變化
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration = 0.5;
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:0.4];
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//設置moveView的旋轉
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2];
rotationAnimation.cumulative = YES;
rotationAnimation.duration = 0.15;
rotationAnimation.repeatCount = MAXFLOAT;
//創建一個動畫組,把所有的動畫都加入動畫組同時執行
CAAnimationGroup *groups = [CAAnimationGroup animation];
groups.animations = @[keyAnimation,rotationAnimation,scaleAnimation];
groups.duration = 0.5;
groups.removedOnCompletion = YES;
groups.delegate = self;
//添加動畫到layer層
[self.moveView.layer addAnimation:groups forKey:nil];
/*在addAnimation:forKey:方法中,也可以給這個動畫設置一個鍵,
可以在其他地方將其取出來,進行一些操作,比如刪除等。
這也充分體現了kvc的靈活。*/
/*
可以根據不同的需求設置的KeyPath(好多我也沒用過??,如果有興趣可以都試試)
animationWithKeyPath的值:
transform.scale = 比例轉換
transform.scale.x = 寬的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 旋轉
opacity = 透明度
backgroundColor = 背景顏色
cornerRadius = 圓角
margin = 間隔
borderWidth = 邊框寬度
zPosition
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
*/
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//在動畫執行結束時移除移動的自定義view
self.moveView = nil;
//在動畫執行結束時需要做的一些操作,例如讓購物車縮放
if (self.delegate && [self.delegate respondsToSelector:@selector(addCartFinished)]) {
[self.delegate addCartFinished];
}
}
并沒有什么難點,只是希望可以幫到大家,不喜勿噴哈。