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:transitionLayer afterDelay: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



發佈了26 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章