KWMFirstDetailView.m 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
//
//  KWMProductDetailView.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/1/11.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMFirstDetailView.h"
#import "KWMImageUtil.h"
#import "KWMStringUtil.h"
#import "KWMPageControl.h"
13 14
//#import "YYWebImage/YYWebImage.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
15

16
@interface KWMFirstDetailView ()<SDCycleScrollViewDelegate>
17 18 19

@property(nonatomic,weak) IBOutlet UIView *vView;

20
@property(nonatomic,weak) IBOutlet SDCycleScrollView *imageScrollView;
21 22 23 24 25 26 27 28 29

@property(nonatomic,weak) IBOutlet UIButton *btnBrand;

@property(nonatomic,weak) IBOutlet UILabel *lbName;

@property(nonatomic,weak) IBOutlet UILabel *lbPrice;

@property(nonatomic,weak) IBOutlet UILabel *lbOldPrice;

30 31
@property(nonatomic,weak) IBOutlet UILabel *lbVariant;

32 33
@property(nonatomic,weak) IBOutlet UIView *freeShipping;

34 35 36
//单位label
@property(nonatomic,weak) IBOutlet UILabel *lbUnit;

37
//@property(nonatomic,weak) IBOutlet KWMPageControl *pageControl;
38

lee committed
39 40 41
@property (weak, nonatomic) IBOutlet UIView *sizeGuideView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *sizeGuideViewHeightConstraint;

42 43 44 45
@property (nonatomic) BUYProduct *product;

@property (nonatomic) NSMutableArray *imageArr;

46
//@property (nonatomic) NSTimer *timer;
47

48
//@property (nonatomic) BOOL isFirstScroll;
49 50 51 52 53 54 55

@property (nonatomic) BOOL isShowCNY;

- (IBAction)onClickSizeBtn:(id)sender;

- (IBAction)onClickBlandBtn:(id)sender;

houweibin committed
56
//- (IBAction)onClickBuyNow:(id)sender;
houweibin committed
57

houweibin committed
58
//- (IBAction)onClickAddToShopCart:(id)sender;
houweibin committed
59

60 61 62 63
@end

@implementation KWMFirstDetailView

64 65 66 67 68 69
//- (id)init{
//    if (self=[super init]){
//        [self addView];
//    }
//    return self;
//}
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

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self addView];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void) addView{
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                  owner:self
                                options:nil];
    NSLog(@"KWMProductDetailView%f%f",self.frame.size.width,self.frame.size.height);
    self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:self.vView];
97
    self.imageScrollView.autoScroll = false;
98 99 100 101
    self.imageScrollView.pageDotImage = [UIImage imageNamed:@"ic_page_1"];
    self.imageScrollView.currentPageDotImage = [UIImage imageNamed:@"ic_page_2"];
    self.imageScrollView.delegate = self;
    self.imageScrollView.backgroundColor = [UIColor whiteColor];
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
//-(void) addTimer{
//    if ([self.timer isValid]) {
//        return;
//    }
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(doTimer) userInfo:nil repeats:YES];
//    [self.timer setFireDate:[NSDate new]];//开启
//}

//-(void) removeTimer{
//    //结束
//    if ([self.timer isValid]) {
//        [self.timer invalidate];
//        self.timer = nil;
//    }
//}
//
////开始计时
//-(void) doTimer{
//    if (self.isFirstScroll) {
//        self.isFirstScroll = NO;
//        return;
//    }
//    NSInteger currentIndex = self.pageControl.currentPage;
//    currentIndex ++;
//    currentIndex = currentIndex % self.pageControl.numberOfPages;
////    [self.imageScrollView setContentOffset:CGPointMake(UI_SCREEN_WIDTH * currentIndex, 0) animated:YES];
//    self.pageControl.currentPage = currentIndex;
//}
132 133 134 135 136 137

-(void)setData:(BUYProduct *)product{
    if(product == nil){
        return;
    }
    self.isShowCNY = YES;
138 139
//    self.isFirstScroll = YES;
//    [self removeTimer];
140
    self.product = product;
141 142 143 144
//    [self.imageScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
//    for (UIView *subview in self.imageScrollView.subviews) {
//        [subview removeFromSuperview];
//    }
145 146 147 148 149 150 151 152 153
    [self initImage:product];
    [self initView:product];
    //[self addTimer];
}

-(void)initImage:(BUYProduct *)product{
    if(product == nil){
        return;
    }
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
//    [self.imageScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    NSInteger imageCount = product.imagesArray == nil? 0:product.imagesArray.count;
//    self.imageArr = [NSMutableArray array];
    self.imageArr = [NSMutableArray arrayWithArray:[KWMImageUtil getProductImageUrls:self.product ImageSize:BigImage]];
    self.imageScrollView.imageURLStringsGroup = self.imageArr;
    
//    CGFloat w = UI_SCREEN_WIDTH;
//    CGFloat h = UI_SCREEN_WIDTH;
//    for( int i =0;i < imageCount;i++){
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*w, 0, w, h)];
//        imageView.tag = i;
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
//        imageView.clipsToBounds = YES;
//        NSString *imageUrl = [KWMImageUtil getProductImageUrlByPosition:self.product ImageSize:BigImage Position:i];
//        [self.imageArr addObject:imageUrl];
//        NSURL *imageURL = [NSURL URLWithString:imageUrl];
//        UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)];
//        imageView.userInteractionEnabled = YES;
//        [imageView addGestureRecognizer:imageTap];
//        if(i + 1 == imageCount){
//            [imageView yy_setImageWithURL:imageURL placeholder:nil options:YYWebImageOptionProgressive completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//                if(image && !error){
//                    [self performSelector:@selector(addTimer) withObject:nil afterDelay:0.5f];
//                }
//            }];
//        }else{
//             [imageView yy_setImageWithURL:imageURL options:YYWebImageOptionProgressive];
//        }
//        
//        [self.imageScrollView addSubview:imageView];
//    }
//    self.pageControl.numberOfPages = imageCount;
//    self.pageControl.currentPage = 0;
//    self.pageControl.defaultImage = [UIImage imageNamed:@"ic_page_1"];
//    self.pageControl.currentImage = [UIImage imageNamed:@"ic_page_2"];
//    self.imageScrollView.contentSize = CGSizeMake(w*imageCount,0);
//    self.imageScrollView.pagingEnabled = YES;
191 192 193 194 195 196 197 198 199 200
}


-(void)initView:(BUYProduct *)product{
    if(product == nil){
        return;
    }
    [self.btnBrand setTitle:product.vendor forState:UIControlStateNormal];
    self.lbName.text = product.title;
    BUYProductVariant *productVariant= product.variants.firstObject;
houweibin committed
201 202
//    self.lbPrice.text = [KWMStringUtil price:productVariant.price];
    self.lbPrice.text = productVariant.price.priceValueFormatted;
203
    if (productVariant.compareAtPrice) {
houweibin committed
204 205
//        NSString *priceString = [NSString stringWithFormat:@"¥%@",[KWMStringUtil price:productVariant.compareAtPrice]];
        NSString *priceString = productVariant.compareAtPrice.priceValueFormatted;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:priceString attributes:attribtDic];
        self.lbOldPrice.attributedText = attribtStr;
    }else{
        self.lbOldPrice.hidden = YES;
    }
    
    UITapGestureRecognizer *priceTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPrice:)];
    UITapGestureRecognizer *priceTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPrice:)];
    UITapGestureRecognizer *priceTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPrice:)];
    [self.lbUnit addGestureRecognizer:priceTap1];
    [self.lbPrice addGestureRecognizer:priceTap2];
    [self.lbOldPrice addGestureRecognizer:priceTap3];
    self.lbUnit.userInteractionEnabled = YES;
    self.lbPrice.userInteractionEnabled = YES;
    self.lbOldPrice.userInteractionEnabled = YES;
222
    self.freeShipping.hidden = [product.tags containsObject:@"not-return"];
223 224
}

225 226 227 228
//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    NSInteger index = fabs(scrollView.contentOffset.x)/scrollView.frame.size.width;
//    [self.pageControl setCurrentPage:index];
//}
229

230 231
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    [self.delegate kwm_onClickImage:self.imageArr currentIndex:index];
232 233
}

234 235 236 237 238 239 240
//-(void)onClickImage:(UITapGestureRecognizer *)tapGesture{
//    if(self.delegate!=nil){
//        UIView *view = [tapGesture view];
//        [self.delegate kwm_onClickImage:self.imageArr currentIndex:view.tag];
//    }
//}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
-(void)onClickPrice:(UITapGestureRecognizer *)tapGesture{
    if(self.product == nil){
        return;
    }
    self.isShowCNY = !self.isShowCNY;
    BUYProductVariant *productVariant= self.product.variants.firstObject;
    if(self.isShowCNY){
        self.lbUnit.text = @"¥";
        self.lbPrice.text = [KWMStringUtil price:productVariant.price];
        if (productVariant.compareAtPrice) {
            NSString *priceString = [NSString stringWithFormat:@"¥%@",[KWMStringUtil price:productVariant.compareAtPrice]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:priceString attributes:attribtDic];
            self.lbOldPrice.attributedText = attribtStr;
        }
    }else{
        self.lbUnit.text = @"€";
        self.lbPrice.text = productVariant.price.stringValue;
        if (productVariant.compareAtPrice) {
            NSString *priceString = [NSString stringWithFormat:@"€%@",productVariant.compareAtPrice.stringValue];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:priceString attributes:attribtDic];
            self.lbOldPrice.attributedText = attribtStr;
        }
    }
}


-(void)onClickSizeBtn:(id)sender{
houweibin committed
270
    if(self.delegate){
271 272 273 274
        [self.delegate kwm_onClickSizeBtn];
    }
}

lee committed
275 276 277 278 279
- (IBAction)sizeGuideAction:(id)sender {
    [self.delegate kwm_sizeGuide];
}


280
-(void)onClickBlandBtn:(id)sender{
houweibin committed
281
    if(self.delegate){
282 283 284 285
        [self.delegate kwm_goToBrandVC];
    }
}

houweibin committed
286 287

-(void)setSelect:(BUYProductVariant *)variant color:(KWMColor *)color count:(NSInteger)count{
288
    _variant = variant;
houweibin committed
289 290 291 292 293 294 295
    _color = color;
    _count = count;
    if(!variant){
        self.lbVariant.text = @"选择尺码/颜色";
    }else{
        NSString *colorString = color?[NSString stringWithFormat:@"%@ /",color.title]:@"";
        self.lbVariant.text =  [NSString stringWithFormat:@"已选择 %@ %@ / %ld",colorString,variant.title,count];
296 297
    }
}
298

lee committed
299 300 301 302
- (void)setSizeGuideHidden:(BOOL)sizeGuideHidden {
    self.sizeGuideView.hidden = sizeGuideHidden;
    self.sizeGuideViewHeightConstraint.constant = sizeGuideHidden ? 0 : 50;
}
houweibin committed
303

304
@end