// // GuideTheOperationImage.swift // ShareCar // // Created by WeiChaoZheng on 2017/11/3. // Copyright © 2017年 jianYou. All rights reserved. // import UIKit class GuideTheOperationImage: UIView { var removeBlock:(()->Void)? /// 圆形0 圆角矩形1 椭圆2 var rectType = "" /// 透明的区域 var showImageView :UIImageView? var showNextBtn = UIButton.init(type: .custom) var areaRect : CGRect? class func newInstance() -> GuideTheOperationImage? { let nibView = Bundle.main.loadNibNamed("GuideTheOperationImage", owner: nil, options: nil); if let view = nibView?.first as? GuideTheOperationImage { return view } return nil } /// 设置位置 /// /// - Parameters: /// - frame: 矩形范围 /// - shape: 圆形0 圆角矩形1 椭圆2 /// - type: head 头像的 location 定位的 add 添加设备的 func setFrameAndShape(frame:CGRect, shape:String, type:String){ self.areaRect = frame; self.rectType = shape; let screenWidth = UIScreen.main.bounds.width let screenHeight = UIScreen.main.bounds.height if(type == "watch"){ //指向我的设备的 165h let imageFrame = CGRect.init(x: 20, y: screenHeight-frame.height-165-30, width:screenWidth , height: 165) showImageView = UIImageView.init(frame: imageFrame) showImageView?.image = UIImage.init(named: "设备指导图") let btnFrame = CGRect.init(x: screenWidth-30-100, y: 75, width: 100, height: 52) showNextBtn.frame = btnFrame showNextBtn.setBackgroundImage(UIImage.init(named: "下一步指导图按钮"), for: .normal) }else if(type == "add"){ //添加设备的 165h let imageFrame = CGRect.init(x: 0, y: frame.origin.y+frame.height, width:screenWidth , height: 165) showImageView = UIImageView.init(frame: imageFrame) showImageView?.image = UIImage.init(named: "添加新设备指导图") let btnFrame = CGRect.init(x: screenWidth-30-100, y: screenHeight-52-45, width: 100, height: 52) showNextBtn.frame = btnFrame showNextBtn.setBackgroundImage(UIImage.init(named: "下一步指导图按钮"), for: .normal) }else if(type == "location"){ //定位的 165h let imageFrame = CGRect.init(x: 0, y: screenHeight-frame.height-165-20, width:screenWidth , height: 165) showImageView = UIImageView.init(frame: imageFrame) showImageView?.image = UIImage.init(named: "定位指导图") let btnFrame = CGRect.init(x: screenWidth-30-100, y: 75, width: 100, height: 52) showNextBtn.frame = btnFrame showNextBtn.setBackgroundImage(UIImage.init(named: "我知道了指导图按钮"), for: .normal) } if(showImageView != nil){ showImageView?.contentMode = .scaleAspectFit self.addSubview(showImageView!) self.addSubview(showNextBtn) showNextBtn.addTarget(self, action: #selector(self.OKAction), for: .touchUpInside) } self.setNeedsDisplay() //会立即调用 draw 方法 } @objc func OKAction() { if removeBlock != nil { removeBlock!() } self.removeFromSuperview() } override func awakeFromNib() { self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.OKAction))) } override func draw(_ rect: CGRect) { super.draw(rect) if(self.areaRect != nil){ //获取画板 let context = UIGraphicsGetCurrentContext()! context.clear(self.bounds); //清除画板 context.setFillColor(UIColor.black.withAlphaComponent(0.2).cgColor) let bezPath = UIBezierPath.init(rect: self.bounds) var addPath : UIBezierPath! switch self.rectType { case "0"://圆 addPath = UIBezierPath.init(roundedRect: self.areaRect!, cornerRadius: self.areaRect!.width/2) break case "1"://圆角矩形 addPath = UIBezierPath.init(roundedRect: self.areaRect!, cornerRadius: 5) break case "2"://椭圆 addPath = UIBezierPath.init(ovalIn: self.areaRect!) break default: addPath = UIBezierPath.init() break } bezPath.append(addPath.reversing()) context.addPath(bezPath.cgPath) context.fillPath() } } }