蒙板封裝:結合了自定義按鈕button(帶有圖片和文字)spring
使用了pop動畫,具備彈簧效果彈出windows
#import "BaseView.h" @interface MeSelectGenderView : BaseView @property (nonatomic, strong) void(^myBlock)(NSInteger index); + (instancetype)selectGender; - (void)show; - (void)dismiss; @end
// // MeSelectSenderView.m // YangLand // // Created by 李勝兵 on 16/6/17. // Copyright © 2016年 tshiny. All rights reserved. // #import "MeSelectGenderView.h" #import "YLButton.h" #import <POPSpringAnimation.h> @interface MeSelectGenderView () @property (nonatomic, strong)YLButton *button; @property (nonatomic, strong)UIView *whiteView; @property (nonatomic, strong)UIButton *cancelButton; @property (nonatomic, strong)UIView *lineView; @property (nonatomic, strong)UILabel *label; @property (nonatomic, strong)NSArray *arr; @end @implementation MeSelectGenderView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor =[UIColor colorWithWhite:.2 alpha:.6]; [self setupUI]; } return self; } - (void)setupUI { [self addSubview:self.whiteView]; [self.whiteView autoPinEdgeToSuperviewEdge:ALEdgeLeft]; [self.whiteView autoPinEdgeToSuperviewEdge:ALEdgeRight]; [self.whiteView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:YLScreenH]; [self.whiteView autoSetDimension:ALDimensionHeight toSize:YLScreenH *0.4]; [self.whiteView addSubview:self.cancelButton]; [self.cancelButton autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop]; [self.cancelButton autoSetDimension:ALDimensionHeight toSize:50]; [self.whiteView addSubview:self.lineView]; [self.lineView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.cancelButton]; [self.lineView autoSetDimensionsToSize:CGSizeMake(YLScreenW, 0.5)]; [self.whiteView addSubview:self.label]; [self.label autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom]; [self.label autoSetDimension:ALDimensionHeight toSize:50]; CGFloat btnX = 40; CGFloat btnY = 70; CGFloat btnW = 80; CGFloat btnH = 100; CGFloat middlePadding = (YLScreenW-btnX*2-btnW*self.arr.count)/(self.arr.count-1); for (int i=0; i<self.arr.count; i++) { YLButton *button = [[YLButton alloc]init]; [button setTitle:self.arr[i] forState:UIControlStateNormal]; button.x = btnX + (btnW+middlePadding) * i; button.y = btnY; button.width = btnW; button.height = btnH; button.tag = 10000+i; self.button = button; [self.whiteView addSubview:button]; [button addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside]; } } #pragma mark - 選擇性別事件 - (void)selectClick:(UIButton *)sender { [self dismiss]; NSInteger index = 2; if ([sender.currentTitle isEqualToString:self.arr[0]]) { index = 0; }else if ([sender.currentTitle isEqualToString:self.arr[1]]) { index = 3; }else if ([sender.currentTitle isEqualToString:self.arr[2]]) { index = 1; } if (self.myBlock) { self.myBlock(index); } } #pragma mark - 快速建立蒙板對象 + (instancetype)selectGender { return [[self alloc]init]; } #pragma mark - 顯示蒙板 - (void)show { // 獲取最上面的窗口 UIWindow *window = [[UIApplication sharedApplication].windows lastObject]; // 將本身添加到最上面的窗口上 [window addSubview:self]; // 設置本身的尺寸 self.frame = window.bounds; // pop動畫 POPSpringAnimation *changeFrame = [POPSpringAnimation animation]; changeFrame.property = [POPAnimatableProperty propertyWithName: kPOPViewFrame]; changeFrame.fromValue = [NSValue valueWithCGRect:CGRectMake(0, YLScreenH,YLScreenW,YLScreenH *0.4)]; changeFrame.toValue = [NSValue valueWithCGRect:CGRectMake(0, YLScreenH-(YLScreenH *0.4),YLScreenW,YLScreenH *0.4)]; changeFrame.springBounciness = 10; [self.whiteView pop_addAnimation:changeFrame forKey:@"changeFrame"]; } #pragma mark - 銷燬蒙板 - (void)dismiss { [self removeFromSuperview]; } #pragma mark - 取消事件 - (void)cancelClick:(UIButton *)sender { [self removeFromSuperview]; } - (UIView *)whiteView { if (!_whiteView) { _whiteView = [UIView newAutoLayoutView]; _whiteView.backgroundColor = [UIColor YLColorGrayBg]; } return _whiteView; } - (UIButton *)cancelButton { if (!_cancelButton) { _cancelButton = [UIButton newAutoLayoutView]; [_cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [_cancelButton setTitleColor:[UIColor YLColorGrayPrimary] forState:UIControlStateNormal]; [_cancelButton setBackgroundColor:[UIColor whiteColor]]; _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15]; [_cancelButton addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } - (UIView *)lineView { if (!_lineView) { _lineView = [UIView newAutoLayoutView]; _lineView.backgroundColor = [UIColor YLColorGrayLine]; } return _lineView; } - (UILabel *)label { if (!_label) { _label = [UILabel newAutoLayoutView]; _label.text = @"選擇性別"; _label.textColor = [UIColor YLColorGrayPrimary]; _label.font = [UIFont systemFontOfSize:15]; _label.textAlignment = NSTextAlignmentCenter; } return _label; } - (NSArray *)arr { if (!_arr) { _arr = [NSArray arrayWithObjects:@"女神",@"保密",@"型男",nil]; } return _arr; } @end