Search code examples
swift3uiviewswift4uiswipegesturerecognizer

swipe left/right uiview without storyboard


I want to swipe right to show another view and swipe left to get back to first view, Without using storyboard like this example:

enter image description here

Could someone show me how to do that with swift 4


Solution

  • I would assume that if you were to use a swipe gesture, you would do something like:

    let swipeGesture = UISwipeGestureRecognizer()
    let scrollView = UIScrollView()
    
    swipeGesture.direction = .right
    swipeGesture.addTarget(self, action: #selector(scrollToBeginning))
    
    @objc func scrollToBeginning() {            
        scrollView.setContentOffset(.zero, animated: true)
    }
    

    Or using scrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)