// // SubmitChangeTransportView.m // FirstLink // // Created by jack on 15/6/20. // Copyright (c) 2015年 FirstLink. All rights reserved. // 选择运输方式 #import "SubmitSelectTransportView.h" typedef enum { kTransportBtnTagCarriageIn = 0, kTransportBtnTagTakeSelf, kTransportBtnTagCancel, kTransportBtnTagConfirm } kTransportBtnTag; #define kTransportContentHeight 190 @interface SubmitSelectTransportView () @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UIButton *countryInBtn; @property (nonatomic, strong) UIButton *takeSelfBtn; @end @implementation SubmitSelectTransportView @synthesize carriageInLabel = _carriageInLabel; @synthesize takeSelfLabel = _takeSelfLabel; - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initializeLayout]; [self addTapGesture]; self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4]; } return self; } - (void)addTapGesture { UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureEvent:)]; [self addGestureRecognizer:singleTap]; } - (void)initializeLayout { UIView *bannerView = [[UIView alloc]init]; UIView *countryInView = [[UIView alloc]init]; UIView *takeSelfView = [[UIView alloc]init]; UIView *bottomView = [[UIView alloc]init]; bannerView.backgroundColor = [UIColor colorWithRed:234.0 / 255 green:234.0 / 255 blue:234.0 / 255 alpha:1.0f]; UILabel *titleLabel = ({ UILabel *label = [[UILabel alloc]init]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont systemFontOfSize:15]; label.textColor = UIColorFromRGB(0x999999); label.text = @"运送方式"; label; }); [self addSubview:self.contentView]; [self.contentView addSubview:bannerView]; [self.contentView addSubview:countryInView]; [self.contentView addSubview:takeSelfView]; [self.contentView addSubview:bottomView]; [bannerView addSubview:titleLabel]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@kTransportContentHeight); make.left.right.bottom.equalTo(self); }]; [bannerView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.equalTo(self.contentView); make.height.equalTo(@40); }]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(bannerView); }]; [countryInView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bannerView.mas_bottom); make.left.right.equalTo(self.contentView); make.height.equalTo(@50); }]; [takeSelfView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(countryInView.mas_bottom); make.left.right.equalTo(self.contentView); make.height.equalTo(@50); }]; [bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(takeSelfView.mas_bottom); make.left.right.equalTo(self.contentView); make.bottom.equalTo(self.contentView); }]; [self layoutContentView:countryInView withLabel:self.carriageInLabel actionBtn:self.countryInBtn]; [self layoutContentView:takeSelfView withLabel:self.takeSelfLabel actionBtn:self.takeSelfBtn]; [self layoutBottomView:bottomView]; } - (void)layoutBottomView:(UIView *)bottomView { UIView *middleLine = [[UIView alloc]init]; middleLine.backgroundColor = UIColorFromRGB(0xe5e5e5); UIButton *cancelBtn = ({ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"取消" forState:UIControlStateNormal]; [button setTitleColor:UIColorFromRGB(0x2b6ef7) forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:18]; [button addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside]; button.tag = kTransportBtnTagCancel; button; }); UIButton *confirmBtn = ({ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"确认" forState:UIControlStateNormal]; [button setTitleColor:UIColorFromRGB(0x2b6ef7) forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:18]; [button addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside]; button.tag = kTransportBtnTagConfirm; button; }); [bottomView addSubview:middleLine]; [bottomView addSubview:cancelBtn]; [bottomView addSubview:confirmBtn]; [middleLine mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.centerX.equalTo(bottomView); make.width.equalTo(@0.5); }]; [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(middleLine.mas_left); make.top.left.bottom.equalTo(bottomView); }]; [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(middleLine.mas_right); make.top.right.bottom.equalTo(bottomView); }]; } - (void)layoutContentView:(UIView *)contentView withLabel:(UILabel *)titleLabel actionBtn:(UIButton *)actionBtn { UIView *bottomLine = [[UIView alloc]init]; bottomLine.backgroundColor = UIColorFromRGB(0xe5e5e5); [contentView addSubview:titleLabel]; [contentView addSubview:bottomLine]; [contentView addSubview:actionBtn]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(contentView).offset(30); make.centerY.equalTo(contentView); }]; [actionBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(contentView).offset(- 30); make.centerY.equalTo(contentView); make.width.height.equalTo(contentView.mas_height); }]; [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(contentView); make.height.equalTo(@0.5); }]; } - (void)showInView:(UIView *)view animated:(BOOL)animated { if (!view) return; [view addSubview:self]; self.frame = view.bounds; if (animated) { self.backgroundColor = [UIColor clearColor]; CGAffineTransform transForm = CGAffineTransformMakeTranslation(0, kTransportContentHeight); self.contentView.transform = transForm; [UIView animateWithDuration:0.3f animations:^{ self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4]; self.contentView.transform = CGAffineTransformIdentity; }]; } } - (void)dismissAnimation { CGRect frame = self.contentView.frame; CGRect downFrame = CGRectOffset(frame, 0, CGRectGetHeight(frame)); self.contentView.transform = CGAffineTransformIdentity; [UIView animateWithDuration:0.3f animations:^{ self.backgroundColor = [UIColor clearColor]; self.contentView.frame = downFrame; } completion:^(BOOL finished) { [self removeFromSuperview]; self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4]; self.contentView.frame = frame; }]; } #pragma mark - event response - (void)singleBtnClick:(UIButton *)sender { switch (sender.tag) { case kTransportBtnTagCarriageIn: case kTransportBtnTagTakeSelf:{ self.countryInBtn.selected = NO; self.takeSelfBtn.selected = NO; sender.selected = YES; } break; case kTransportBtnTagCancel:{ [self dismissAnimation]; } break; case kTransportBtnTagConfirm:{ if (_confirmAction) _confirmAction(self); [self dismissAnimation]; } break; default: break; } } - (void)singleTapGestureEvent:(UIGestureRecognizer *)gesture { CGPoint location = [gesture locationInView:self]; if (!CGRectContainsPoint(self.contentView.frame, location)) { [self dismissAnimation]; } } #pragma mark - getter && setter - (UIView *)contentView { if (_contentView == nil) { _contentView = [[UIView alloc]init]; _contentView.backgroundColor = [UIColor whiteColor]; } return _contentView; } - (UILabel *)carriageInLabel { if (_carriageInLabel == nil) { _carriageInLabel = [[UILabel alloc]init]; _carriageInLabel.textColor = UIColorFromRGB(0x333333); _carriageInLabel.font = [UIFont systemFontOfSize:15]; _carriageInLabel.text = @"国内运费¥10.00"; } return _carriageInLabel; } - (UILabel *)takeSelfLabel { if (_takeSelfLabel == nil) { _takeSelfLabel = [[UILabel alloc]init]; _takeSelfLabel.textColor = UIColorFromRGB(0x333333); _takeSelfLabel.font = [UIFont systemFontOfSize:15]; _takeSelfLabel.text = @"自取运费¥0.00"; } return _takeSelfLabel; } - (UIButton *)countryInBtn { if (_countryInBtn == nil) { _countryInBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_countryInBtn setImage:[UIImage imageNamed:@"common_body_unselected_n"] forState:UIControlStateNormal]; [_countryInBtn setImage:[UIImage imageNamed:@"common_body_choose_h"] forState:UIControlStateSelected]; [_countryInBtn addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _countryInBtn.tag = kTransportBtnTagCarriageIn; _countryInBtn.selected = YES; // 默认选中 } return _countryInBtn; } - (UIButton *)takeSelfBtn { if (_takeSelfBtn == nil) { _takeSelfBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_takeSelfBtn setImage:[UIImage imageNamed:@"common_body_unselected_n"] forState:UIControlStateNormal]; [_takeSelfBtn setImage:[UIImage imageNamed:@"common_body_choose_h"] forState:UIControlStateSelected]; [_takeSelfBtn addTarget:self action:@selector(singleBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _takeSelfBtn.tag = kTransportBtnTagTakeSelf; } return _takeSelfBtn; } - (void)setSelectedIndex:(NSInteger)selectedIndex { self.takeSelfBtn.selected = self.countryInBtn.selected = NO; if (selectedIndex == 1) { self.takeSelfBtn.selected = YES; }else if (selectedIndex == 0){ self.countryInBtn.selected = YES; } } - (NSInteger)selectedIndex { if (self.takeSelfBtn.selected) return 1; return 0; } - (NSInteger)defaultIndex { NSInteger index = 0; if (self.takeSelfBtn.selected) index = 1; return index; } @end