Advertisement
Guest User

Untitled

a guest
Oct 19th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.05 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {
  4.  
  5.     @IBOutlet weak var scrollView: UIScrollView!
  6.    
  7.     override func viewDidLoad() {
  8.         super.viewDidLoad()
  9.         // Do any additional setup after loading the view, typically from a nib.
  10.         self.scrollView.delegate = self
  11.        
  12.         let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:)))
  13.         swipeUp.direction = UISwipeGestureRecognizerDirection.up
  14.         let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(gesture:)))
  15.         swipeDown.direction = UISwipeGestureRecognizerDirection.down
  16.        
  17.         self.view.addGestureRecognizer(swipeUp)
  18.         self.view.addGestureRecognizer(swipeDown)
  19.     }
  20.    
  21.     // here are those protocol methods with Swift syntax
  22.     public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  23.         return true
  24.     }
  25.    
  26.     public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  27.         return true
  28.     }
  29.    
  30.     // Debugging - All Swipes Are Detected Now
  31.     func respondToSwipeGesture(gesture: UIGestureRecognizer) {
  32.         if let swipeGesture = gesture as? UISwipeGestureRecognizer {
  33.             switch swipeGesture.direction {
  34.             case UISwipeGestureRecognizerDirection.down:
  35.                 print("Swiped down")
  36.             case UISwipeGestureRecognizerDirection.up:
  37.                 print("Swiped up")
  38.             default:
  39.                 break
  40.             }
  41.         }
  42.     }
  43.    
  44.     override func viewDidAppear(_ animated: Bool) {
  45.         scrollView.contentSize.height = 1000
  46.         self.view.layoutIfNeeded()
  47.     }
  48.  
  49.     override func didReceiveMemoryWarning() {
  50.         super.didReceiveMemoryWarning()
  51.         // Dispose of any resources that can be recreated.
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement