菜單類型
1、首頁(yè)點(diǎn)擊更多按鈕彈起的菜單:FunctionView拣展。
2拜银、詳情頁(yè)點(diǎn)擊更多按鈕彈出的菜單:YYWMenuView。
3阴汇、長(zhǎng)按webview中的鏈接彈出的菜單:YYWContextMenuHelp.swift数冬。
具體實(shí)現(xiàn)
-
FunctionView
界面由nib搭建。view加在mainviewcontroller的view中搀庶。
通過(guò)以下代碼來(lái)隱藏和顯示拐纱,有動(dòng)畫效果。
部分按鈕在首頁(yè)時(shí)置灰不可點(diǎn)擊哥倔。
該菜單的具體功能實(shí)現(xiàn)全由PageViewManager代理實(shí)現(xiàn)秸架。
func showFunctionBar(_ sender: UIButton)
{
guard self.functionView.isAnimatting == false else {
return
}
self.mainViewController().view.bringSubview(toFront: self.functionView)
let show = self.functionView.contentViewBottomAL.constant < 0
self.functionView.isHidden = false
self.functionView.isAnimatting = true
if show
{
self.functionView.showAnimation()
MobClick.event("homeToolBar_showMenu")
let enabled = MainViewFacade.sharedInstance.isNotHomeWebPage()
self.setUpButtonEnabled(functionView.addBookmarkButton, label: functionView.addBookmarkLabel, enabled: enabled)
self.setUpButtonEnabled(functionView.shareBtn, label: functionView.shareLabel, enabled: enabled)
self.setUpButtonEnabled(functionView.webSnapShotButton, label: functionView.webSnapShotLabel, enabled: enabled)
self.setUpButtonEnabled(functionView.refreshBtn, label: functionView.refreshLabel, enabled: enabled)
}
else
{
self.functionView.hideAnimation()
}
self.functionView.contentView.layoutIfNeeded()
}
- ** YYWMenuView**
該菜單是添加在window上的,因此全局調(diào)用一次show方法即可顯示出來(lái)咆蒿。
該枚舉代表顯示的位置东抹,不同類型顯示不同的菜單項(xiàng)。
enum contentType: Int {
case home = 0//
case web
case card
}
switch ctype {
case .home:
self.menuItems = [
YYWMenuItem(defaultType: .accountStatus),
YYWMenuItem(defaultType: .bookmarkHistiry),
YYWMenuItem(defaultType: .setting)
]
break
case .web:
self.menuItems = [
YYWMenuItem(defaultType: .accountStatus),
YYWMenuItem(defaultType: .setting),
YYWMenuItem(defaultType: .capture),
YYWMenuItem(defaultType: .copyLink),
YYWMenuItem(defaultType: .bookmarkHistiry),
YYWMenuItem(defaultType: .addBookmark)
]
case .card:
self.menuItems = [
YYWMenuItem(defaultType: .setFont),
YYWMenuItem(defaultType: .feedback)
]
break
}
各菜單項(xiàng)對(duì)應(yīng)的title與image已經(jīng)預(yù)先定義好沃测。
init(defaultType: YYWMenuItemType) {
type = defaultType
switch type {
case .accountStatus:
title = NSLocalizedString("未登錄", comment: "")
image = UIImage(named: "menu_visitor")!
case .setting:
title = NSLocalizedString("設(shè)置", comment: "")
image = UIImage(named: "menu_setting")!
case .darkModel:
title = NSLocalizedString("夜間模式", comment: "")
image = UIImage(named: "menu_night")!
case .capture:
title = NSLocalizedString("截圖", comment: "")
image = UIImage(named: "menu_screenshot")!
case .copyLink:
title = NSLocalizedString("復(fù)制鏈接", comment: "")
image = UIImage(named: "menu_copy")!
case .cloudOof:
title = NSLocalizedString("收藏到115", comment: "")
image = UIImage(named: "menu_star")!
case .bookmarkHistiry:
title = NSLocalizedString("書簽/歷史", comment: "")
image = UIImage(named: "menu_sc")!
case .addBookmark:
title = NSLocalizedString("添加書簽", comment: "")
image = UIImage(named: "menu_bookmark")!
case .setFont:
title = NSLocalizedString("字體設(shè)置", comment: "")
image = UIImage(named: "menu_font")!
case .feedback:
title = NSLocalizedString("意見(jiàn)反饋", comment: "")
image = UIImage(named: "menu_edit")!
case .itemType:
assert(true, "itemType 不是默認(rèn)類型, 使用上面那個(gè)方法初始化缭黔。")
title = ""
image = UIImage()
}
super.init()
}
因此初始化時(shí)指定type,顯示方向type蒂破,以及一個(gè)block馏谨。
init(archorView: UIView, contentType ctype: contentType, directionType dtype: directionType, action: @escaping YYWMenuAction)
// block定義 回調(diào)時(shí)返回點(diǎn)擊的type項(xiàng),根據(jù)該type處理事件附迷。
typealias YYWMenuAction = (YYWMenuItemType) -> Void
-
YYWContextMenuHelp
該彈層使用系統(tǒng)自帶的UIAlertController實(shí)現(xiàn)惧互。
根據(jù)以下方法獲取的webview元素決定顯示哪些彈層哎媚。
userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
YYWContextMenuHelp.Elements(link: linkURL, image: imageURL, imageUrls: self.allImagesUrl, linkText: linkText)
創(chuàng)建代碼
func contextMenuHelper(_ elements: Elements, cookie:String?, gestureRecognizer: UILongPressGestureRecognizer) {
// locationInView can return (0, 0) when the long press is triggered in an invalid page
// state (e.g., long pressing a link before the document changes, then releasing after a
// different page loads).
guard let navigationController = UIApplication.shared.delegate?.window??.rootViewController as? UINavigationController else { return }
guard let mainViewController = navigationController.viewControllers.first as? MainViewController else { return }
let view = MainViewFacade.sharedInstance.currentPageView()
let touchPoint = view.tabWebView!.gestureRecognizer.location(in: view)
guard touchPoint != CGPoint.zero else { return }
let touchSize = CGSize(width: 0, height: 16)
let actionSheetController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
var dialogTitle: String?
if let url = elements.link, let imageUrl = elements.image {//長(zhǎng)按鏈接與圖片
dialogTitle = url.absoluteString
if dialogTitle?.hasPrefix("javascript:") == true {
return
}
if dialogTitle?.characters.count > 0{
if !view.isPushPageView {
actionSheetController.addAction(openNewTabAction(url, mainViewController))
}
actionSheetController.addAction(copyAction(url))
actionSheetController.addAction(shareAction(url.absoluteString,elements.linkText ?? ""))
actionSheetController.addAction(saveImageAction(imageUrl, cookie ?? ""))
actionSheetController.addAction(shareImageAction(imageUrl, cookie ?? ""))
actionSheetController.addAction(browserImageAction(imageUrl, elements, cookie ?? "", mainViewController))
// if let _ = UserDefaults.standard.object(forKey: "defaults_user_authdata"), !imageUrl.absoluteString.hasPrefix("data:image/") {
// actionSheetController.addAction(cloudImageAction(imageUrl))
// }
}
} else if let url = elements.link {///長(zhǎng)按鏈接
dialogTitle = url.absoluteString
if dialogTitle?.hasPrefix("javascript:") == true {
return
}
if dialogTitle?.characters.count > 0{
// actionSheetController.addAction(openNewTabAction(url, mainViewController))
if !view.isPushPageView {
if (yywFunctionMatch != "" && url.absoluteString.hasPrefix(yywFunctionMatch) == true)
{
var originalString = url.absoluteString
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
originalString = "http://115.com/lx?mag="
originalString = originalString + (escapedString ?? "")
if let strUrl = URL(string: originalString){
actionSheetController.addAction(openNewTabAction(strUrl, mainViewController))
}
} else {
actionSheetController.addAction(openNewTabAction(url, mainViewController))
}
}
actionSheetController.addAction(copyAction(url))
actionSheetController.addAction(copyLinkTextAction(elements.linkText ?? ""))
actionSheetController.addAction(shareAction(url.absoluteString,elements.linkText ?? ""))
}
} else if let imageUrl = elements.image {//長(zhǎng)按圖片
if dialogTitle == nil {
dialogTitle = imageUrl.absoluteString
}
dialogTitle = ""
actionSheetController.addAction(browserImageAction(imageUrl, elements, cookie ?? "", mainViewController))
actionSheetController.addAction(saveImageAction(imageUrl, cookie ?? ""))
actionSheetController.addAction(shareImageAction(imageUrl, cookie ?? ""))
// if let _ = UserDefaults.standard.object(forKey: "defaults_user_authdata"), !imageUrl.absoluteString.hasPrefix("data:image/") {
// actionSheetController.addAction(cloudImageAction(imageUrl))
// }
}
// If we're showing an arrow popup, set the anchor to the long press location.
if let popoverPresentationController = actionSheetController.popoverPresentationController {
popoverPresentationController.delegate = self
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = CGRect(origin: touchPoint, size: touchSize)
popoverPresentationController.permittedArrowDirections = .any
}
if dialogTitle != nil && dialogTitle?.characters.count > 0 {
actionSheetController.title = dialogTitle?.ellipsize(maxLength: 120)
}
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil).changeAlertActionTextColor()
actionSheetController.addAction(cancelAction)
if let navVc = mainViewController.presentedViewController as? UINavigationController {
navVc.present(actionSheetController, animated: true, completion: nil)
} else {
mainViewController.present(actionSheetController, animated: true, completion: nil)
}
}
旋轉(zhuǎn)屏幕時(shí)會(huì)將彈出收起。模仿UC做法壹哺。