博客> 购物车添加效果 点击后动画添加到购物车
购物车添加效果 点击后动画添加到购物车
2017-05-25 17:57 评论:3 阅读:1270 guohutiger12
ios 购物车动画 点击CELL 图标漂浮 控件不移动
  • (void)viewDidLoad:

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; _imageView.image = [UIImage imageNamed:@"list_icon_music_radio.png"]; _imageView.center = CGPointMake(self.view.frame.size.width - 70,460); [self.view addSubview:_imageView]; // label _cntLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50,440, 20, 20)]; _cntLabel.textColor = customColor; _cntLabel.textAlignment = NSTextAlignmentCenter; _cntLabel.font = [UIFont boldSystemFontOfSize:13]; _cntLabel.backgroundColor = [UIColor whiteColor]; _cntLabel.layer.cornerRadius = CGRectGetHeight(_cntLabel.bounds)/2; _cntLabel.layer.masksToBounds = YES; _cntLabel.layer.borderWidth = 1.0f; _cntLabel.layer.borderColor = customColor.CGColor; [self.view addSubview:_cntLabel]; if (_cnt == 0) { _cntLabel.hidden = YES; }

    self.path = [UIBezierPath bezierPath]; [_path moveToPoint:CGPointMake(50, 150)]; [_path addQuadCurveToPoint:CGPointMake(270, 300) controlPoint:CGPointMake(150, 20)];

//滑动tableview的时候 图标保持漂浮在cell之上 不移动

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView { _imageView.center = CGPointMake(self.view.frame.size.width - 70,self.tableView.contentOffset.y+460); [self.tableView bringSubviewToFront:_imageView]; _cntLabel.center = CGPointMake(self.view.frame.size.width - 50, self.tableView.contentOffset.y +440); [self.tableView bringSubviewToFront:_cntLabel]; }

//点击cell的时候 开始动画添加

  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath { CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath]; CGRect rect = [self.tableView convertRect:rectInTableView toView:[tableView superview]]; [_path removeAllPoints]; [_path moveToPoint:CGPointMake(rect.origin.x,rect.origin.y)]; [_path addQuadCurveToPoint:_imageView.center controlPoint:CGPointMake(rect.origin.x, 20)]; }

//实现动画的函数 -(void)startAnimation {

} -(void)groupAnimation { CAKeyframeAnimation animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; animation.path = _path.CGPath; animation.rotationMode = kCAAnimationRotateAuto; CABasicAnimation expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; expandAnimation.duration = 0.5f; expandAnimation.fromValue = [NSNumber numberWithFloat:1]; expandAnimation.toValue = [NSNumber numberWithFloat:2.0f]; expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
narrowAnimation.beginTime = 0.5;
narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
narrowAnimation.duration = 1.5f;
narrowAnimation.toValue = [NSNumber numberWithFloat:0.5f];

narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

CAAnimationGroup *groups = [CAAnimationGroup animation];
groups.animations = @[animation,expandAnimation,narrowAnimation];
groups.duration = 2.0f;
groups.removedOnCompletion=NO;
groups.fillMode=kCAFillModeForwards;
groups.delegate = self;
[layer addAnimation:groups forKey:@"group"];

}- 1. - list text here

-(void)animationDidStop:(CAAnimation )anim finished:(BOOL)flag { // [anim def]; if (anim == [layer animationForKey:@"group"]) { _btn.enabled = YES; [layer removeFromSuperlayer]; layer = nil; _cnt++; if (_cnt) { _cntLabel.hidden = YES; } CATransition animation = [CATransition animation]; animation.duration = 0.25f; _cntLabel.text = [NSString stringWithFormat:@"%d",_cnt]; [_cntLabel.layer addAnimation:animation forKey:nil];

    CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    shakeAnimation.duration = 0.25f;
    shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
    shakeAnimation.toValue = [NSNumber numberWithFloat:5];
    shakeAnimation.autoreverses = YES;
    [_imageView.layer addAnimation:shakeAnimation forKey:nil];
}

}

收藏
2
sina weixin mail 回到顶部