在首次启动项目时,需求常常会要求你在特定的位置添加说明图,以帮助新用户更好使用 App ,这些说明图又叫引导图,本文称之为 tips.
public class TipsView:UIView {
/**
创建Tips
- parameter imageStr: tips 图片
- parameter hasCloseBtn: 是否有关闭按钮 **将会创建在 tips 右上角**
- parameter hasCloseGesture: 是否有点击 tips 的关闭手势
- parameter key: 标注 tips 的 key **唯一存在**
*/
func showTips(imageStr:String,hasCloseBtn:Bool,hasCloseGesture:Bool,key:String){
if !NSUserDefaults.standardUserDefaults().boolForKey(key) {
//判断是否是第一次创建
NSUserDefaults.standardUserDefaults().setBool(true, forKey: key)
//tips图片
let tipsImageView = UIImageView.init(frame: CGRectMake(0, 0, self.frame.width, self.frame.height))
tipsImageView.image = UIImage(named: imageStr)
self .addSubview(tipsImageView)
//关闭按钮
if hasCloseBtn {
let closeTipsBtn = UIButton(type:.Custom)
closeTipsBtn.frame = CGRectMake(self.frame.width-50, 20, 40, 40)
closeTipsBtn.setImage(UIImage(named: "tips_close"), forState: UIControlState.Normal)
closeTipsBtn.addTarget(self, action:#selector(closeBtnAction) , forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(closeTipsBtn)
}
//关闭手势
if hasCloseGesture {
tipsImageView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self,action: #selector(tapGestureAction))
tipsImageView.addGestureRecognizer(tapGesture)
}
// UIApplication.sharedApplication().windows.first?.makeKeyWindow()
UIApplication.sharedApplication().keyWindow?.addSubview(self)
}
}
/**
添加透明按钮来响应事件
- parameter btnFrame: 按钮的 frame
- parameter target: 事件的执行者
- parameter action: 事件
*/
func addEmptyBtnToAction(btnFrame:CGRect,target:AnyObject,action:Selector) {
let emptyBtn = UIButton(type:.Custom)
emptyBtn.frame = btnFrame
emptyBtn.addTarget(target, action:action, forControlEvents: .TouchUpInside)
self.addSubview(emptyBtn)
}
// MARK: - private method
func closeBtnAction() {
self.removeFromSuperview()
}
func tapGestureAction() {
self.closeBtnAction()
}
}
当你实现上面这个类之后,即可简单方便的调用
override func viewDidLoad() {
super.viewDidLoad()
//添加 tips
let tips = TipsView.init(frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
tips.showTips("tips", hasCloseBtn: true, hasCloseGesture: true, key: "FirstTips")
}
期待你的评论建议O(∩_∩)O~
网友评论