[一句秒懂]带你怎样封装一个蒙板

本文介绍了一个iOS应用中用于选择性别的自定义视图组件的实现细节,包括按钮、动画效果及事件处理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蒙板封装:结合了自定义按钮button(带有图片和文字)

使用了pop动画,具有弹簧效果弹出

 

#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

 

转载于:https://my.oschina.net/shengbingli/blog/693463

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值