IOS|IOS 实现加入购物车的效果

IOS|IOS 实现加入购物车的效果
文章图片

代码示例:


这是临时写的,代码粗糙,只希望能有所帮助吧!

//
//WWCRootViewController.m
//TestCAOrUIViewAnimationApp7-30
//
//Created by Whitney.c on 15/7/30.
//Copyright (c) 2015年 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.
//


#import "WWCRootViewController.h"


@interface WWCRootViewController ()


@end


@implementation WWCRootViewController


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

NSLog(@"viewDidLoad ... ");

self.view.backgroundColor = [UIColor whiteColor];

//[self loadSelfLayoutSubViews];
}






-(void)loadView
{
[super loadView];


[self loadSelfLayoutSubViews];
}




-(void)loadSelfLayoutSubViews
{

// 加入购物车的按钮
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 80, 100, 35);
[btn setTitle:@"$900.99" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(addGodToMarket:) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor grayColor]];
[btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
[self.view addSubview:btn];


// 购物车


cartImgV = [[UIImageView alloc] init];
cartImgV.image = [UIImage imageNamed:@"cart_empty"];
cartImgV.backgroundColor = [UIColor clearColor];
cartImgV.frame = CGRectMake(250, 500, 35, 35);

[self.view addSubview:cartImgV];

}


-(void)addGodToMarket:(UIButton*)bt
{
NSString *price =bt.titleLabel.text ;
NSLog(@"price : %@" ,price);

// 加入购物车动画效果

CALayer *transitionLayer = [[CALayer alloc] init];
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
transitionLayer.opacity = 1.0;
transitionLayer.contents = bt.titleLabel.layer.contents;
transitionLayer.frame = [[UIApplication sharedApplication].keyWindow convertRect:bt.titleLabel.bounds fromView:bt.titleLabel];
[[UIApplication sharedApplication].keyWindow.layer addSublayer:transitionLayer];
[CATransaction commit]; // 提交动画


// 路径曲线
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:transitionLayer.position];
CGPoint toPoint = CGPointMake(cartImgV.center.x, cartImgV.center.y);
[movePath addQuadCurveToPoint:toPoint controlPoint:CGPointMake(cartImgV.center.x
, transitionLayer.position.x-100)];

//关键帧
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.path = movePath.CGPath;
positionAnimation.removedOnCompletion = YES;

CAAnimationGroup *group = [CAAnimationGroup animation];
group.beginTime = CACurrentMediaTime();
group.duration = 0.6;
group.animations = [NSArray arrayWithObjects:positionAnimation, nil];
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
group.delegate = self;
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
group.autoreverses = NO;


[transitionLayer addAnimation:group forKey:@"opacity"];

[self performSelector:@selector(addCartFinished:) withObject:transitionLayerafterDelay:0.6f];
}


-(void)addCartFinished:(CALayer*)transitionLayer
{
transitionLayer.opacity = 0; //透明

}




- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/


@end

【IOS|IOS 实现加入购物车的效果】

    推荐阅读