Swift语言学习之如何用swift写ProgressHUD.docx
《Swift语言学习之如何用swift写ProgressHUD.docx》由会员分享,可在线阅读,更多相关《Swift语言学习之如何用swift写ProgressHUD.docx(15页珍藏版)》请在冰豆网上搜索。
Swift语言学习之如何用swift写ProgressHUD
Swift语言学习之如何用swift写ProgressHUD
//
//ProgressHUD.swift
//BiaoGeMusic
//
//Createdbyljy-335on14-10-21.
//Copyright(c)2014年uni2uni.Allrightsreserved.
//
importFoundation
importUIKit
///
///@brief样式
enumHYBProgressHUDStyle{
caseBlackHUDStyle///黑色风格
caseWhiteHUDStyle///白色风格
}
///
///@brief定制显示通知的视图HUD
///@authorhuangyibiao
classHYBProgressHUD:
UIView{
varhud:
UIToolbar?
varspinner:
UIActivityIndicatorView?
varimageView:
UIImageView?
vartitleLabel:
UILabel?
///
///private属性
///
privateletstatusFont=UIFont.boldSystemFontOfSize(16.0)
privatevarstatusColor:
UIColor!
privatevarspinnerColor:
UIColor!
privatevarbgColor:
UIColor!
privatevarsuccessImage:
UIImage!
privatevarerrorImage:
UIImage!
///
///@brief单例方法,只允许内部调用
privateclassfuncsharedInstance()->HYBProgressHUD{
structInstance{
staticvaronceToken:
dispatch_once_t=0
staticvarinstance:
HYBProgressHUD?
}
dispatch_once(&Instance.onceToken,{()->Voidin
Instance.instance=HYBProgressHUD(frame:
UIScreen.mainScreen().bounds)
Instance.instance?
.setStyle(HYBProgressHUDStyle.WhiteHUDStyle)
})
returnInstance.instance!
}
overrideinit(frame:
CGRect){
super.init(frame:
frame)
hud=nil
spinner=nil
imageView=nil
titleLabel=nil
self.alpha=0.0
}
requiredinit(coderaDecoder:
NSCoder){
fatalError("init(coder:
)hasnotbeenimplemented")
}
///
///公开方法
///
///显示信息
classfuncshow(status:
String){
sharedInstance().configureHUD(status,image:
nil,isSpin:
true,isHide:
false)
}
///显示成功信息
classfuncshowSuccess(status:
String){
sharedInstance().configureHUD(status,image:
sharedInstance().successImage,isSpin:
false,isHide:
true)
}
///显示出错信息
classfuncshowError(status:
String){
sharedInstance().configureHUD(status,image:
sharedInstance().errorImage,isSpin:
false,isHide:
true)
}
///隐藏
classfuncdismiss(){
sharedInstance().hideHUD()
}
///
///私有方法
///
///
///@brief创建并配置HUD
privatefuncconfigureHUD(status:
String?
image:
UIImage?
isSpin:
Bool,isHide:
Bool){
configureProgressHUD()
///标题
ifstatus==nil{
titleLabel!
.hidden=true
}else{
titleLabel!
.text=status!
titleLabel!
.hidden=false
}
//图片
ifimage==nil{
imageView?
.hidden=true
}else{
imageView?
.hidden=false
imageView?
.image=image
}
//spin
ifisSpin{
spinner?
.startAnimating()
}else{
spinner?
.stopAnimating()
}
rotate(nil)
addjustSize()
showHUD()
ifisHide{
NSThread.detachNewThreadSelector("hideWhenTimeout",toTarget:
self,withObject:
nil)
}
}
///
///@brief设置风格样式,默认使用的是黑色的风格,如果需要改成白色的风格,请在内部修改样式
privatefuncsetStyle(style:
HYBProgressHUDStyle){
switchstyle{
case.BlackHUDStyle:
statusColor=UIColor.whiteColor()
spinnerColor=UIColor.whiteColor()
bgColor=UIColor(white:
0,alpha:
0.8)
successImage=UIImage(named:
"ProgressHUD.bundle/success-white.png")
errorImage=UIImage(named:
"ProgressHUD.bundle/error-white.png")
break
case.WhiteHUDStyle:
statusColor=UIColor.whiteColor()
spinnerColor=UIColor.whiteColor()
bgColor=UIColor(red:
192.0/255.0,green:
37.0/255.0,blue:
62.0/255.0,alpha:
1.0)
successImage=UIImage(named:
"ProgressHUD.bundle/success-white.png")
errorImage=UIImage(named:
"ProgressHUD.bundle/error-white.png")
break
default:
break
}
}
///
///@brief获取窗口window
privatefuncgetWindow()->UIWindow{
ifletdelegate:
UIApplicationDelegate=UIApplication.sharedApplication().delegate{
ifletwindow=delegate.window{
returnwindow!
}
}
returnUIApplication.sharedApplication().keyWindow
}
///
///@brief创建HUD
privatefuncconfigureProgressHUD(){
ifhud==nil{
hud=UIToolbar(frame:
CGRectZero)
hud?
.barTintColor=bgColor
hud?
.translucent=true
hud?
.layer.cornerRadius=10
hud?
.layer.masksToBounds=true
///监听设置方向变化
NSNotificationCenter.defaultCenter().addObserver(self,
selector:
"rotate:
",
name:
UIDeviceOrientationDidChangeNotification,
object:
nil)
}
ifhud!
.superview==nil{
getWindow().addSubview(hud!
)
}
ifspinner==nil{
spinner=UIActivityIndicatorView(activityIndicatorStyle:
UIActivityIndicatorViewStyle.WhiteLarge)
spinner!
.color=spinnerColor
spinner!
.hidesWhenStopped=true
}
ifspinner!
.superview==nil{
hud!
.addSubview(spinner!
)
}
ifimageView==nil{
imageView=UIImageView(frame:
CGRectMake(0,0,28,28))
}
ifimageView!
.superview==nil{
hud!
.addSubview(imageView!
)
}
iftitleLabel==nil{
titleLabel=UILabel(frame:
CGRectZero)
titleLabel?
.backgroundColor=UIColor.clearColor()
titleLabel?
.font=statusFont
titleLabel?
.textColor=statusColor
titleLabel?
.baselineAdjustment=UIBaselineAdjustment.AlignCenters
titleLabel?
.numberOfLines=0
titleLabel?
.textAlignment=NSTextAlignment.Center
titleLabel?
.adjustsFontSizeToFitWidth=false
}
iftitleLabel!
.superview==nil{
hud!
.addSubview(titleLabel!
)
}
}
///
///@brief释放资源
privatefuncdestroyProgressHUD(){
NSNotificationCenter.defaultCenter().removeObserver(self,name:
UIDeviceOrientationDidChangeNotification,object:
nil)
titleLabel?
.removeFromSuperview()
titleLabel=nil
spinner?
.removeFromSuperview()
spinner=nil
imageView?
.removeFromSuperview()
imageView=nil
hud?
.removeFromSuperview()
hud=nil
}
///
///@brief设置方向变化通知处理
funcrotate(sender:
NSNotification?
){
varrotation:
CGFloat=0.0
switchUIApplication.sharedApplication().statusBarOrientation{
caseUIInterfaceOrientation.Portrait:
rotation=0.0
break
case.PortraitUpsideDown:
rotation=CGFloat(M_PI)
break
case.LandscapeLeft:
rotation=-CGFloat(M_PI_2)
break
case.LandscapeRight:
rotation=CGFloat(M_PI_2)
break
default:
break
}
hud?
.transform=CGAffineTransformMakeRotation(rotation)
}
///
///@brief调整大小
privatefuncaddjustSize(){
varrect=CGRectZero
varwidth:
CGFloat=100.0
varheight:
CGFloat=100.0
///计算文本大小
iftitleLabel!
.text!
=nil{
varstyle=NSMutableParagraphStyle()
style.lineBreakMode=NSLineBreakMode.ByCharWrapping
varattributes=[NSFontAttributeName:
statusFont,NSParagraphStyleAttributeName:
style.copy()]
varoption=NSStringDrawingOptions.UsesLineFragmentOrigin
vartext:
NSString=NSString(CString:
titleLabel!
.text!
.cStringUsingEncoding(NSUTF8StringEncoding)!
encoding:
NSUTF8StringEncoding)
rect=text.boundingRectWithSize(CGSizeMake(160,260),options:
option,attributes:
attributes,context:
nil)
rect.origin.x=12
rect.origin.y=66
width=rect.size.width+24
height=rect.size.height+80
ifwidth<100{
width=100
rect.origin.x=0
rect.size.width=100
}
}
hud!
.center=CGPointMake(kScreenWidth/2,kScreenHeight/2)
hud!
.bounds=CGRectMake(0,0,width,height)
varh=titleLabel!
.text==nil?
height/2:
36
imageView!
.center=CGPointMake(width/2,h)
spinner!
.center=CGPointMake(width/2,h)
titleLabel!
.frame=rect
}
///
///@brief显示
privatefuncshowHUD(){
ifself.alpha==0.0{
self.alpha=1.0
hud!
.alpha=0.0
self.hud!
.transform=CGAffineTransformScale(self.hud!
.transform,1.4,1.4)
UIView.animateKeyframesWithDuration(0.15,
delay:
0.0,
options:
UIViewKeyframeAnimationOptions.AllowUserInteraction,
animations:
{()->Voidin
self.hud!
.transform=CGAffineTransformScale(self.hud!
.transform,1.0/1.4,1.0/1.4)
self.hud!
.alpha=1.0
},completion:
{(isFinished)->Voidin
})
}
}
///
///@brief隐藏
privatefunchideHUD(){
ifself.alpha==1.0{
UIView.animateKeyframesWithDuration(0.2,
delay:
0.0,
options:
UIViewKeyframeAnimationOptions.AllowUserInteraction,
animations:
{()->Voidin
self.hud!
.transform=CGAffineTransformScale(self.hud!
.transform,0.35,0.35)
self.hud!
.alpha=0.0
},completion:
{(isFinished)->Voidin
self.destroyProgressHUD()
self.alpha=0.0
})
}
}
///
///@brief在指定时间内隐藏
funchideWhenTimeout(){
autoreleasepool{()->()in
varlength=countElements(self.titleLabel!
.text!
)
varsleepTime:
NSTimeInterval=NSTimeInterval(length)*0.04+0.5
NSThread.sleepForTimeInterval(sleepTime)
self.hideHUD()
}
}
}