CAKeyframeAnimation CABasicAnimation 結合做加入購物車的動畫

開始動畫

  • 創建一個Calyer的全局變量,
 CALayer  *_layer;
  • 設置動畫過程(注意動畫的過程是移動一個ImageView)
//添加加入購物車動畫
- (void)addAnimationPath:(HSGoodsListTableViewCell *)cell
{
    HSGoodsListTableViewCell *bolthCell=(HSGoodsListTableViewCell *)cell;
    _layer = [CALayer layer];
    _layer.contentsGravity = kCAGravityResizeAspectFill;
    UIImageView *imagV = bolthCell.goodsImage;
    _layer.contents = (id)imagV.layer.contents;
    CGRect frame = [bolthCell.goodsImage convertRect:bolthCell.goodsImage.frame toView:self.view];
    _layer.bounds=frame;
    
    CGPoint p=[bolthCell.goodsImage convertPoint:bolthCell.goodsImage.center toView:self.view];
    
    //創建貝塞爾曲線
    UIBezierPath *bPath=[UIBezierPath bezierPath];
    [bPath moveToPoint:p];
    [bPath addQuadCurveToPoint:CGPointMake(shopCarBtnCenter.x,shopCarBtnCenter.y) controlPoint:CGPointMake(shopCarBtnCenter.x, p.y)];
    [self groupAnimation:bPath];
    [self.view.layer addSublayer:_layer];
    
}

//構建一組動畫
-(void)groupAnimation:(UIBezierPath *)path
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    //動畫縮小
    CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    narrowAnimation.beginTime = 0;
    narrowAnimation.fromValue = [NSNumber numberWithFloat:0.6f];
    narrowAnimation.duration = 0.5f;
    narrowAnimation.toValue = [NSNumber numberWithFloat:0.1f];
    narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //動畫組建
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,narrowAnimation];
    groups.duration = 0.5f;
    groups.removedOnCompletion=YES;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    //根據路線畫路線或填充
    [_layer addAnimation:groups forKey:nil];
}
//移除動畫
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [_layer removeFromSuperlayer];
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,551評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,141評論 5 13
  • 在iOS實際開發中常用的動畫無非是以下四種:UIView動畫,核心動畫,幀動畫,自定義轉場動畫。 1.UIView...
    請叫我周小帥閱讀 3,142評論 1 23
  • Core Animation Core Animation,中文翻譯為核心動畫,它是一組非常強大的動畫處理API,...
    45b645c5912e閱讀 3,047評論 0 21
  • 如果想讓事情變得順利,只有靠自己--夏爾·紀堯姆 上一章介紹了隱式動畫的概念。隱式動畫是在iOS平臺創建動態用戶界...
    夜空下最亮的亮點閱讀 1,978評論 0 1