在UITableView Swift中点击单元格时使用UIView创建弹出窗口



>我有一个表格视图,如果在表格视图上点击了一个单元格,我想创建一个 UIView 以在表格视图上弹出并在表格视图单元格中显示内容。我想在tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)中添加代码,以便在选择一行时打开 UIView。我只需要知道如何使用 UIView 创建弹出窗口。我找不到任何帮助我解决这个问题的来源。关于如何做到这一点的一些解释会很棒。提前感谢!

步骤-1

创建一个UIView将框架设置为View.bounds同时添加隐藏Tap gesture功能。

步骤-2

要在单击时显示视图,请使用

UIView.animateWithDuration(1.0, animations: {
    yourView.alpha = 1.0
})

步骤-3

隐藏

 UIView.animateWithDuration(1.0, animations: {
    yourView.alpha = 0.0
})

最后,如果要隐藏,请使用hiddenremovefromsuperview

如果您使用的是IOS 8或更高版本,则可以在新的UIViewController中创建UIView,其设置属性为ModalPresentationStyle,值为UIModalPresentationStyle。像这样的东西在你做了选择RowAtIndexPath:

var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(500,600)
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)

先添加let vw = UIView(),然后再试试这个

    vw.frame = CGRectMake(150, 150, 0, 0)
    vw.backgroundColor = UIColor.redColor()
    self.view.addSubview(vw)
    UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
        self.vw.frame = CGRectMake(75, 75, 300, 300)
    }, completion: nil)

首先创建一个 UIView,并设置其 X 和 Y 点 (150),然后设置宽度和高度 (300)。我只是简单地添加一个背景颜色来表明它确实在那里。然后animatedWithDuration部分是重置框架,使其看起来像弹出。

如果您希望后面的视图变暗,请添加此按钮。

view.backgroundColor = UIColor(white: 1, alpha: 0.5)

至于触摸背景视图以关闭弹出视图。您需要一个点击手势识别器,并为其提供功能

    var tap = UITapGestureRecognizer()
    tap.delegate = self
    tap.addTarget(self, action: "tapped")
    self.view.addGestureRecognizer(tap)

然后对于它的功能

    func tapped(){
       vw.removeFromSuperview()
    }