簡體   English   中英

使用滑動手勢關閉視圖-Swift 4

[英]Dismissing A View with a swipe gesture - Swift 4

在我的應用程序中,當用戶在視圖上向下滑動時,我希望視圖散布到某個框架。 到目前為止,這是我嘗試過的操作,但是由於某種原因,它一直給我這個錯誤:

“ #selector”的參數未引用“ @objc”方法,屬性或初始化程序

這是我的代碼如下:

class VideoLauncher: NSObject {

@objc func dismissView(myView: UIView) {
    UIView.animate(withDuration: 0.4) {
        if let theWindow = UIApplication.shared.keyWindow {
        myView.frame = CGRect(x:theWindow.frame.width - 15 , y: theWindow.frame.height - 15, width: 10 , height: 10)
        }
    }
}

func showVideoPlayer() {

    print("showing player")
    if let keyWindow = UIApplication.shared.keyWindow {
        let view = UIView(frame: keyWindow.frame)
        view.backgroundColor = UIColor.spaceBlue
        view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)
        let videoPlayerView = VideoPlayerView(frame: keyWindow.frame)
        keyWindow.addSubview(view)
        view.addSubview(videoPlayerView)
        let slideDown = UISwipeGestureRecognizer(target: self, action: #selector(dismissView(myView: view)))
        view.addGestureRecognizer(slideDown)

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            view.frame = keyWindow.frame
        }, completion: { (completedAnimation) in
            UIApplication.shared.isStatusBarHidden = true
        })
    }
}

}

您必須像這樣使用:

let slideDown = UISwipeGestureRecognizer(target: self, action: #selector(dismissView(gesture:)))
slideDown.direction = .down
view.addGestureRecognizer(slideDown)

您的職能:

@objc func dismissView(gesture: UISwipeGestureRecognizer) {
    UIView.animate(withDuration: 0.4) {
        if let theWindow = UIApplication.shared.keyWindow {
            gesture.view?.frame = CGRect(x:theWindow.frame.width - 15 , y: theWindow.frame.height - 15, width: 10 , height: 10)
        }
    }
}

暫無
暫無

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

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