
城市有我奮斗的青春.jpg
這波怕是要來一波濕得了,今天2017年農(nóng)歷1月22。
恩,今天是我的生日。
還好周六,寫了一個提示框分享一下。
本來只是想模仿QQ的提示框動畫的,寫著寫著寫了好多。
做iOS的時間說長不長說短也不短,今天這個框架全當生日禮物了,么么噠。
還是老套路看下效果圖吧。

背景圖還是要騷.png

展示兩個,哦啦.png
先說下思路吧:
一開始我繼承個UIView直接開懟,然而我還需要一個模糊背景這就比較蛋疼了。
扒了幾下看到一個UIVisualEffectView,所以就用了。
很簡單:就是畫好控件全部加在 UIVisualEffectView 然后大家都懂提示框加在window。
實現(xiàn)外界可調(diào)用的接口:彈出提示框,清除提示框等等一些方法。
貼下代碼:
1.懶加載把所有的控件都寫好加在self(UIVisualEffectView)上
@interface DzProgressHUD ()
@property(strong,nonatomic)UIView * backGViewForCenter;
@property(copy ,nonatomic)NSString * message;
@property(strong,nonatomic)UILabel * messageLab;
@property(strong,nonatomic)UIImageView * successImg;
@property(strong,nonatomic)UIImageView * faildImg;
@property(weak,nonatomic)UIActivityIndicatorView * aivLoading;
@property(strong,nonatomic)UIImageView * gifImg;
// QQ提示框
@property(assign,nonatomic)BOOL isQQPromatBox;
@property(strong,nonatomic)UIImageView * promatSuccessImg;
@property(strong,nonatomic)UIImageView * promatFaildImg;
@property(strong,nonatomic)UILabel * titleLab;
@property(strong,nonatomic)UIView * backGView;
@end
#pragma mark --- 懶加載 ---
-(UIView *)backGViewForCenter
{
if (_backGViewForCenter == nil) {
UIView * view = [[UIView alloc]init];
view.backgroundColor = [UIColor whiteColor];
[self addSubview:view];
_backGViewForCenter = view;
}
return _backGViewForCenter;
}
-(UIActivityIndicatorView *)aivLoading
{
if (_aivLoading == nil) {
UIActivityIndicatorView *aivLoading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[aivLoading startAnimating];
aivLoading.color = [UIColor whiteColor];
[self addSubview:aivLoading];
_aivLoading = aivLoading;
}
return _aivLoading;
}
-(UILabel *)messageLab
{
if (_messageLab == nil) {
UILabel * lab = [[UILabel alloc]init];
lab.textAlignment = NSTextAlignmentCenter;
lab.font = [UIFont systemFontOfSize:14];
lab.textColor = [UIColor whiteColor];
lab.numberOfLines = 0;
[self addSubview:lab];
_messageLab = lab;
}
return _messageLab;
}
// 貼三個實現(xiàn) ,都是一個套路
2.從layoutSubviews布局好每一個控件的位置
#pragma mark --- 最后的布局 ---
-(void)layoutSubviews
{
if (self.isQQPromatBox) {
[self qqPromptBoxLayout];
}else{
[self dzPromptBoxLayout];
}
}
-(void)dzPromptBoxLayout
{
DzProgressHUD * hudForQQ = [DzProgressHUD promatBoxForQQ];
[hudForQQ removeFromSuperview];
CGFloat promatBoxWH;
DzProgressHUD * hud = [DzProgressHUD promatBox];
if (hud.gifImg.hidden == NO) {
promatBoxWH = 200;
}else promatBoxWH = 120;
CGFloat promatBoxX = (WIDTH - promatBoxWH)*0.5;
CGFloat promatBoxY = (HEIGHT- promatBoxWH)*0.5;
self.frame = CGRectMake(promatBoxX, promatBoxY, promatBoxWH, promatBoxWH);
self.backGViewForCenter.frame = CGRectMake(promatBoxX, promatBoxY, promatBoxWH, promatBoxWH);
CGFloat promatBoxW = self.frame.size.width;
CGFloat promatBoxH = self.frame.size.height;
CGFloat imgW = 50;
CGFloat imgX = (promatBoxW - imgW) * 0.5;
CGFloat imgY = (promatBoxH - imgW) * 0.5;
self.successImg.frame = CGRectMake(imgX, imgY, imgW, imgW);
self.faildImg.frame = CGRectMake(imgX, imgY, imgW, imgW);
self.aivLoading.frame = CGRectMake(imgX, imgY, imgW, imgW);
self.gifImg.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
if (![@"" isEqualToString:self.message]) {
UILabel * lab = [[UILabel alloc]init];
lab.text = self.message;
[lab sizeToFit];
CGFloat w = lab.frame.size.width;
CGFloat h = lab.frame.size.height;
CGSize size = CGSizeMake(w, h);
// 略微調(diào)高
CGFloat imgY = (self.frame.size.height - imgW) * 0.5 - 10;
CGRect frame = self.aivLoading.frame;
frame.origin.y= imgY;
self.aivLoading.frame = frame;
self.successImg.frame = frame;
self.faildImg.frame = frame;
CGFloat messageW = promatBoxW - 12 * 2;
CGFloat messageH = size.height;
CGFloat messageX = (promatBoxW - messageW) * 0.5;
CGFloat messageY = CGRectGetMaxY(self.successImg.frame) + 12;
self.messageLab.frame = CGRectMake(messageX,messageY, messageW, messageH);
// 如果高度超過限定的高度50,增加自身高度
if (size.height > 50) {
CGRect frame = self.frame;
frame.size.height = CGRectGetMaxY(self.messageLab.frame) + 12;
frame.origin.x = WIDTH - self.frame.size.width * 0.5;
self.frame = frame;
self.gifImg.frame = self.frame;
}
}
}
-(void)qqPromptBoxLayout
{
self.layer.cornerRadius = 0;
CGFloat bgViewH = 64;
CGFloat bgViewX = 0;
CGFloat bgViewY = 0;
CGFloat bgViewW = WIDTH;
self.frame = CGRectMake(bgViewX, bgViewY, bgViewW, bgViewH);
self.backGView.frame = CGRectMake(bgViewX, bgViewY, bgViewW, bgViewH);
CGFloat imgWH = 26;
CGFloat imgX = 10;
CGFloat imgY = (bgViewH - imgWH)* 0.5+12;
self.promatFaildImg.frame = CGRectMake(imgX, imgY, imgWH, imgWH);
self.promatSuccessImg.frame = CGRectMake(imgX, imgY, imgWH, imgWH);
CGFloat labX = CGRectGetMaxX(self.promatFaildImg.frame)+10;
CGFloat labY = imgY;
CGFloat labW = WIDTH - labX - 10;
CGFloat labH = imgWH;
self.titleLab.frame = CGRectMake(labX, labY, labW, labH);
}
3.實現(xiàn)外界可調(diào)用的接口:我這里寫的類方法,因為是提示框所以存在這么一個情況(網(wǎng)絡請求->菊花轉(zhuǎn)動,請求完成或失敗后->清除提示框,所以在類中創(chuàng)建了單例可以理解為self來使用)
// 所有外界可調(diào)用的接口基本都是這么實現(xiàn)的,我利用hidden屬性來控制的UI(可能比較low希望童鞋們有好的辦法可以教我一下)
+ (void)showFaild:(NSString *)message intervalTime:(CGFloat)intervalTime
{
// 添加視圖
UIWindow * window = [UIApplication sharedApplication].keyWindow;
DzProgressHUD * hud = [self promatBox];
hud.isQQPromatBox = NO;
[hud ownSet];
[window addSubview:hud];
// 傳遞文字數(shù)據(jù)
hud.message = message;
hud.messageLab.text = hud.message;
// 顯示和隱藏的模塊
hud.backGView.hidden = YES;
hud.successImg.hidden = YES;
hud.faildImg.hidden = NO;
hud.aivLoading.hidden = YES;
hud.messageLab.hidden = NO;
hud.gifImg.hidden = YES;
// 重新布局
[hud layoutSubviews];
// 動畫
hud.transform = CGAffineTransformScale(hud.transform, 0.1, 0.1);
[UIView animateWithDuration:0.3 animations:^{
hud.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
// 自動消失
CGFloat time = intervalTime;
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time/*延遲執(zhí)行時間*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
[DzProgressHUD dismiss];
});
}
#pragma mark -- 模仿QQ提示框 ---
// 這里是模仿QQ提示框的實現(xiàn)部分(其實都一樣)
+(void)showQQPromatBoxForGreenTitle:(NSString *)title
{
UIWindow * window = [UIApplication sharedApplication].keyWindow;
DzProgressHUD * hud = [DzProgressHUD promatBox];
[window addSubview:hud];
[hud hidsomeView];
hud.isQQPromatBox = YES;
hud.titleLab.text = title;
hud.backGView.hidden = NO;
hud.promatFaildImg.hidden = YES;
hud.promatSuccessImg.hidden = NO;
hud.titleLab.hidden = NO;
[hud layoutSubviews];
hud.transform = CGAffineTransformMakeTranslation(0, HEIGHT/2 - HEIGHT);
// hud.transform = CGAffineTransformTranslate(hud.transform, 0, HEIGHT/2 - HEIGHT);
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.6 options:(UIViewAnimationOptionTransitionFlipFromTop) animations:^{
hud.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1/*延遲執(zhí)行時間*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
[self dismiss];
});
}];
}
好了,容我附上Demo下載地址:https://pan.baidu.com/s/1kUKrd35
demo中還有一個自己封裝的網(wǎng)絡請求類,所以有點亂名字也是網(wǎng)絡請求的.
再bb點沒用的:
今天是我的23歲生日,也是如此平常的一天。
早上在公交上走神還做過了站,很難受。遲到了(雖然我的內(nèi)心是拒絕的)。
但是我?guī)Я藥灼磕毯鸵恍┣煽肆Ψ窒斫o同事(我們這個小團隊很和諧),大家都很開心。
but,全公司就我一個單身汪(哇,好氣。)。
另:
希望有可以用到我的東西的童鞋升職加薪,早日脫單,BUG滾蛋。
實在過意不去就給評論生日快樂助助興 哈哈 。
無兄弟不擼碼,兄弟們一塊加油! GO GO GO 。
(下一篇可能會更新關(guān)于動畫文章,謝謝大家)