// // SubmitDiscountPickView.m // FirstLink // // Created by jack on 15/6/20. // Copyright (c) 2015年 FirstLink. All rights reserved. // 选择代金券 #import "SubmitDiscountPickView.h" #define kContentHeight 194 @interface SubmitDiscountPickView () @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UIView *bannerView; @property (nonatomic, strong) NSArray *titleArray; @end @implementation SubmitDiscountPickView @synthesize pickerView = _pickerView; - (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 *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap:)]; [self addGestureRecognizer:tap]; } - (void)initializeLayout { UIButton *cancelBtn = ({ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"取消" forState:UIControlStateNormal]; [button setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal]; [button addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside]; button.titleLabel.font = [UIFont systemFontOfSize:14]; button; }); UIButton *confirmBtn = ({ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"完成" forState:UIControlStateNormal]; [button setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal]; [button addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside]; button.titleLabel.font = [UIFont systemFontOfSize:14]; button; }); [self addSubview:self.contentView]; [self.contentView addSubview:self.bannerView]; [self.contentView addSubview:self.pickerView]; [self.bannerView addSubview:cancelBtn]; [self.bannerView addSubview:confirmBtn]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self); make.height.equalTo(@kContentHeight); }]; [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.equalTo(self.contentView); make.height.equalTo(@44); }]; [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bannerView.mas_bottom); make.left.right.bottom.equalTo(self.contentView); }]; [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.bannerView); make.height.equalTo(self.bannerView); make.centerY.equalTo(self.bannerView); make.width.equalTo(@80); }]; [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.bannerView); make.height.equalTo(self.bannerView); make.centerY.equalTo(self.bannerView); make.width.equalTo(cancelBtn); }]; } - (void)showInView:(UIView *)view animated:(BOOL)animated { if (!view) return; [view addSubview:self]; self.frame = view.bounds; WeakSelf(weakSelf); if (animated) { self.backgroundColor = [UIColor clearColor]; CGAffineTransform transForm = CGAffineTransformMakeTranslation(0, kContentHeight); self.contentView.transform = transForm; [UIView animateWithDuration:0.3f animations:^{ weakSelf.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4]; weakSelf.contentView.transform = CGAffineTransformIdentity; }]; } } - (void)dismissAnimated { CGRect frame = self.contentView.frame; CGRect downFrame = CGRectOffset(frame, 0, CGRectGetHeight(frame)); self.contentView.transform = CGAffineTransformIdentity; WeakSelf(weakSelf); [UIView animateWithDuration:0.3f animations:^{ weakSelf.backgroundColor = [UIColor clearColor]; weakSelf.contentView.frame = downFrame; } completion:^(BOOL finished) { [weakSelf removeFromSuperview]; weakSelf.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4]; weakSelf.contentView.frame = frame; }]; } - (BOOL)isShowing{ if (self.superview) return YES; return NO; } - (void)refreshTitleArray:(NSArray *)titleArray withSelectedIndex:(NSInteger)index { _titleArray = titleArray; [self.pickerView reloadAllComponents]; [self.pickerView selectRow:index inComponent:0 animated:NO]; } #pragma mark - event response - (void)clickTap:(UITapGestureRecognizer *)gesture{ [self dismissAnimated]; } - (void)cancelBtnClick { [self dismissAnimated]; } - (void)confirmBtnClick { NSInteger selectedRow = [self.pickerView selectedRowInComponent:0]; if (_selectDone) { _selectDone(selectedRow); } [self dismissAnimated]; } #pragma mark - pickerView dataSource & delegate - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return self.titleArray.count; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { return UISCREENWIDTH; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 50; } - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{ return self.titleArray[row]; } #pragma mark - getter && setter - (UIPickerView *)pickerView { if (_pickerView == nil) { _pickerView = [[UIPickerView alloc]init]; _pickerView.dataSource = self; _pickerView.delegate = self; _pickerView.backgroundColor = [UIColor whiteColor]; } return _pickerView; } - (UIView *)contentView { if (_contentView == nil) { _contentView = [[UIView alloc]init]; _contentView.backgroundColor = [UIColor clearColor]; } return _contentView; } - (UIView *)bannerView { if (_bannerView == nil) { _bannerView = [[UIView alloc]init]; _bannerView.backgroundColor = [UIColor colorWithRed:235.0/255.0 green:236.0/255.0 blue:239.0/255.0 alpha:1.0]; } return _bannerView; } - (void)setTitleArray:(NSArray *)titleArray { _titleArray = titleArray; [self.pickerView reloadAllComponents]; } @end