最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift如何实现点击弹出菜单之外的地方让弹出菜单消失

Swift crifan 3512浏览 0评论
1.搜:
swift click else where dismiss menu
swift click elsewhere dismiss menu
参考:
2.搜:
swift overlay view
参考:
代码:
        addDropdownView = UITableView(frame: addDropdownFrame)
        addDropdownView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        addDropdownView.backgroundColor = UIColor.yellowColor()

        //add border
        addDropdownView.layer.masksToBounds = true
        addDropdownView.layer.borderColor = UIColor.lightGrayColor().CGColor
        addDropdownView.layer.borderWidth = 1.0

        super.init(frame:addDropdownFrame)

        addDropdownView.delegate = self
        addDropdownView.dataSource = self

        //self.addSubview(addDropdownView)
        //self.bringSubviewToFront(addDropdownView)

        // click elsewhere dismiss add dropdownlist
        overlayView = UIView(frame: UIScreen.mainScreen().bounds)
        overlayView.backgroundColor = UIColor.yellowColor()
        overlayView.alpha = 0.6

        addDropdownView.delegate = self
        let tap = UITapGestureRecognizer(target: self, action: Selector("addDropdownListViewShouldDismiss"))
        tap.numberOfTapsRequired = 1
        overlayView.addGestureRecognizer(tap)
        overlayView.addSubview(addDropdownView)

        self.addSubview(overlayView)

    func addDropdownListViewShouldDismiss() {
        print("addDropdownListViewShouldDismiss: Overlay Tapped")
        self.hidden = true
    }
结果不工作:
click out side not hide menu
点击黄色区域,还是没有让下拉菜单消失
感觉是delegate的问题。
搜:
swift uitapgesturerecognizer not working
参考:
添加:
        overlayView.userInteractionEnabled = true
结果:
好像起效果了,addDropdownListViewShouldDismiss调用到了:
 delegate dismiss called
但是好像我点击了2,3次,才调用到。。。
并且点击其他区域也还是进去对应字页面,没有响应到此处的tap。
加了tap.delegate和UIGestureRecognizerDelegate:
结果效果依旧:
虽然部分区域点击后,可以响应tap,但是还是需要点击三次左右才可以响应tap。
然后父view中把此view放到最前面:
        self.view.addSubview(addDropdownListView)
        self.view.bringSubviewToFront(addDropdownListView)
问题依旧:
还是不能实现点击一次,就响应tap。
[解决过程]
1.搜:

swift multiple click response tap
参考:
后来:
把整个tap的代码,移动到主界面中:
就正常工作了:
单击一次,即可响应对应tap的函数。
swift  subview tap not capture whole screen
swift  subview tap full screen
6.swift UITapGestureRecognizer pass to subview
import UIKit

class MainViewController: UITabBarController, UIGestureRecognizerDelegate {
    var titleLabel:UILabel!

    var addDropdownListView:AddDropdownListView!

    let mainTabs = ["消息", "通讯录", "文件", "收藏", "我"]

    let dropdownListTitleArr = ["发起会话", "发起话题", "添加同事", "添加人脉"]

    //var overlayView:UIView!
    var tapRecognizer:UITapGestureRecognizer!
    var searchButton:UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        NSLog("MainViewController viewDidLoad()")

        self.view.backgroundColor = UIColor.whiteColor()

        let statusView = UIView()
        statusView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), STATUS_BAR_HEIGHT)
        //statusView.backgroundColor = UIColor(hexString: "#ff3333")
        statusView.backgroundColor = COLOR_MAIN
        self.view.addSubview(statusView)

        let tabbgView = UIView()
        tabbgView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), NAVI_BAR_HEIGHT)
        tabbgView.backgroundColor = COLOR_MAIN
        //tabbgView.backgroundColor = UIColor.blackColor()
        self.view.addSubview(tabbgView)

        let addMenu = UIImage(named:"add_white_24x24.png")
        let addButton = UIButton()
        addButton.frame = CGRectMake(self.view.bounds.width - 54, 0, 44.0, NAVI_BAR_HEIGHT)
        //addButton.addTarget(self, action:"toggleAddDropdownList:", forControlEvents:UIControlEvents.TouchUpInside)
        addButton.addTarget(self, action:Selector("toggleAddDropdownList"), forControlEvents:UIControlEvents.TouchUpInside)
        addButton.setImage(addMenu,forState:UIControlState.Normal)
        tabbgView.addSubview(addButton)


        let searchImage = UIImage(named:"search_24x24.png")
        let searchFrame = CGRectMake(
            addButton.frame.origin.x - 44,
            0,
            44.0,
            NAVI_BAR_HEIGHT)
        searchButton = UIButton()
        searchButton.frame = searchFrame
        searchButton.addTarget(self, action:"jumpToSearch:", forControlEvents:UIControlEvents.TouchUpInside)
        searchButton.setImage(searchImage,forState:UIControlState.Normal)
        tabbgView.addSubview(searchButton)



        let addDropdownWidth:CGFloat = 200
        let addDropdownFrame = CGRectMake(
            self.view.bounds.width - addDropdownWidth,
            STATUS_BAR_HEIGHT + NAVI_BAR_HEIGHT,
            addDropdownWidth,
            ADD_DROPDOWN_LIST_CELL_HEIGHT * 4)
        print("addDropdownFrame=\(addDropdownFrame)")

        addDropdownListView = AddDropdownListView(frame:addDropdownFrame)
        addDropdownListView.hidden = true
        self.view.addSubview(addDropdownListView)
        self.view.bringSubviewToFront(addDropdownListView)

        tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("toggleAddDropdownList"))
        tapRecognizer.numberOfTapsRequired = 1
        tapRecognizer.delegate = self
    }

    //toggle add drop down list
    func toggleAddDropdownList(){
        addDropdownListView.hidden = !addDropdownListView.hidden

        if !addDropdownListView.hidden {
            self.view.addGestureRecognizer(tapRecognizer)
            searchButton.enabled = false
        }else {
            self.view.removeGestureRecognizer(tapRecognizer)
            searchButton.enabled = true
        }

        print("addDropdownListView.hidden=\(addDropdownListView.hidden)")
    }


    //jump to search view
    func jumpToSearch(button : UIButton){
        print("realy called jumpToSearch")
    }


    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

        print("touch=\(touch)")
        print("touch.view=\(touch.view)")

        //get touch/tap point/position in current (main) view
        let touchPoint = touch.locationInView(self.view)
        print("touchPoint=\(touchPoint)")

        //check tap point in subview or not
        if CGRectContainsPoint(addDropdownListView.frame, touchPoint){
            // not intercept touch -> forward to subview: addDropdownListView
            return false
        }
        else
        {
            // intercept touch -> forward to toggleAddDropdownList
            return true
        }
    }

}
实现了效果了:
点击其它区域后,当前弹出的,子视图,一个tableview,即可消失。
而点击到属于tableview视图范围内时,则可以正常传递touch给didSelectRowAtIndexPath

转载请注明:在路上 » [已解决]swift如何实现点击弹出菜单之外的地方让弹出菜单消失

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.205 seconds, using 22.09MB memory