商品添加购物车效果

2018-06-01  本文已影响0人  梁苏珍

    CGPoint_endPoint;

 UIButton*  butOne = [[UIButton alloc]init];

    butOne.frame=CGRectMake(100,100,50,50);

    butOne.backgroundColor = [UIColor yellowColor];

    [butOneaddTarget:self action:@selector(clickBut:) forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:butOne];

    UIButton * butTwo = [[UIButton alloc]init];

    butTwo.frame = CGRectMake(100, 10+CGRectGetMaxY(butOne.frame), 50, 50);

    butTwo.backgroundColor = [UIColor yellowColor];

    [butTwoaddTarget:self action:@selector(clickBut:) forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:butTwo];

//终点

    UIButton* cornerView1 = [[UIButtonalloc]init];

    cornerView1.frame = CGRectMake(self.view.frame.size.width-50, self.view.frame.size.height-60, 50, 50);

    cornerView1.backgroundColor = [UIColor redColor];

    [cornerView1addTarget:self action:@selector(clickBut:) forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:cornerView1];

    _endPoint= cornerView1.center;

点击方法

-(void)clickBut:(UIButton*)but

{

    CGPointcar =but.center;

    //控点

    CGPointcontrolPoint =CGPointMake(_endPoint.x, car.y);

    //创建一个layer

    CALayer *layer = [CALayer layer];

    layer.frame=CGRectMake(0,0,40,40);

    layer.position= car;

    layer.backgroundColor = [UIColor redColor].CGColor;

    layer.cornerRadius = layer.frame.size.width/2;

    layer.masksToBounds=YES;

    [self.view.layer addSublayer:layer];

    //创建关键帧

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    animation.delegate=self;

    //动画时间

    animation.duration=5;

    //当动画完成,停留到结束位置

    animation.removedOnCompletion = NO;

    animation.fillMode = kCAFillModeForwards;

    //当方法名字遇到create,new,copy,retain,都需要管理内存

    CGMutablePathRef path = CGPathCreateMutable();

    //设置起点

    CGPathMoveToPoint(path,NULL, car.x, car.y);

    CGPathAddQuadCurveToPoint(path,NULL, controlPoint.x, controlPoint.y,_endPoint.x,_endPoint.y);

    //设置动画路径

    animation.path= path;

    //执行动画

    [layeraddAnimation:animationforKey:nil];

    //释放路径

    CGPathRelease(path);

}

上一篇下一篇

猜你喜欢

热点阅读