簡體   English   中英

如何用輕擊手勢關閉SDCAlertView?

[英]How to dismiss SDCAlertView with tap gesture?

為了顯示警報,我使用SDCAlertView (UIAlertView的克隆)。 我想通過點擊屏幕上的UIActionSheet來消除警報。

UIAlertView不同, SDCAlertView作為視圖添加到視圖層次結構。 這意味着您可以簡單地將輕SDCAlertView手勢識別器添加到SDCAlertView視圖,該[SDCAlertView dismissWithClickedButtonIndex:animated:]視圖調用[SDCAlertView dismissWithClickedButtonIndex:animated:]

我找到了一種方法。 與iOS 7+兼容,但在8+上點擊即可關閉。

class SmartAlert
{
    static var backroundWindow: UIWindow!
    static var alertController: SDCAlertController!

    class func showAlertWithTitle(title: String!, message: String!, actionTitle: String)
    {
        if (iOS8)
        {
            self.backroundWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.backroundWindow.backgroundColor = UIColor.clearColor()
            self.backroundWindow.rootViewController = EmptyViewController()
            self.backroundWindow.windowLevel = UIWindowLevelAlert
            self.backroundWindow.makeKeyAndVisible()
        }

        self.alertController = SDCAlertController(title: title, message: message, preferredStyle: .Alert)
        self.alertController.addAction(SDCAlertAction(title: actionTitle, style: .Default, handler:
        {
            (_) -> Void in
            self.backroundWindow = nil
        }))

        self.alertController.presentWithCompletion(nil)
    }

    class func dismissAlert()
    {
        self.alertController.dismissWithCompletion
        {
            self.backroundWindow = nil
        }
    }
}

class EmptyViewController: UIViewController
{
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
    {
        SmartAlert.dismissAlert()
    }

    override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent)
    {
        if motion == UIEventSubtype.MotionShake
        {
            SmartAlert.dismissAlert()
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM