KWMProductDetailView.m 9.84 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
//
//  KWMProductDetailView.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/1/11.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMProductDetailView.h"
#import "KWMImageUtil.h"
#import "KWMStringUtil.h"
#import "KWMPageControl.h"
#import "YYWebImage/YYWebImage.h"

@interface KWMProductDetailView ()<UIScrollViewDelegate>

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

@property(nonatomic,weak) IBOutlet UIScrollView *imageScrollView;

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

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

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

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

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

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

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

@property (nonatomic) BUYProduct *product;

@property (nonatomic) NSMutableArray *imageArr;

@property (nonatomic) NSTimer *timer;

@property (nonatomic) BOOL isFirstScroll;

@property (nonatomic) BOOL isShowCNY;

- (IBAction)onClickSizeBtn:(id)sender;

- (IBAction)onClickInformationBtn:(id)sender;

- (IBAction)onClickBlandBtn:(id)sender;

@end

@implementation KWMProductDetailView

- (id)init{
    if (self=[super init]){
        [self addView];
    }
    return self;
}

- (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];
}

-(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;
}

-(void)setData:(BUYProduct *)product{
    if(product == nil){
        return;
    }
    self.isShowCNY = YES;
    self.isFirstScroll = YES;
    [self removeTimer];
    self.product = product;
    self.imageScrollView.delegate = self;
    [self.imageScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    for (UIView *subview in self.imageScrollView.subviews) {
        [subview removeFromSuperview];
    }
    [self initImage:product];
    [self initView:product];
    //[self addTimer];
}

-(void)initImage:(BUYProduct *)product{
    if(product == nil){
        return;
    }
    NSInteger imageCount = product.imagesArray == nil? 0:product.imagesArray.count;
    self.imageArr = [NSMutableArray array];
    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;
houweibin committed
151
        NSString *imageUrl = [KWMImageUtil getProductImageUrlByPosition:self.product ImageSize:BigImage Position:i];
houweibin committed
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
        [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 setValue:[UIImage imageNamed:@"ic_page_2"] forKey:@"_currentPageImage"];
//    [self.pageControl setValue:[UIImage imageNamed:@"ic_page_1"] forKey:@"_pageImage"];
    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;
}


-(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;
    NSAttributedString *detailAttr = [[NSAttributedString alloc] initWithData:[self.product.htmlDescription dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    self.lbDetail.attributedText = detailAttr;
lee committed
189 190
//    self.lbPrice.text = [KWMStringUtil getEUR2CNYstring:productVariant.price];
    self.lbPrice.text = productVariant.price.priceValueFormatted;
houweibin committed
191
    if (productVariant.compareAtPrice) {
lee committed
192 193
//        NSString *priceString = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:productVariant.compareAtPrice]];
        NSString *priceString = productVariant.compareAtPrice.priceValueFormatted;
houweibin committed
194 195 196 197 198 199 200
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:priceString attributes:attribtDic];
        self.lbOldPrice.attributedText = attribtStr;
    }else{
        self.lbOldPrice.hidden = YES;
    }
    
lee committed
201 202 203 204 205 206
//    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];
houweibin committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    self.lbUnit.userInteractionEnabled = YES;
    self.lbPrice.userInteractionEnabled = YES;
    self.lbOldPrice.userInteractionEnabled = YES;
}

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

-(void)onClickImage:(UITapGestureRecognizer *)tapGesture{
    if(self.delegate!=nil){
        UIView *view = [tapGesture view];
        [self.delegate kwm_onClickImage:self.imageArr currentIndex:view.tag];
    }
}

-(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 = @"¥";
lee committed
232 233
//        self.lbPrice.text = [KWMStringUtil getEUR2CNYstring:productVariant.price];
        self.lbPrice.text = productVariant.price.priceValueFormatted;
houweibin committed
234
        if (productVariant.compareAtPrice) {
lee committed
235 236
//            NSString *priceString = [NSString stringWithFormat:@"¥%@",[KWMStringUtil getEUR2CNYstring:productVariant.compareAtPrice]];
            NSString *priceString = productVariant.compareAtPrice.priceValueFormatted;
houweibin committed
237 238 239 240 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 270 271 272
            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{
    if(self.delegate!=nil){
        [self.delegate kwm_onClickSizeBtn];
    }
}

-(void)onClickBlandBtn:(id)sender{
    if(self.delegate!=nil){
        [self.delegate kwm_goToBrandVC];
    }
}

-(void)onClickInformationBtn:(id)sender{
    if(self.delegate!=nil){
        [self.delegate kwm_onClickInformation];
    }
}

lee committed
273 274 275 276
- (IBAction)openSizeGuide:(id)sender {
    [self.delegate kwm_onClickSizeGuide];
}

houweibin committed
277 278 279 280



@end