KWMBrandCaramelCell.m 13.1 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
//
//  KWMBrandCaramelCell.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/29.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMBrandCaramelCell.h"
#import "UIImageView+WebCache.h"
#import "KWMUserDao.h"
#import "KWMStringUtil.h"
#import "KWMImageUtil.h"
#import "KWMVariants.h"


@implementation KWMBrandCaramelCell

#define Width UI_SCREEN_WIDTH*0.5
#define ruleWidth UI_SCREEN_WIDTH/2 - 20
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self initView];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)initView{

    self.lbPriceLeft.textColor = [UIColor colorWithRed:151.0/255 green:151.0/255 blue:151.0/255 alpha:1];
    self.lbDiscountLeft.textColor = [UIColor colorWithRed:241.0/255 green:146.0/255 blue:154.0/255 alpha:1];
    self.lbPriceRight.textColor = [UIColor colorWithRed:151.0/255 green:151.0/255 blue:151.0/255 alpha:1];
    self.lbDiscountRight.textColor = [UIColor colorWithRed:241.0/255 green:146.0/255 blue:154.0/255 alpha:1];
    [self initRule];
    [self addGesture];
}

- (void)initRule{
    
    CGFloat height = self.vRootLeft.frame.size.height - 84;
    _vRuleLeft = [[KWMRuleView alloc] initWithFrame:CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height)];
    [self.ivIconLeft addSubview:_vRuleLeft];
    
    height = self.vRootRight.frame.size.height - 84;
    _vRuleRight = [[KWMRuleView alloc] initWithFrame:CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height)];
    [self.ivIconRight addSubview:_vRuleRight];
}

//添加手势
- (void)addGesture{
    //添加图片手势
    UITapGestureRecognizer *leftImagGeustre = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickLeftImage:)];
    [self.ivIconLeft addGestureRecognizer:leftImagGeustre];
    
     UITapGestureRecognizer *rightImagGeustre = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickRightImage:)];
    [self.ivIconRight addGestureRecognizer:rightImagGeustre];
    
    //添加收起标尺背景手势
    UITapGestureRecognizer *leftTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickLeftBackground:)];
    [self.vLBackground addGestureRecognizer:leftTapGesture];
    
    UITapGestureRecognizer *rightTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickRightBackground:)];
    [self.vRBackground addGestureRecognizer:rightTapGesture];
    
}

- (void)onClickLeftImage:(UITapGestureRecognizer *)leftImagGeustre{
    if ([self.delegate respondsToSelector:@selector(kwm_onClickLeftIamge:)]) {
        [self.delegate kwm_onClickLeftIamge:self];
    }
}

- (void)onClickRightImage:(UITapGestureRecognizer *)rightImagGeustre{
    if ([self.delegate respondsToSelector:@selector(kwm_onClickRightIamge:)]) {
        [self.delegate kwm_onClickRightIamge:self];
    }
}

- (void)onClickLeftBackground:(UITapGestureRecognizer *)leftTapGesture{
    self.btnRuleLeft.selected = !self.btnRuleLeft.selected;
    self.vLBackground.hidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _vRuleLeft.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height);
    }];
}

- (void)onClickRightBackground:(UITapGestureRecognizer *)rightTapGesture{
    self.btnRuleRight.selected = !self.btnRuleRight.selected;
    self.vRBackground.hidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _vRuleRight.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height);
    }];
}

//点击左边标尺
- (IBAction)onClickRuleLeftBtn:(id)sender {
    [self setLeftRlueAnimation];
}

//点击右边标尺
- (IBAction)onClickRuleRightBtn:(id)sender {
    [self setRightRlueAnimation];
}

- (void)setLeftRlueAnimation{
    self.btnRuleLeft.selected = !self.btnRuleLeft.selected;
    self.vLBackground.userInteractionEnabled = NO;
//    self.btnRuleLeft.userInteractionEnabled = NO;
//    [UIView animateWithDuration:0.5 animations:^{
//        if (self.btnRuleLeft.selected) {
//            _vRuleLeft.frame = CGRectMake(0, 0, ruleWidth, self.ivIconLeft.frame.size.height);
//        }else{
//            _vRuleLeft.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height);
//        }
//    } completion:^(BOOL finished) {
        self.vLBackground.userInteractionEnabled = YES;
        self.vLBackground.hidden = NO;
        self.btnRuleLeft.userInteractionEnabled = YES;
    
        if ([self.delegate respondsToSelector:@selector(kwm_openRule:Or:And:)]) {
            [self.delegate kwm_openRule:_leftProduct Or:_leftDataProduct And:self];
        }
    
    
//    }];
}

- (void)setRightRlueAnimation{
    self.btnRuleRight.selected = !self.btnRuleRight.selected;
//    self.vRBackground.userInteractionEnabled = NO;
//    self.btnRuleRight.userInteractionEnabled = NO;
//    [UIView animateWithDuration:0.5 animations:^{
//        if (self.btnRuleRight.selected) {
//            _vRuleRight.frame = CGRectMake(0, 0, ruleWidth, self.ivIconRight.frame.size.height);
//        }else{
//            _vRuleRight.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height);
//        }
//    } completion:^(BOOL finished) {
        self.vRBackground.hidden = NO;
        self.vRBackground.userInteractionEnabled = YES;
        self.btnRuleRight.userInteractionEnabled = YES;
        if ([self.delegate respondsToSelector:@selector(kwm_openRule:Or:And:)]) {
            [self.delegate kwm_openRule:_rightProduct Or:_rightDataProduct And:self];
        }
//    }];
}

- (void)initFrame{
    
    self.vRBackground.hidden = YES;
    self.vRBackground.userInteractionEnabled = NO;
    self.btnRuleRight.userInteractionEnabled = YES;
    self.vLBackground.hidden = YES;
    self.vLBackground.userInteractionEnabled = NO;
    self.btnRuleLeft.userInteractionEnabled = YES;
    
    _btnRuleRight.selected = NO;
    _btnRuleLeft.selected = NO;
    _vRuleLeft.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconLeft.frame.size.height);
    _vRuleRight.frame = CGRectMake(ruleWidth, 0, ruleWidth, self.ivIconRight.frame.size.height);
}

//展开标尺
- (void)openRule:(BOOL)isLeft{
    if (isLeft) {//展开左边标尺
        self.btnRuleLeft.selected = YES;
        self.vLBackground.userInteractionEnabled = NO;
        self.btnRuleLeft.userInteractionEnabled = NO;
        [UIView animateWithDuration:0.5 animations:^{
           
            _vRuleLeft.frame = CGRectMake(0, 0, ruleWidth, self.ivIconLeft.frame.size.height);
        } completion:^(BOOL finished) {
            self.vLBackground.userInteractionEnabled = YES;
            self.vLBackground.hidden = NO;
            self.btnRuleLeft.userInteractionEnabled = YES;
        }];
    }else{//展开右边标尺
        self.btnRuleRight.selected = YES;
        self.vRBackground.userInteractionEnabled = NO;
        self.btnRuleRight.userInteractionEnabled = NO;
        [UIView animateWithDuration:0.5 animations:^{
            _vRuleRight.frame = CGRectMake(0, 0, ruleWidth, self.ivIconRight.frame.size.height);
        } completion:^(BOOL finished) {
            self.vRBackground.hidden = NO;
            self.vRBackground.userInteractionEnabled = YES;
            self.btnRuleRight.userInteractionEnabled = YES;
        }];
    }
}

- (void)setPrice:(NSNumber *)price WithComparePrice:(NSNumber *)compareAtPrice And:(BOOL)isLeft{
    if(price == nil){
        price = @(0);
    }
    if(compareAtPrice == nil){
        compareAtPrice = @(0);
    }
    if( compareAtPrice.integerValue!=0 ){
lee committed
199 200
//        NSString *oldString = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:compareAtPrice]];
        NSString *oldString = compareAtPrice.priceValueFormatted;
houweibin committed
201 202 203 204 205 206 207 208
        //中划线
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:oldString attributes:attribtDic];
        
        if (isLeft) {
            self.priceLeft.constant = -30;
            self.lbDiscountLeft.hidden = NO;
            self.lbPriceLeft.attributedText = attribtStr;
lee committed
209 210
//            self.lbDiscountLeft.text = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:price]];
            self.lbDiscountLeft.text = price.priceValueFormatted;
houweibin committed
211 212 213 214
        }else{
            self.priceRight.constant = -30;
            self.lbDiscountRight.hidden = NO;
            self.lbPriceRight.attributedText = attribtStr;
lee committed
215 216
//            self.lbDiscountRight.text =  [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:price]];
            self.lbDiscountRight.text = price.priceValueFormatted;
houweibin committed
217 218 219 220 221
        }
    }else{
        if (isLeft) {
            self.priceLeft.constant = 0;
            self.lbDiscountLeft.hidden = YES;
lee committed
222
            self.lbDiscountLeft.text = nil;
lee committed
223 224
//            self.lbPriceLeft.text =  [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring: price]];
            self.lbPriceLeft.text = price.priceValueFormatted;
houweibin committed
225 226 227
        }else{
            self.priceRight.constant = 0;
            self.lbDiscountRight.hidden = YES;
lee committed
228
            self.lbDiscountRight.text = nil;
lee committed
229 230
//            self.lbPriceRight.text = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:price]];
            self.lbPriceRight.text = price.priceValueFormatted;
houweibin committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        }
    }
}

//搜索Model赋值
- (void)setDataProduct:(KWMDataProduct *)leftDataProduct rightProduct:(KWMDataProduct *)rightDataProduct{
    [self.ivIconLeft setImage:[UIImage imageNamed:@"ic_loading"]];
    [self.ivIconRight setImage:[UIImage imageNamed:@"ic_loading"]];
    
    [self initFrame];
    if(leftDataProduct == nil && rightDataProduct == nil){
        return;
    }
    self.leftDataProduct = leftDataProduct;
    self.rightDataProduct = rightDataProduct;
    if(leftDataProduct != nil){
        [self.vRuleLeft setDataProduct:leftDataProduct];
        
        self.lbBrandLeft.text = leftDataProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandLeft.text = leftDataProduct.vendor;
        }
        
houweibin committed
254
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:leftDataProduct ImageSize:NormalImage];
houweibin committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconLeft sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
        
        KWMVariants *variant = leftDataProduct.variants.firstObject;
        if(variant == nil){
            return;
        }
        [self setPrice:variant.price WithComparePrice:variant.compareAtPrice And:YES];
    }
    if(rightDataProduct != nil){
        [self.vRuleRight setDataProduct:rightDataProduct];
        self.vRootRight.hidden = NO;
        
        self.lbBrandRight.text = rightDataProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandRight.text = rightDataProduct.vendor;
        }
houweibin committed
272
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:rightDataProduct ImageSize:NormalImage];
houweibin committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconRight sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];

        KWMVariants *variant = rightDataProduct.variants.firstObject;
        if(variant == nil){
            return;
        }
        [self setPrice:variant.price WithComparePrice:variant.compareAtPrice And:NO];
    }else{
        self.vRootRight.hidden = YES;
    }
}

- (void)setData:(BUYProduct *)leftProduct rightProduct:(BUYProduct *)rightProduct{
    
    [self initFrame];
    if(leftProduct == nil && rightProduct == nil){
        return;
    }
    self.leftProduct = leftProduct;
    self.rightProduct = rightProduct;
    if(leftProduct != nil){
        [self.vRuleLeft setData:leftProduct];
        self.lbBrandLeft.text = leftProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandLeft.text = leftProduct.vendor;
        }
houweibin committed
300
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:leftProduct ImageSize:NormalImage];
houweibin committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconLeft sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
        BUYProductVariant *productVariant = leftProduct.variants.firstObject;
        if(productVariant == nil){
            return;
        }
        
        [self setPrice:productVariant.price WithComparePrice:productVariant.compareAtPrice And:YES];
    }
    if(rightProduct != nil){
        [self.vRuleRight setData:rightProduct];
        self.vRootRight.hidden = NO;
        self.lbBrandRight.text = rightProduct.title;
        if(self.isShowBrandTitle){
            self.lbBrandRight.text = rightProduct.vendor;
        }
houweibin committed
317
        NSString *imageUrl = [KWMImageUtil getProductImageUrl:rightProduct ImageSize:NormalImage];
houweibin committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331
        NSURL *imageURL = [NSURL URLWithString:imageUrl];
        [self.ivIconRight sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"ic_loading"]];
        BUYProductVariant *productVariant= rightProduct.variants.firstObject;
        if(productVariant == nil){
            return;
        }
        [self setPrice:productVariant.price WithComparePrice:productVariant.compareAtPrice And:NO];
        
    }else{
        self.vRootRight.hidden = YES;
    }
}

@end