說到彈框控件,想必我們用到最多的應該就是UIAlertView啦,雖然蘋果官方在iOS9.0之后推出來了UIAlertController,也可以達到之前的效果,但是調用起來比原來麻煩多了,而且如果使用UIAlertController,你的項目就無法在iOS9.0以下的設備上運行。
今天筆者給大家分享一個添加自動消失效果的彈框,說白了就是UIAlertView+NSTimer封裝了一下。
效果圖如下:
在ViewController.h中導入SZKCustomAlter.h頭文件
一句代碼調用
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
[SZKCustomAlter showAlter:@"簡書號:iOS_凱"];
}
實現方法:
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.使用系統的UIAlertView+NSTimer進行了簡單的封裝,調用簡單,使用方便。
2.僅有一個輸入內容的接口,可拓展性差,控件大小,位置,字體等均不能改變,僅用于一個簡單的提示效果。
3.如果想要自定義彈框,可以參考:
iOS開發-輕松學會封裝自定義視圖view(自定義彈框封裝詳解)
http://www.lxweimin.com/p/de2ecfd770c2
筆者的其他文章:
iOS開發-兩句代碼快速實現無限輪播圖(基于ScrollView封裝)
http://www.lxweimin.com/p/d240bd977689
iOS在線音樂播放SZKAVPlayer(基于AVPlayer的封裝)
http://www.lxweimin.com/p/4e0ac2898de0
iOS開發-一句代碼調用實現網絡的監測功能(基于AFNetworkReachabilityManager的封裝)
http://www.lxweimin.com/p/b901ad0c1d81
如果有不足或者錯誤的地方還望各位讀者批評指正,可以評論留言,筆者收到后第一時間回復。
QQ/微信:790057066 。
簡書號:iOS_凱:http://www.lxweimin.com/users/86b0ddc92021/latest_articles
GitHub個人主頁(歡迎star):https://github.com/18811314750
感謝各位觀眾老爺的閱讀,如果覺得筆者寫的還湊合,可以關注或收藏一下,不定期分享一些好玩的實用的demo給大家。