简体   繁体   中英

Dismissing A View with a swipe gesture - Swift 4

In my app, when a user swipes down on a view, I want the view to dismiss to a certain frame. This is what I have tried so far but for some reason it keeps giving me this error:

Argument of '#selector' does not refer to an '@objc' method, property, or initializer

This is my code down below:

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
        })
    }
}

}

You have to use like this :

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

Your function :

@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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM