美文网首页
购物车 及动画效果

购物车 及动画效果

作者: LZM轮回 | 来源:发表于2016-06-20 15:14 被阅读82次

最近在做一个商城, 购物车功能的实现.效果如下

说一下代码结构


IMG_2088.PNG Snip20170724_1.png
  1. 购物车控制器
    #import <UIKit/UIKit.h>

     @interface FCShopCartViewController : UIViewController
    
     @end
    
  2. 商品模型, 由于这是初版, 很多数据待定, 所有模型里商品字段不完善, 但一个基本的商品信息, 有了.
    #import <Foundation/Foundation.h>

     @interface FCShopCartModel : NSObject
    
     /// 商店名字
     @property (nonatomic, copy) NSString *name;
     /// 商店 id
     @property (nonatomic, assign) int shopId;
     /// 商店类型
     @property (nonatomic, assign) BOOL isWJShop;
     /// 商品数组 内部是商品模型
     @property (nonatomic, strong) NSMutableArray *goodsArray;
     /// 商店是否选中状态
     @property (nonatomic, assign) BOOL isSelectShop;
    
     /**
     字典转模型
     @param dict 传入的商店字典
     @return 创建好的自身
      */
     + (instancetype)createShopCartModelWithDict:(NSDictionary *)dict;
     @end
    
    
     @interface GoodsModel : NSObject
     /// skuId
     @property (nonatomic, assign) NSInteger skuId;
     /// 商品单价
     @property (nonatomic, assign) float price;
     /// 优惠金额
     @property (nonatomic, assign) float discountAmount;
     /// id
     @property (nonatomic, assign) NSInteger ID;
     /// 订单金额
     @property (nonatomic, assign) float amount;
     /// 订单创建时间
     @property (nonatomic, copy) NSString *createdTime;
     /// 订单更改时间
     @property (nonatomic, copy) NSString *updatedTime;
     /// 购物车中商品数量
     @property (nonatomic, assign) int quantity;
     /// 商品名称
     @property (nonatomic, copy) NSString *title;
      /// 商品是否选中状态
     @property (nonatomic, assign) BOOL isSelectGood;
    
     + (instancetype)createGoodsModelWithDict:(NSDictionary *)dict;
    @end
    

3 . 购物车管理单例工具类, 该类的作用就是全局使用, 购物车数据 逻辑尽可能的与界面显示分离,
#import <Foundation/Foundation.h>
@interface FCShopCartManage : NSObject
/// 传入一个购物车数组
@property (nonatomic, strong) NSMutableArray *cartArray;

/// 当前购物车内商品应该付款总价格
@property (nonatomic, assign) CGFloat payAmount;
/**
 实例化购物车对象
 */
+ (instancetype)shareInstance;
/**
 购物车内商品是否全部选中
 @param isSelect 是否选中的状态
 */
- (void)goodsWhetherAllTheSelectedWithSelect:(BOOL)isSelect;
/**
 计算购物车内商品总价格
 */
- (CGFloat)calculateAllGoodsPrice;
/**
 该组内商品是否全部选中
 @param isSelect 该组是否选中
 */
- (void)allProductIsSelectedInTheGroupWithSection:(int)section andSelect:(BOOL)isSelect;
/**
 清空购物车内所有商品
 */
- (void)clearAllTheGoods;
/**
 添加商品到购物车时需要的动画效果
 @param startPoint      起点坐标
 @param endPoint        终点坐标
 @param completionBlock 动画完成后的回调
 */
- (void)addTheAnimationOfTheGoodsWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint completion:(void (^)())completionBlock;
@end
  1. 这里注重介绍下购物车动画, 点击添加按钮, 弹出一个商品图片, 做抛物线降落, 降落到购物车底部价格上面, 图片消失, 价格变动.

主要是购物车工具类里的这个方法:

 /**
 添加商品到购物车时需要的动画效果
 @param startPoint      起点坐标
 @param endPoint        终点坐标
 @param completionBlock 动画完成后的回调
 */
- (void)addTheAnimationOfTheGoodsWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint completion:(void (^)())completionBlock;

使用:
#pragma mark cell 内部商品添加按钮点击事件
/**
添加商品数量
@param indexPath 该商品所在cell 的index
*/
- (void)shopCartCellAddGoodsNumWithCellIndex:(NSIndexPath *)indexPath isSelect:(BOOL)isSelect centerPoint:(CGPoint)centerPoint{

// 1. 拿到该商品所属商店的信息
FCShopCartModel *cartM = self.cartManage.cartArray[indexPath.section];
// 2. 拿到该商品信息
GoodsModel *gdM = cartM.goodsArray[indexPath.row];
gdM.quantity += 1;

// 如果当前商品是选中状态 更改底部商品总价格
if(isSelect){
    
    //起点
    CGPoint startPoint = [self.cartTableV convertPoint:centerPoint toView:[UIApplication sharedApplication].keyWindow];
    
    // 终点
    self.endAnimationPoint = [self.allPriceLB convertPoint:self.allPriceLB.center toView:[UIApplication sharedApplication].keyWindow];
    self.endAnimationPoint = CGPointMake(150, self.endAnimationPoint.y);
    FCWeakSelf(self);
    [self.cartManage addTheAnimationOfTheGoodsWithStartPoint:startPoint endPoint:self.endAnimationPoint completion:^{
        
        // 当前价格 = 原有总价格 + 当前商品价格 - 优惠价格;
        weakself.cartManage.payAmount = (gdM.price - gdM.discountAmount) + weakself.cartManage.payAmount;
        
        // 赋值更改底部商品总价格
        // 2. 计算购物车内价格 并赋值
        weakself.allPriceLB.text = [NSString stringWithFormat:@"合计: %.2f",weakself.cartManage.payAmount];
    }];
}

// 发送网络请求 添加商品
NSDictionary *parameter = @{@"skuId":@(gdM.skuId),
                            @"quantity":@(1)};
[FCNetworking FCPOST:INCREAMENT_ITEM parameters:parameter success:^(NSDictionary *responseObject) {
    
} error:^(NSError *error) {
    
}];
}

相关文章

网友评论

      本文标题:购物车 及动画效果

      本文链接:https://www.haomeiwen.com/subject/femwdttx.html