iOS自定義控件:彈窗控件封裝

鎮(zhèn)樓圖

彈窗控件封裝蚁趁,仿照微信的樣式進(jìn)行基礎(chǔ)開發(fā)瘦真,并進(jìn)行其他樣式擴(kuò)展忠蝗。

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, WDPopAnimationType) {
    WDPopAnimationTypeDefault,
    WDPopAnimationTypeFromLeft,
    WDPopAnimationTypeFromRight,
    WDPopAnimationTypeFromTop,
    WDPopAnimationTypeFromBottom
};

UIKIT_EXTERN const NSTimeInterval WDPopAnmationDurition;

@interface WDPopBaseView : UIControl
+ (instancetype)defaultView;

- (void)showView;
- (void)showViewInSuperView:( UIView * _Nullable)superView;

- (void)showViewWithAnimationType:(WDPopAnimationType)animationType;
- (void)showViewWithAnimationType:(WDPopAnimationType)animationType inSuperView:( UIView * _Nullable)superView;

- (void)hidenView;
@end

NS_ASSUME_NONNULL_END
#import "WDPopBaseView.h"

const NSTimeInterval WDPopAnmationDurition = 0.35;

static inline UIView *wKeyWindow() {
    if (UIApplication.sharedApplication.delegate.window != nil) {
        return UIApplication.sharedApplication.delegate.window;
    } else {
         return UIApplication.sharedApplication.keyWindow;
    }
}

@interface WDPopBaseView ()
@property (nonatomic, assign) WDPopAnimationType animationType;
@property (nonatomic, assign) CGRect originRect;
@property (nonatomic, assign) CGRect animationRect;
@end

@implementation WDPopBaseView

+ (instancetype)defaultView {
    static dispatch_once_t onceToken;
    static WDPopBaseView *view = nil;
    dispatch_once(&onceToken, ^{
        view = [[self alloc] init];
    });
    return view;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addTarget:self action:@selector(hidenView) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)showView {
    [self showViewInSuperView:wKeyWindow()];
    self.animationType = WDPopAnimationTypeDefault;
}

- (void)showViewInSuperView:(UIView *)superView {
    self.animationType = WDPopAnimationTypeDefault;
    if (superView != nil) {
        [self showViewWithAnimationType:WDPopAnimationTypeDefault inSuperView:superView];
    } else {
        [self showViewWithAnimationType:WDPopAnimationTypeDefault inSuperView:wKeyWindow()];
    }
}

- (void)showViewWithAnimationType:(WDPopAnimationType)animationType {
    self.animationType = animationType;
    [self showViewWithAnimationType:animationType inSuperView:wKeyWindow()];
}

- (void)showViewWithAnimationType:(WDPopAnimationType)animationType inSuperView:(UIView *)superView {
    if (superView != nil) {
        [superView addSubview:self];
    } else {
        [wKeyWindow() addSubview:self];
    }
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
    
    UIView *view = self.subviews.firstObject;
    CGRect origin = view.frame;
    self.originRect = view.frame;
    
    CGRect screen_bounds = [[UIScreen mainScreen] bounds];
    CGFloat screen_width = screen_bounds.size.width;
    CGFloat screen_height = screen_bounds.size.height;
    
    CGFloat view_width = CGRectGetWidth(view.frame);
    CGFloat view_height = CGRectGetHeight(view.frame);
    CGFloat view_x = CGRectGetMinX(view.frame);
    CGFloat view_y = CGRectGetMinY(view.frame);

    self.frame = screen_bounds;
    
    if (animationType == WDPopAnimationTypeDefault) {
        view.transform = CGAffineTransformMakeScale(0, 0);
        [UIView animateWithDuration:WDPopAnmationDurition animations:^{
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
            view.transform = CGAffineTransformIdentity;
        }];
    } else {
        switch (animationType) {
           case WDPopAnimationTypeFromTop:
           {
               view.frame = CGRectMake(view_x, view_y - screen_height, view_width, view_height);
           }
               break;
           case WDPopAnimationTypeFromBottom:
           {
               view.frame = CGRectMake(view_x, view_y + screen_height, view_width, view_height);
           }
               break;
           case WDPopAnimationTypeFromLeft:
           {
               view.frame = CGRectMake(-screen_width, view_y, view_width, view_height);
           }
               break;
           case WDPopAnimationTypeFromRight:
           {
               view.frame = CGRectMake(screen_width, view_y, view_width, view_height);
           }
               break;
           default:
               break;
        }
        [self handerViewFrame:view originFrame:origin];
    }
}

- (void)handerViewFrame:(UIView *)view originFrame:(CGRect)originFrame {
    self.animationRect = view.frame;
    [UIView animateWithDuration:WDPopAnmationDurition animations:^{
        view.frame = originFrame;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }];
}

- (void)hidenView {
    UIView *view = self.subviews.firstObject;
    [UIView animateWithDuration:WDPopAnmationDurition delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        if (self.animationType != WDPopAnimationTypeDefault) {
            view.frame = self.animationRect;
        } else {
            view.transform = CGAffineTransformMakeScale(0.2, 0.2);
        }
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        view.transform = CGAffineTransformIdentity;
        view.frame = self.originRect;
    }];
}
@end
#import "WDPopBaseView.h"

NS_ASSUME_NONNULL_BEGIN

typedef void(^TestViewBlock)(NSString *obj);

@interface TestView : WDPopBaseView
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, copy) TestViewBlock testViewBlock;
- (void)showView:(TestViewBlock)finished;

@end

NS_ASSUME_NONNULL_END
#import "TestView.h"

@implementation TestView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _view = [[UIView alloc] init];
        _view.backgroundColor = [UIColor whiteColor];
        _view.layer.cornerRadius = 10;
        _view.frame = CGRectMake(30, (CGRectGetHeight(UIScreen.mainScreen.bounds) - 200) / 2, CGRectGetWidth(UIScreen.mainScreen.bounds) - 60, 200);
        [self addSubview:_view];
            
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"標(biāo)題";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
        _titleLabel.frame = CGRectMake(0, 10, CGRectGetWidth(_view.frame), 30);
        [_view addSubview:_titleLabel];

        _textField = [[UITextField alloc] init];
        _textField.placeholder = @"1111";
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.frame = CGRectMake(15, 50, CGRectGetWidth(UIScreen.mainScreen.bounds) - 90, 40);
        [_view addSubview:_textField];
        
        _button = [[UIButton alloc] init];
        _button.backgroundColor = [UIColor redColor];
        _button.layer.cornerRadius = 10;
        [_button setTitle:@"Sure" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:15];
        _button.frame = CGRectMake(15, CGRectGetMaxY(_textField.frame) + 10, CGRectGetWidth(UIScreen.mainScreen.bounds) - 90, 40);
        [_button addTarget:self action:@selector(buttonTargetAction:) forControlEvents:UIControlEventTouchUpInside];
        [_view addSubview:_button];
    }
    return self;
}

- (void)buttonTargetAction:(id)sender {
    [self hidenView];
}

- (void)showView:(TestViewBlock)finished {
    [self showView];
    [self.textField becomeFirstResponder];
    self.testViewBlock = finished;
}

- (void)hidenView {
    [super hidenView];
    if (self.testViewBlock) {
        [self.textField resignFirstResponder];
        self.testViewBlock(_textField.text);
        _textField.text = @"";
    }
}

@end
#import "ViewController.h"
#import "TestView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [[TestView defaultView] showView:^(NSString * _Nonnull obj) {
        NSLog(@"%@", obj);
    }];
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末逛绵,一起剝皮案震驚了整個(gè)濱河市墙歪,隨后出現(xiàn)的幾起案子垮媒,更是在濱河造成了極大的恐慌舍悯,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件睡雇,死亡現(xiàn)場(chǎng)離奇詭異萌衬,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)它抱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門秕豫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人观蓄,你說(shuō)我怎么就攤上這事混移。” “怎么了侮穿?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵歌径,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我亲茅,道長(zhǎng)回铛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任克锣,我火速辦了婚禮茵肃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘袭祟。我一直安慰自己验残,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布巾乳。 她就那樣靜靜地躺著胚膊,像睡著了一般故俐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上紊婉,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天药版,我揣著相機(jī)與錄音,去河邊找鬼喻犁。 笑死槽片,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肢础。 我是一名探鬼主播还栓,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼传轰!你這毒婦竟也來(lái)了剩盒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤慨蛙,失蹤者是張志新(化名)和其女友劉穎辽聊,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體期贫,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡跟匆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了通砍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片玛臂。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖封孙,靈堂內(nèi)的尸體忽然破棺而出迹冤,到底是詐尸還是另有隱情,我是刑警寧澤虎忌,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布泡徙,位于F島的核電站,受9級(jí)特大地震影響呐籽,放射性物質(zhì)發(fā)生泄漏锋勺。R本人自食惡果不足惜蚀瘸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一狡蝶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贮勃,春花似錦贪惹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)枫绅。三九已至,卻和暖如春硼端,著一層夾襖步出監(jiān)牢的瞬間并淋,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工珍昨, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留县耽,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓镣典,卻偏偏與公主長(zhǎng)得像兔毙,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子兄春,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361