OC 使用贝塞尔曲线简单的实现点击添加到购物车效果

1.使用扩展的方式对UIView添加方法:

- (void)animationStartPoint:(CGPoint)start endPoint:(CGPoint)end didStopAnimation:(void (^)(void))event{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:start];
    //设置控制点
    CGPoint point1 = CGPointZero;
    CGPoint point2 = CGPointZero;
    if (start.x > end.x) {
        point1 = CGPointMake((start.x - end.x) / 4 * 3 + end.x, start.y - 40);
        point2 = CGPointMake((start.x - end.x) / 4 + end.x, (end.y - start.y) / 2 + start.y);
    }else {
        point1 = CGPointMake((end.x - start.x) / 4 + start.x , start.y - 40);
        point2 = CGPointMake((end.x - start.x) / 4 * 3 + start.x, (end.y - start.y) / 2 + start.y);
    }
    [path addCurveToPoint:end controlPoint1:point1 controlPoint2:point2];
    //路径
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    //缩放
    CABasicAnimation *baAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //控制缩放的倍数,根据项目自定
    baAnimation.fromValue = @1;
    baAnimation.toValue = @1;
    baAnimation.autoreverses = YES;
    //动画组合
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,baAnimation];
    groups.duration = 0.5;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = self;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [self.layer addAnimation:groups forKey:nil];
    self.animStop = event;
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.animStop();
}
- (void)setAnimStop:(void (^)(void))animStop {
    objc_setAssociatedObject(self, @"animStop", animStop, OBJC_ASSOCIATION_COPY);
}
- (void (^)(void))animStop {
    return objc_getAssociatedObject(self, @"animStop");
}

2.使用:

#import "AddToCarViewController.h"

@interface AddToCarViewController ()
@property (nonatomic, strong)UIButton *addBtn;
@property (nonatomic, strong)UIButton *carBtn;
@end

@implementation AddToCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.addBtn = [[UIButton alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 60, UI_SCREEN_HEIGHT / 2, 20, 20)];
    self.addBtn.layer.cornerRadius = 10;
    self.addBtn.layer.masksToBounds = YES;
    [self.addBtn setTitle:@"+" forState:UIControlStateNormal];
    [self.addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.addBtn.backgroundColor = [UIColor purpleColor];
    [self.addBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.addBtn];
    
    self.carBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, UI_SCREEN_HEIGHT - HOME_INDICATOR_HEIGHT - 60, 120, 60)];
    self.carBtn.backgroundColor = [UIColor purpleColor];
    [self.carBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.carBtn setTitle:@"购物车" forState:UIControlStateNormal];
    [self.view addSubview:self.carBtn];
}
//添加
- (void)addAction {
    UIView *tempView = [[UIView alloc]initWithFrame:self.addBtn.frame];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
    [tempView animationStartPoint:tempView.center endPoint:self.carBtn.center didStopAnimation:^{
        [tempView removeFromSuperview];
    }];
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值