2016-08-11 27 views
-1

我有一個視圖控制器,有一個主視圖,裏面,我已經創建了一個小的下拉(向上彈出菜單)右側,這也是一個UIView。我添加了一個按鈕,當點擊時顯示和解除彈出菜單視圖。如何解開一個UIView,當它在界限外面被點擊時?

一切工作正常。

但是現在我希望它在用戶在這些界限之外觸發時消除視圖。 當前的代碼如下

var vw = UIView() 

func popup(){ 
    vw = UIView(frame: CGRectMake(self.view.frame.size.width - 210, 20, 260, 320)) 
    vw.backgroundColor = UIColor.whiteColor() 
    vw.layer.cornerRadius = 5.0 

    vw.layer.shadowColor = UIColor.blackColor().CGColor 
    vw.layer.shadowOpacity = 0.8 
    vw.layer.shadowRadius = 3.0 
    vw.layer.shadowOffset = CGSizeMake(2.0, 2.0) 
    let name: UILabel = UILabel(frame: CGRectMake(5, 0, 90, 30)) 
    name.text = testUserName 
    name.font = UIFont(name: "Abc Lt", size: 20.0) 
    let orgName: UILabel = UILabel(frame: CGRectMake(5, 35, 90, 30)) 
    orgName.text = "Abc Lt" 
    name.font = UIFont(name: "V Lt", size: 25.0) 
    let profileImageView: UIImageView = UIImageView(frame: CGRectMake(137, 4, 35, 35)) 
    profileImageView.image = UIImage(named: "profilepic.png") 
    let dropUpButton: UIButton = UIButton(frame: CGRectMake(profileImageView.frame.origin.x + 42, 18, 15, 10)) 
    // dropUpButton.imageView.image = [UIImage imageNamed:@"dropdown.png"]; 
    dropUpButton.setImage(UIImage(named: "dropup.png"), forState: .Normal) 
    dropUpButton.addTarget(self, action: #selector(LandingPageViewController.hideView), forControlEvents: .TouchUpInside) 
    vw.addSubview(name) 
    vw.addSubview(orgName) 
    vw.addSubview(profileImageView) 
    vw.addSubview(dropUpButton) 

    self.drawLine() 
    let tableView: UITableView = UITableView(frame: CGRectMake(0, 70, vw.frame.size.width, vw.frame.size.height-40), style: .Plain) 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    tableView.scrollEnabled = false 
    vw.addSubview(tableView) 

    self.navigationController!.view!.addSubview(vw) 
} 

//the function that displays and dismiss the pop up menu 

func hideView() { 
    vw.hidden = true 
} 

我看了一些帖子,其中一個可以使用點觸手勢尋找到它的挖掘,並使用if/else條件是可以解決基於這一點,但我不是當然。

你也可以檢查我已經上傳了更好的理解圖像:

want to dismiss this alert when touched outside its bounds

回答

0

你所能做的,只能是採取主上海華全尺寸相同的屏幕尺寸。給它明確的背景。採取與你現在正在做的相同的內容。添加一個與超級視圖大小相當的按鈕,點擊選擇器可以從超級視圖中刪除視圖。

還有其他方法可以通過識別觸摸位置來做到這一點。但它需要付出很多努力。

希望這可以幫助你。

+0

需要更清晰,有人可以在這裏扔一些代碼嗎?!請提出替代方法。 –

1
You can use tap gesture to do that 

add this code on main view: 

let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:))) 
     tap.delegate = self 
     self.view.addGestureRecognizer(tap) 

and then 

func handleTap(sender: UITapGestureRecognizer? = nil) 
    { 
     hideView() 
    } 
+0

謝謝,我試過但它墜毀。 ViewController沒有成員'handleTap', –

+0

使用這個:https://github.com/shemona-ios/DismissView – Shemona

+0

它只是一個空白的項目。 –

相關問題