2018-08-06 swift 自定義hud
專案中用到的圖片也可以使用程式碼畫出來。
使用方法
show: YTCustomeHudView.showHudWithText(ShowHudType.loading,"loading...")
dismiss: YTCustomeHudView.dismissHud()
importFoundation
class YTCustomeHudView :NSObject{
privatestaticfunccreateLayer() ->CAShapeLayer{
letpath :UIBezierPath=UIBezierPath(arcCenter:CGPoint(x: 25, y: 25), radius: 17, startAngle: .pi*0.5, endAngle:.pi*0.25, clockwise:false)
let layer : CAShapeLayer =CAShapeLayer()
layer.path= path.cgPath
layer.lineWidth= 2.5
layer.fillColor = UIColor.clear.cgColor
layer.strokeColor = Construction.instance.hex(0x26ABFB).cgColor
returnlayer
}
privatestaticfunccreateAnimation() ->CABasicAnimation{
letanimation =CABasicAnimation(keyPath:"transform.rotation.z")
animation.duration= 1.5
animation.repeatCount=HUGE
animation.autoreverses=false
animation.toValue=Double.pi*2
returnanimation
}
class func showHudWithText(type:ShowHudType,msg:String){
letbgView =UIView()
bgView.backgroundColor = UIColor.clear
bgView.tag= 999
bgView.frame = UIScreen.main.bounds
UIApplication.shared.keyWindow?.addSubview(bgView)
lethudView =UIView()
hudView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
hudView.frame=CGRect(x: 0, y: 0, width: 120, height: 140)
hudView.center = CGPoint(x: YTConstants.ScreenWidth/2, y: YTConstants.ScreenHeight/2)
hudView.layer.cornerRadius= 2
hudView.layer.masksToBounds=true
bgView.addSubview(hudView)
leticonImgView :UIImageView= UIImageView(frame:CGRect(x: 35, y: 30, width: 50, height: 50))
iconImgView.image= ShowHudType.getHudView(type)()
hudView.addSubview(iconImgView)
lettitleLabel =UILabel(frame:CGRect(x: 10, y: iconImgView.frame.origin.y+iconImgView.frame.size.height+16, width: 100, height: 24))
titleLabel.font=UIFont.systemFont(ofSize: 16)
titleLabel.textColor=UIColor.white
titleLabel.textAlignment= .center
titleLabel.numberOfLines= 0
titleLabel.text= msg
hudView.addSubview(titleLabel)
iftype ==ShowHudType.loading{
iconImgView.layer.insertSublayer(createLayer(), at: 0)
iconImgView.layer.add(createAnimation(), forKey:"rotateAnimation")
}else{
DispatchQueue.main.asyncAfter(deadline: .now()+1.5, execute: {
self.dismissHud()
})
}
hudView.alpha= 0
UIView.animate(withDuration: 0.5, animations: {
hudView.alpha= 1
})
}
class func dismissHud(){
let view = UIApplication.shared.keyWindow?.viewWithTag(999) as UIView?
UIView.animate(withDuration: 0.3, animations: {
view?.alpha= 0
}, completion: { flagin
view?.removeFromSuperview()
})
}
}
enumShowHudType{
caseinfo
casesuccess
caseerror
caseloading
public funcgetHudView()->UIImage{
switchself{
case.info:
returnUIImage(named:"hud_info")!
case.success:
returnUIImage(named:"hud_success")!
case.error:
returnUIImage(named:"hud_error")!
case.loading:
returnUIImage(named:"hud_loading")!
}
}
}
相關文章
- Swift 自定義 UIDatePickerSwiftUI
- Swift中自定義運算子Swift
- Swift自定義表情鍵盤+錄音Swift
- 自定義TabBar動畫效果 - 頁面轉場(Swift)tabBar動畫Swift
- Swift 專案總結 03 自定義 CollectionView 佈局SwiftView
- Swift 專案總結 04 自定義控制器轉場Swift
- Swift 專案總結 04 - 自定義控制器轉場Swift
- 使用AVPlayer自定義支援全屏的播放器(五)—Swift重構版本播放器Swift
- android自定義view(自定義數字鍵盤)AndroidView
- netty自定義Decoder用於自定義協議Netty協議
- 自定義VIEWView
- 自定義SnackBar
- 自定義_ajax
- 自定義 Anaconda
- 自定義ClassLoader
- 自定義QTimeEditQT
- 自定義 tabBartabBar
- tailwind自定義AI
- 自定義 GitGit
- 自定義元件元件
- 自定義表格
- 自定義LinkedList
- 自定義useState
- GoCN每日新聞(2018-08-06)Go
- vue自定義全域性元件(或自定義外掛)Vue元件
- 4. 自定義控制元件(4) --- 自定義屬性控制元件
- Android 自定義View:深入理解自定義屬性(七)AndroidView
- MySQL自定義排序MySql排序
- JavaScript自定義事件JavaScript事件
- Maven 自定義archeTypeMaven
- SpringBoot 自定義 starterSpring Boot
- 自定義的PopupWindow
- 自定義元件——TitleView元件View
- 自定義目錄
- 自定義 Artisan 命令
- 自定義型別型別
- Gradle 自定義 pluginGradlePlugin
- 自定義Spring ShellSpring