手势识别的简单使用

UISwipeGestureRecognizer


import UIKit

class ViewController: UIViewController {
  
  var swipeRecognizer: UISwipeGestureRecognizer!
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    swipeRecognizer = UISwipeGestureRecognizer(target: self,
      action: "handleSwipes:")
  }
  
  func handleSwipes(sender: UISwipeGestureRecognizer){
    
    if sender.direction  == .Down{
      print("Swiped Down")
    }
    if sender.direction == .Left{
      print("Swiped Left")
    }
    if sender.direction == .Right{
      print("Swiped Right")
    }
    if sender.direction == .Up{
      print("Swiped Up")
    }
    
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    /* Swipes that are performed from right to
    left are to be detected */
    swipeRecognizer.direction = .Left
    
    /* Just one finger needed */
    swipeRecognizer.numberOfTouchesRequired = 1
    
    /* Add it to the view */
    view.addGestureRecognizer(swipeRecognizer)
    
  }

}

当从右向左滑动时,打印 “Swiped Left”



UIRotationGestureRecognizer


import UIKit

class ViewController: UIViewController {
  
  var helloWorldLabel: UILabel!
  var rotationRecognizer: UIRotationGestureRecognizer!
  var rotationAngleInRadians = 0.0 as CGFloat
  
  func handleRotations(sender: UIRotationGestureRecognizer){
    
    /* Take the previous rotation and add the current rotation to it */
    helloWorldLabel.transform =
      CGAffineTransformMakeRotation(rotationAngleInRadians +
        sender.rotation)
    
    /* At the end of the rotation, keep the angle for later use */
    if sender.state == .Ended{
      rotationAngleInRadians += sender.rotation
    }
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    helloWorldLabel = UILabel(frame: CGRectZero)
    rotationRecognizer = UIRotationGestureRecognizer(target: self,
      action: "handleRotations:")
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    helloWorldLabel.text = "Hello, World!"
    helloWorldLabel.font = UIFont.systemFontOfSize(16)
    helloWorldLabel.sizeToFit()
    helloWorldLabel.center = view.center
    view.addSubview(helloWorldLabel)
    
    view.addGestureRecognizer(rotationRecognizer)
    
  }
  
}

借助UIRotationGestureRecognizer实现label的旋转。



UIPanGestureRecognizer


import UIKit

class ViewController: UIViewController {
  
  var helloWorldLabel: UILabel!
  var panGestureRecognizer: UIPanGestureRecognizer!
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    let labelFrame = CGRect(x: 0, y: 0, width: 150, height: 100)
    helloWorldLabel = UILabel(frame: labelFrame)
    /* Make sure to enable user interaction; otherwise, tap events
    won't be caught on this label */
    helloWorldLabel.userInteractionEnabled = true
    helloWorldLabel.text = "Hello World"
    helloWorldLabel.frame = labelFrame
    helloWorldLabel.backgroundColor = UIColor.blackColor()
    helloWorldLabel.textColor = UIColor.whiteColor()
    helloWorldLabel.textAlignment = .Center
    panGestureRecognizer = UIPanGestureRecognizer(target: self,
      action: "handlePanGestures:")
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    /* Now make sure this label gets displayed on our view */
    view.addSubview(helloWorldLabel)
    
    /* At least and at most we need only one finger to activate
    the pan gesture recognizer */
    panGestureRecognizer.minimumNumberOfTouches = 1
    panGestureRecognizer.maximumNumberOfTouches = 1
    
    /* Add it to our view */
    helloWorldLabel.addGestureRecognizer(panGestureRecognizer)
    
  }
  
  func handlePanGestures(sender: UIPanGestureRecognizer){
    
    if sender.state != .Ended && sender.state != .Failed{
      let location = sender.locationInView(sender.view!.superview!)
      sender.view!.center = location
    }
    
  }
  
  
}


借助UIPanGestureRecognizer,实现拖动标签改变它的位置。



UILongPressGestureRecognizer


import UIKit

class ViewController: UIViewController {
  
  var longPressGestureRecognizer: UILongPressGestureRecognizer!
  var dummyButton: UIButton!
  
  func handleLongPressGestures(sender: UILongPressGestureRecognizer){
    
    /* Here we want to find the mid point of the two fingers
    that caused the long press gesture to be recognized. We configured
    this number using the numberOfTouchesRequired property of the
    UILongPressGestureRecognizer that we instantiated before. If we
    find that another long press gesture recognizer is using this
    method as its target, we will ignore it */
    
    if sender.numberOfTouches() == 2{
      
      let touchPoint1 = sender.locationOfTouch(0, inView: sender.view)
      let touchPoint2 = sender.locationOfTouch(1, inView: sender.view)
      
      let midPointX = (touchPoint1.x + touchPoint2.x) / 2.0
      let midPointY = (touchPoint1.y + touchPoint2.y) / 2.0
      
      let midPoint = CGPoint(x: midPointX, y: midPointY)
      
      dummyButton.center = midPoint
      
    } else {
      /* This is a long press gesture recognizer with more
      or less than 2 fingers */
      let controller = UIAlertController(title: "Two fingers",
        message: "Please use two fingers",
        preferredStyle: .Alert)
      controller.addAction(UIAlertAction(title: "OK",
        style: .Default,
        handler: nil))
      presentViewController(controller, animated: true, completion: nil)
    }
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    dummyButton = UIButton(type:.System)
    dummyButton.frame = CGRect(x: 0, y: 0, width: 72, height: 37)
    dummyButton.setTitle("My Button", forState: .Normal)
    
    /* First create the gesture recognizer */
    longPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
      action: "handleLongPressGestures:")
    
    /* The number of fingers that must be present on the screen */
    longPressGestureRecognizer.numberOfTouchesRequired = 2
    
    /* Maximum 100 points of movement allowed before the gesture
    is recognized */
    longPressGestureRecognizer.allowableMovement = 100
    
    /* The user must press 2 fingers (numberOfTouchesRequired) for
    at least 1 second for the gesture to be recognized */
    longPressGestureRecognizer.minimumPressDuration = 1
    
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    dummyButton.center = view.center
    view.addSubview(dummyButton)
    
    /* Add this gesture recognizer to our view */
    view.addGestureRecognizer(longPressGestureRecognizer)
    
  }
  
}

实现功能:两个手指长按一秒以上,移动按钮到两个手指的中间位置。



UITapGestureRecognizer


import UIKit

class ViewController: UIViewController {
  
  var tapGestureRecognizer: UITapGestureRecognizer!
  
  func handleTaps(sender: UITapGestureRecognizer){
    
    for touchCounter in 0..

实现:两个手指,连按三次后打印两个手指的坐标。



UIPinchGestureRecognizer


import UIKit

class ViewController: UIViewController {
  
  var myBlackLabel: UILabel!
  var pinchGestureRecognizer: UIPinchGestureRecognizer!
  var currentScale = 0.0 as CGFloat
  
  func handlePinches(sender: UIPinchGestureRecognizer){
    
    if sender.state == .Ended{
      currentScale = sender.scale
    } else if sender.state == .Began && currentScale != 0.0{
      sender.scale = currentScale
    }
    
    if sender.scale != CGFloat.NaN && sender.scale != 0.0{
      sender.view!.transform = CGAffineTransformMakeScale(sender.scale,
        sender.scale);
    }
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    let labelRect = CGRect(x: 0, y: 0, width: 200, height: 200)
    myBlackLabel = UILabel(frame: labelRect)
    myBlackLabel.backgroundColor = UIColor.blackColor()
    /* Without this line, our pinch gesture recognizer
    will not work */
    myBlackLabel.userInteractionEnabled = true
    
    /* Create the Pinch Gesture Recognizer */
    pinchGestureRecognizer =  UIPinchGestureRecognizer(target: self,
      action: "handlePinches:")
    
    /* Add this gesture recognizer to our view */
    myBlackLabel.addGestureRecognizer(pinchGestureRecognizer)
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    myBlackLabel.center = view.center
    view.addSubview(myBlackLabel)
    
  }
  
}

借助UIPinchGestureRecognizer实现label的缩放。



UIScreenEdgePanGestureRecognizer


import UIKit
import Twitter

class ViewController: UIViewController {
  
  var screenEdgeRecognizer: UIScreenEdgePanGestureRecognizer!
  
  /* Just a little method to help us display alert dialogs to the user */
  func displayAlertWithTitle(title: String, message: String){
    let controller = UIAlertController(title: title,
      message: message,
      preferredStyle: .Alert)
    
    controller.addAction(UIAlertAction(title: "OK",
      style: .Default,
      handler: nil))
    
    presentViewController(controller, animated: true, completion: nil)
    
  }

  func handleScreenEdgePan(sender: UIScreenEdgePanGestureRecognizer){
    
    if sender.state == .Ended{
      displayAlertWithTitle("Detected",
        message: "Edge swipe was detected")
    }
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    /* Create the Pinch Gesture Recognizer */
    screenEdgeRecognizer =  UIScreenEdgePanGestureRecognizer(target: self,
      action: "handleScreenEdgePan:")
    
    /* Detect pans from left edge to the inside of the view */
    screenEdgeRecognizer.edges = .Left
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.addGestureRecognizer(screenEdgeRecognizer)
  }
  
  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    displayAlertWithTitle("Instructions",
      message: "Start swiping from the left edge of the screen " +
      "to the right, please!")
  }
  
}


当从左边缘拖动到右边时触发事件。




你可能感兴趣的:(【iOS开发记录】)