iOS 自定義彈出View實現(xiàn)邏輯

之前做的幾個項目,都用到了彈出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的優(yōu)先級最高,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];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,155評論 25 708
  • 因為腌制的黃瓜,鬼子姜,白蘿卜咸菜還未到出缸可吃時間,沒有了下飯的小菜,加之技癢難耐,一不做二不休,干煸鲅魚魔術(shù)般...
    天馬行空我也閱讀 686評論 0 0
  • 老媽怕風怕冷。一到天氣涼又有風,老媽呼吸時覺得喉嚨如有針刺感。沒有遇風,天氣暖和時又能說說笑笑,沒有不舒服的感覺。...
    柚稚媽媽閱讀 2,093評論 46 109
  • 《只要菜不要菜園子》 作者:陳序 昨天是小年,到了年根底下,各家各戶也都準備開始了對于新年的準備。 中...
    陳序原創(chuàng)閱讀 534評論 0 0
  • 人生沒有后悔藥 ,即使重新來過,會有怎樣的不同? 第一次世界大戰(zhàn),英國戰(zhàn)斗英雄亨利·坦迪經(jīng)歷了血雨腥風的戰(zhàn)場,永生...
    icesealily閱讀 339評論 0 1