iOS

iOS 自定义弹出View实现逻辑

2015-12-15  本文已影响1429人  flyhao

之前做的几个项目,都用到了弹出View的功能,看截图


IMG_3053.PNG IMG_3054.PNG

几乎每个app都会或多或少用到,这里写个简单思路

订单支付界面

//  PayForView.h
#import <UIKit/UIKit.h>

@protocol PayForViewDelegate <NSObject>

- (void)payforWithPassWd:(NSString *)passWd;

@end

@interface PayForView : UIView

@property (nonatomic, assign) id<PayForViewDelegate> delegate;

- (void)show;

@end
//  PayForView.m
#import "PayForView.h"

@interface PayForView ()
@property (nonatomic, strong)UIView *viewBg;
@property (nonatomic, strong)UITextField *passWdTF;
@end

@implementation PayForView

- (void)show
{
    self.viewBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    self.viewBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    UITapGestureRecognizer *reg = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissMissPresentVC)];
    reg.numberOfTapsRequired = 1;
    UITapGestureRecognizer *reg2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissMissPresentVC)];
    reg.numberOfTapsRequired = 1;
    
    UIView *upView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT - 150)/2)];
    upView.backgroundColor = [UIColor clearColor];
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - (SCREEN_HEIGHT - 150)/2, SCREEN_WIDTH, (SCREEN_HEIGHT - 150)/2)];
    downView.backgroundColor = [UIColor clearColor];
    [upView addGestureRecognizer:reg];
    [downView addGestureRecognizer:reg2];
    [self.viewBg addSubview:upView];
    [self.viewBg addSubview:downView];
    
    UIView *shareView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 40, 150)];
    shareView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT/2);
    shareView.backgroundColor = UIColorWithRGB(26, 26, 26);
    shareView.layer.masksToBounds = YES;
    shareView.layer.cornerRadius = 3;
    shareView.userInteractionEnabled = YES;
    [self.viewBg addSubview:shareView];
    
    // 订单支付
    UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, 40)];
    titleLb.center = CGPointMake((SCREEN_WIDTH-20)/2, 20);
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.text = @"订单支付";
    titleLb.textColor = [UIColor whiteColor];

    // 支付密码
    UIView *payView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH-40, 40)];
    payView.backgroundColor = UIColorWithRGB(51, 51, 51);
    
    [shareView addSubview:titleLb];
    [shareView addSubview:payView];
    
    UILabel *passWdLb = [[UILabel alloc] initWithFrame:CGRectMake(4, 0, 100, 40)];
    passWdLb.textAlignment = NSTextAlignmentCenter;
    passWdLb.font = [UIFont systemFontOfSize:13];
    passWdLb.text = @"支付密码:";
    passWdLb.textColor = [UIColor whiteColor];
    [payView addSubview:passWdLb];
    
    UITextField *passWdTF          = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, SCREEN_WIDTH-40-100, 40)];
    [passWdTF setBorderStyle:UITextBorderStyleNone];
    passWdTF.textColor = [UIColor whiteColor];
    passWdTF.secureTextEntry    = YES;
    passWdTF.autocorrectionType = UITextAutocorrectionTypeNo;
    passWdTF.backgroundColor = [UIColor clearColor];
    [payView addSubview:passWdTF];
    _passWdTF = passWdTF;
    
    // 取消
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-40)/2, 100, (SCREEN_WIDTH-40)/2, 45)];
    cancelBtn.backgroundColor = UIColorWithRGB(50, 50, 50);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [cancelBtn addTarget:self action:@selector(animateOut) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareView addSubview:cancelBtn];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, (SCREEN_WIDTH-40)/2, 45)];
    sureBtn.backgroundColor = UIColorWithRGB(225, 24, 38);
    [sureBtn setTitle:@"支付" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [sureBtn addTarget:self action:@selector(sureIndent) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareView addSubview:sureBtn];
    
    //背景
    [self creatBackgroundView];
    
    [self addSubview:self.viewBg];
    
    self.userInteractionEnabled = YES;
    self.viewBg.userInteractionEnabled = YES;
}

#pragma mark - Action
-(void)sureIndent
{
    if ([self.delegate respondsToSelector:@selector(payforWithPassWd:)]) {
        [self.delegate payforWithPassWd:self.passWdTF.text];
        [self animateOut];
    }
}

-(void)animateIn
{
    self.viewBg.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.3 animations:^{
        self.viewBg.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}

-(void)animateOut
{
    [UIView animateWithDuration:0.3 animations:^{
        self.viewBg.transform = CGAffineTransformMakeScale(0.01, 0.01);
        self.viewBg.alpha = 0.2;
        self.alpha = 0.2;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

-(void)creatBackgroundView
{
    self.frame = [[UIScreen mainScreen] bounds];
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    self.opaque = NO;
    
//这里自定义的View 达到的效果和UIAlterView一样是在Window上添加,UIWindow的优先级最高,Window包含了所有视图,在这之上添加视图,可以保证添加在最上面
    UIWindow *appWindow = [[UIApplication sharedApplication] keyWindow];
    [appWindow addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }];
    
}

-(void)dissMissPresentVC
{
    [self animateOut];
}

上一篇 下一篇

猜你喜欢

热点阅读