環境:
Xcode:Xcode 8.2 Swift:swift3+
git 地址:https://github.com/TieShanWang/KKPopover
pod 集成:pod 'KKPopover'
【注意】pod 版本,swift 3 支持,以 3.+開頭,同理 swift4 以 4.+ 開頭
首先先看效果:
popover.gif
- 灰色區域 底層視圖
- 棕色 popover 泡泡不能超出的區域,popover 是不能超出屏幕或者離屏幕太近,否則效果不好。棕色區域是可以設置的
- 紅色 底層箭頭視圖的邊框
- 白色 底層箭頭視圖的背景顏色
- 藍色 自定義內容區域,在此區域(contentView)可以添加希望展示的內容
- 加號按鈕 模擬了 sourceView 所在的位置。可以看到當 sourceView(加號按鈕) 在不同地方的時候,popover 自動更改方向、調整箭頭方向、位置以適應不同的 sourceView
類說明
open class KKPopover: UIView
- 此視圖是底層視圖,一般是添加到 window 上,與屏幕寬高一致
- touchThrough: Bool
/// 點擊穿透 /// 設置爲 true 點擊空白地方,穿透點擊
- touchDismiss: Bool
/// 是否點擊空白地方自動消失 /// 設置爲 true 的時候,空白地方可以處理觸摸事件 /// 當 touchThrough 爲 true 的時候,此字段不起作用
- arrowDirection: UIPopoverArrowDirection
/// 箭頭方向 /// 當設置爲 .any 時,採用智能查找方向,設置一個比較合適的方向
- perferArrowDirection: [UIPopoverArrowDirection]
/// 指定箭頭方向查找順序 /// 如果 arrowDirection 設置爲 any(按照此順序查找合適的方向,如果查找不到,則按照最合適(智能)的方向) /// 相當於增加了方向的優先級
- minScreenEdg: CGFloat = 10
/// 到屏幕邊緣的最小距離,上圖片中的棕色區域
- contentInset: CGFloat
contentView 縮進,contentView 在底層箭頭視圖中四周的縮進。防止內容覆蓋圓角
- targetSize: CGSize
/// 需要展示內容的大小,此字段一定需要重新設置 /// 並且寬高大小加上 minScreenEdg contentInset 不能大於屏幕寬高
- contentView: UIView
/// 自定義內容請在此視圖上增加
- arrowView: KKPopoverArrowView
箭頭視圖
- delegate: KKPopoverDelegate
- func showInView(view: UIView?, sourceView: UIView)
/// 根據 sourceView 展示在指定視圖中 /// - Parameters: /// - view: 添加到的視圖,不傳則添加到 window 上 /// - sourceView: 視圖來源,箭頭指向的視圖
- func dismiss()
消失
- func getBestDirection(sourceRect: CGRect, size: CGSize) -> UIPopoverArrowDirection
/// sourceRect: sourceView 相對於 self (KKPopover) 的位置 /// size:self 的 size /// 當 arrowDirection 爲 .any 時,調用此方法獲最合適方向 /// 智能計算出最好的方向 /// 重寫此方法可以自定義最合適方向
- func isDirectionOk(sourceRect: CGRect, size: CGSize, direction: UIPopoverArrowDirection) -> Bool
/// sourceRect: sourceView 相對於 self (KKPopover) 的位置 /// size:self 的 size /// 確定指定方向是否合適 /// 當 arrowDirection 爲 .any 並且設置 perferArrowDirection 時,查找 perferArrowDirection 內部合適的方向使用
open class KKPopoverArrowView: UIView
箭頭視圖的配置項
/// 邊角弧度 open var borderRadius: CGFloat = 5; /// 線條寬度 open var lineWidth: CGFloat = 1 /// 填充顏色 open var fillColor: UIColor = UIColor.white /// 邊沿線條顏色 open var lineColor: UIColor = UIColor.white /// 箭頭方向 open var arrowDirection: UIPopoverArrowDirection = .any /// 箭頭頂尖半徑 open var arrowTipRadius: CGFloat = 0 /// 箭頭底部基座寬度 open var arrowBaseWidth: CGFloat = 17 /// 箭頭高度,從頂部中心點到基座距離 open var arrowHeight: CGFloat = 10 /// 箭頭邊緣到邊框最小的距離 open var minArrowEdg: CGFloat = 20 /// 線條交接處風格 open var lineJoin: CGLineJoin = CGLineJoin.round
待完善
- 增加支持設置背景圖片,以適應各種UI
- 增加自帶毛玻璃效果(可以取消)
- 增加可以自定義智能選擇方向的回調