// // FKCouponSelectView.m // FirstLink // // Created by jack on 16/8/21. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKCouponSelectView.h" #import "FKSelectCouponCell.h" #import "CashCouponItem.h" @interface FKCouponSelectView () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIView *topLine; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UIButton *cancelBtn; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSArray *couponArray; @end @implementation FKCouponSelectView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self addAllSubviews]; [self addTapGesture]; } return self; } - (void)addTapGesture{ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)]; tap.delegate = self; [self addGestureRecognizer:tap]; } - (void)addAllSubviews{ [self addSubview:self.contentView]; [self.contentView addSubview:self.topLine]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.cancelBtn]; [self.contentView addSubview:self.tableView]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self); make.height.mas_equalTo(285); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(15); make.centerY.equalTo(self.contentView.mas_top).offset(22.5); }]; [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.top.equalTo(self.contentView); make.size.mas_equalTo(CGSizeMake(45, 45)); }]; [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(15); make.top.equalTo(self.contentView).offset(45); make.right.equalTo(self.contentView); make.height.mas_equalTo(0.5); }]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.topLine.mas_bottom); make.left.right.bottom.equalTo(self.contentView); }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.couponArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ FKSelectCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKSelectCouponCell class])]; CashCouponItem *item = [self couponItemAtIndex:indexPath.row]; NSArray *selectIndexs = [tableView indexPathsForSelectedRows]; BOOL selected = [selectIndexs containsObject:indexPath]; if (cell && item) { cell.priceLabel.text = [FLStringHelper convertFenToRMBmoneyString:item.amount]; cell.typeLabel.text = item.type; cell.ruleLabel.text = item.desc; cell.timeLabel.text = item.timeDesc; cell.selectBtn.selected = selected ? YES : NO; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80.0f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSArray *selectArray = [tableView indexPathsForSelectedRows]; for (NSIndexPath *selectIndex in selectArray) { if (selectIndex != indexPath) { [tableView deselectRowAtIndexPath:selectIndex animated:NO]; FKSelectCouponCell *cell = [tableView cellForRowAtIndexPath:selectIndex]; cell.selectBtn.selected = NO; } } FKSelectCouponCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.selectBtn.selected = YES; [self selectFinish]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ FKSelectCouponCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.selectBtn.selected = NO; [self selectFinish]; } - (void)refreshCouponArray:(NSArray *)couponArray selectFirst:(BOOL)selectFirst{ self.couponArray = couponArray; [self.tableView reloadData]; if (selectFirst) { NSIndexPath *firstIndex = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView selectRowAtIndexPath:firstIndex animated:NO scrollPosition:UITableViewScrollPositionNone]; FKSelectCouponCell *cell = [self.tableView cellForRowAtIndexPath:firstIndex]; if (cell) cell.selectBtn.selected = YES; } [self.tableView setContentOffset:CGPointZero]; } - (void)showInView:(UIView *)view animated:(BOOL)animated{ if (!view) { return; } CGFloat height = view.frame.size.height; CGFloat width = view.frame.size.width; CGFloat contentH = 285; // 内容高度 [view addSubview:self]; [self setFrame:view.frame]; [self.contentView setFrame:CGRectMake(0, height, width, contentH)]; self.backgroundColor = [UIColor clearColor]; CGFloat duration = animated ? 0.3f : 0; WeakSelf(weakSelf); [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ weakSelf.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; [weakSelf.contentView setFrame:CGRectMake(0, height - contentH, width, contentH)]; } completion:nil]; } - (void)tapGestureClick:(UIGestureRecognizer *)tapGesture{ CGPoint location = [tapGesture locationInView:self]; BOOL isOnContent = CGRectContainsPoint(self.contentView.frame, location); if (isOnContent) { return; }else{ [self clickCancelBtn]; } } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ CGPoint location = [touch locationInView:self]; if (CGRectContainsPoint(self.contentView.frame, location)) return NO; return YES; } - (void)dismissAnimated{ if (self.superview){ [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.backgroundColor = [UIColor clearColor]; [self.contentView setFrame:CGRectOffset(self.contentView.frame, 0, CGRectGetHeight(self.contentView.frame))]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } } - (void)clickCancelBtn{ [self selectFinish]; } - (void)selectFinish{ [self dismissAnimated]; if (self.finish) { NSIndexPath *index = [self selectedIndexPath]; CashCouponItem *couponItem = nil; if (index) { couponItem = [self couponItemAtIndex:index.row]; } self.finish(couponItem.itemID); } } - (CashCouponItem *)couponItemAtIndex:(NSInteger)index{ if (index >= 0 && index < self.couponArray.count) { return self.couponArray[index]; } return nil; } - (NSIndexPath *)selectedIndexPath{ NSArray *indexs = [self.tableView indexPathsForSelectedRows]; if (indexs.count) { return indexs.firstObject; } return nil; } - (BOOL)isShowing{ if (self.superview) return YES; return NO; } #pragma mark - property - (UILabel *)titleLabel{ if (_titleLabel == nil) { _titleLabel = [[UILabel alloc]init]; _titleLabel.textColor = UIColorFromRGB(0x999999); _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.text = @"选择优惠券"; } return _titleLabel; } - (UIView *)topLine{ if (_topLine == nil) { _topLine = [[UIView alloc]init]; _topLine.backgroundColor = UIColorFromRGB(0xe5e5e5); } return _topLine; } - (UIView *)contentView{ if (_contentView == nil) { _contentView = [[UIView alloc]init]; _contentView.backgroundColor = [UIColor whiteColor]; } return _contentView; } - (UIButton *)cancelBtn{ if (_cancelBtn == nil) { _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_cancelBtn setImage:[UIImage imageNamed:@"selectCoupon_cancel"] forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(clickCancelBtn) forControlEvents:UIControlEventTouchUpInside]; } return _cancelBtn; } - (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.dataSource = self; _tableView.delegate = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.allowsMultipleSelection = YES; [_tableView registerClass:[FKSelectCouponCell class] forCellReuseIdentifier:NSStringFromClass([FKSelectCouponCell class])]; } return _tableView; } @end