說到彈框控件,想必我們用到最多的應(yīng)該就是UIAlertView啦,雖然蘋果官方在iOS9.0之后推出來了UIAlertController,也可以達(dá)到之前的效果虎谢,但是調(diào)用起來比原來麻煩多了,而且如果使用UIAlertController,你的項目就無法在iOS9.0以下的設(shè)備上運行昏兆。
今天筆者給大家分享一個添加自動消失效果的彈框,說白了就是UIAlertView+NSTimer封裝了一下妇穴。
效果圖如下:
在ViewController.h中導(dǎo)入SZKCustomAlter.h頭文件
一句代碼調(diào)用
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
[SZKCustomAlter showAlter:@"簡書號:iOS_凱"];
}
實現(xiàn)方法:
SZKCustomAlter.h
修改ALTERTIME
的值爬虱,控制彈框在多長時間后消失
//
// SZKCustomAlter.h
// CustomAlterView
//
// Created by sunzhaokai on 16/7/27.
// Copyright ? 2016年 孫趙凱. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#define ALTERTIME 2
@interface SZKCustomAlter : NSObject
+(void)showAlter:(NSString *)message;
@end
SZKCustomAlter.m中
//
// SZKCustomAlter.m
// CustomAlterView
//
// Created by sunzhaokai on 16/7/27.
// Copyright ? 2016年 孫趙凱. All rights reserved.
//
#import "SZKCustomAlter.h"
@implementation SZKCustomAlter
+(void)showAlter:(NSString *)message;
{
UIAlertView *alter=[[UIAlertView alloc]initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[NSTimer scheduledTimerWithTimeInterval:ALTERTIME target:self selector:@selector(timerAction:) userInfo:alter repeats:NO];
[alter show];
}
+(void)timerAction:(NSTimer *)timer
{
UIAlertView *alter=(UIAlertView *)[timer userInfo];
[alter dismissWithClickedButtonIndex:0 animated:YES];
}
@end
備注:
1.使用系統(tǒng)的UIAlertView+NSTimer進行了簡單的封裝隶债,調(diào)用簡單,使用方便跑筝。
2.僅有一個輸入內(nèi)容的接口死讹,可拓展性差,控件大小曲梗,位置赞警,字體等均不能改變,僅用于一個簡單的提示效果稀并。
3.如果想要自定義彈框仅颇,可以參考:
iOS開發(fā)-輕松學(xué)會封裝自定義視圖view(自定義彈框封裝詳解)
http://www.reibang.com/p/de2ecfd770c2
筆者的其他文章:
iOS開發(fā)-兩句代碼快速實現(xiàn)無限輪播圖(基于ScrollView封裝)
http://www.reibang.com/p/d240bd977689
iOS在線音樂播放SZKAVPlayer(基于AVPlayer的封裝)
http://www.reibang.com/p/4e0ac2898de0
iOS開發(fā)-一句代碼調(diào)用實現(xiàn)網(wǎng)絡(luò)的監(jiān)測功能(基于AFNetworkReachabilityManager的封裝)
http://www.reibang.com/p/b901ad0c1d81
如果有不足或者錯誤的地方還望各位讀者批評指正,可以評論留言碘举,筆者收到后第一時間回復(fù)忘瓦。
QQ/微信:790057066 。
簡書號:iOS_凱:http://www.reibang.com/users/86b0ddc92021/latest_articles
GitHub個人主頁(歡迎star):https://github.com/18811314750
感謝各位觀眾老爺?shù)拈喿x引颈,如果覺得筆者寫的還湊合耕皮,可以關(guān)注或收藏一下,不定期分享一些好玩的實用的demo給大家蝙场。