一般的弹出框或一些嵌入控件都可以单独写成一个类,然后用xib来制作,而封装成一个类控件,我这里就哟个简单的购物弹出框。
制作如下:首先建一个类,继承于UIView,如图:
然后在建一个xib,如下图:
xib上的名字一定要和类的名字一样,这样就会自动关联在一起。
接着修改xib的大小:
一定要选中整个View,然后才能设置frame:
然后在其View上面拖控件,设置约束,就不累赘了,约束一定要是设置成全适配的,不然不同型号就会出问题的。
然后,贴上代码:.h
#import <UIKit/UIKit.h>
@protocol PopCatViewDelegate <NSObject>
-(void)btnClick:(UIView *)popView andTag:(NSInteger)tag;
@end
@interface PopCatView : UIView
{
}
/** 显示 */
- (void)show;
/** 隐藏 */
- (void)hide;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *subButton;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIButton *comfinButton;
@property (weak, nonatomic) IBOutlet UIButton *detailButton;
@property(assign,nonatomic)id<PopCatViewDelegate>delegate;
@end
.m
#import "PopCatView.h"
#define mScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define mScreenHeight ([UIScreen mainScreen].bounds.size.height)
@interface PopCatView ()
@property (nonatomic, strong) UIView *bgView;
/** 标题背景View */
@end
@implementation PopCatView
#pragma mark 配置视图
- (instancetype)init {
self = [super init];
if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:@"PopCatView" owner:self options:nil] lastObject];
// 初始化设置
UIWindow *window = [UIApplication sharedApplication].keyWindow;
self.frame = CGRectMake(20 + mScreenWidth, (mScreenHeight-160)/2, mScreenWidth - 80, 160);
[window addSubview:self.bgView];
[window addSubview:self];
// // 监听键盘改变的通知
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
return self;
}
- (void)awakeFromNib {
// 一次性的设置
self.layer.cornerRadius = 8;
self.layer.borderWidth = 0.5;
self.layer.borderColor = [UIColor clearColor].CGColor;
self.clipsToBounds = YES;
[self resetTextFiled];
self.bgView.alpha = 0.5;
self.bgView.backgroundColor=[UIColor blackColor];
self.subButton.layer.borderWidth = 1;
self.subButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.addButton.layer.borderWidth = 1;
self.addButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
- (UIView *)bgView {
if (_bgView == nil) {
_bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
_bgView.hidden = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[_bgView addGestureRecognizer:tap];
}
return _bgView;
}
#pragma mark 功能方法
/** 显示 */
- (void)show {
// 重置输入框
// 延迟0.2秒,等View显示出来后,再激活键盘
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self.textField becomeFirstResponder];
});
[UIView animateWithDuration:0.35 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.bgView.hidden = NO;
self.bgView.userInteractionEnabled = NO;
CGPoint center = self.center;
center.x = mScreenWidth / 2;
self.center = center;
} completion:^(BOOL finished) {
self.bgView.userInteractionEnabled = YES;
}];
}
/** 隐藏 */
- (void)hide {
[UIView animateWithDuration:0.35 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:0 animations:^{
CGPoint center = self.center;
center.x = mScreenWidth / 2 + mScreenWidth;
self.center = center;
} completion:^(BOOL finished) {
self.bgView.hidden = YES;
}];
[self endEditing:YES];
}
#pragma mark 通知方法
/** 键盘frame即将改变的通知 */
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:duration animations:^{
// 键盘下去了
if (keyboardF.origin.y >= mScreenHeight) {
CGPoint center = self.center;
center.y = mScreenHeight / 2;
self.center = center;
// 键盘上来了
} else {
CGRect frame = self.frame;
frame.origin.y = keyboardF.origin.y - frame.size.height - 2; // 2是输入框和键盘的间隙,自己调..
self.frame = frame;
}
}];
}
- (IBAction)sub:(id)sender {
[_delegate btnClick:self andTag:111];
}
- (IBAction)add:(id)sender {
[_delegate btnClick:self andTag:112];
}
- (IBAction)comfig:(id)sender {
[_delegate btnClick:self andTag:113];
}
- (IBAction)detail:(id)sender {
[_delegate btnClick:self andTag:114];
}
#pragma mark 私有方法
/** 重置textFiled */
- (void)resetTextFiled {
self.textField.text = @"0";
}
@end
#import "PopCatView.h"
@interface ViewController ()<PopCatViewDelegate>
@property(nonatomic,strong)PopCatView *popView;
然后,我的是在点击cell的事件上调用的
//点击弹出购物框
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
GoodsBean *good = [self.dataSource modelsAtIndexPath: indexPath];
self.selectedGood = good;
[self popCostomView];
}
-(void)popCostomView{
if (!_popView) {
self.popView = [[PopCatView alloc] init];
}
self.popView.delegate = self;
NSMutableArray * goods = self.getAppContext.cat.goodsArray ;
BOOL has = false;
GoodsBean* rgood = nil;
for (GoodsBean* g in goods) {
if(g.id == self.selectedGood.id){
g.number = self.selectedGood.number;
has = true;
if (self.selectedGood.number >= 0) {
rgood = g;
}
break;
}
}
if(!has)
{
self.popView.textField.text = @"0";
}else{
self.popView.textField.text = [NSString stringWithFormat:@"%ld",rgood.number];
}
[self.popView.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
self.popView.titleLabel.text = [self.selectedGood.shortName stringByAppendingString:@"购买数量:"];
[self.popView.comfinButton setTitle:@"加入购物车" forState:UIControlStateNormal];
[self.popView show];
}
#pragma popView的代理
-(void)btnClick:(UIView *)popView andTag:(NSInteger)tag
{
switch (tag) {
case 111:[self subBtnClick];
break;
case 112:[self addBtnClick];
break;
case 113:[self confirmBtnData];
break;
case 114:[self gotoGoodsDetail];
break;
default:
break;
}
}
然后就没有然后了。