如何检测用户刷卡的方式?

时间:2015-06-11 16:20:00

标签: swift uiimageview uigesturerecognizer

我使用下面的代码,以便用户可以将UIImageView滑动到他们想要的任何方向。我需要检测用户正在滑动的方式。如果它向左滑动,则需要println"向左滑动!"有没有办法做到这一点?

  let ThrowingThreshold: CGFloat = 1000
  let ThrowingVelocityPadding: CGFloat = 35

  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var redSquare: UIView!
  @IBOutlet weak var blueSquare: UIView!

  private var originalBounds = CGRect.zeroRect
  private var originalCenter = CGPoint.zeroPoint

  private var animator: UIDynamicAnimator!
  private var attachmentBehavior: UIAttachmentBehavior!
  private var pushBehavior: UIPushBehavior!
  private var itemBehavior: UIDynamicItemBehavior!

    func resetDemo() {
        animator.removeAllBehaviors()

        UIView.animateWithDuration(0.45) {
            self.imageView.bounds = self.originalBounds
            self.imageView.center = self.originalCenter
            self.imageView.transform = CGAffineTransformIdentity
        }
    }
@IBAction func handleAttachmentGesture(sender: UIPanGestureRecognizer) {
    let location = sender.locationInView(self.view)
    let boxLocation = sender.locationInView(self.imageView)

    switch sender.state {
    case .Began:
      println("Your touch start position is \(location)")
      println("Start location in image is \(boxLocation)")

      // 1
      animator.removeAllBehaviors()

      // 2
      let centerOffset = UIOffset(horizontal: boxLocation.x - imageView.bounds.midX,
        vertical: boxLocation.y - imageView.bounds.midY)
      attachmentBehavior = UIAttachmentBehavior(item: imageView,
        offsetFromCenter: centerOffset, attachedToAnchor: location)

      // 3
      redSquare.center = attachmentBehavior.anchorPoint
      blueSquare.center = location

      // 4
      animator.addBehavior(attachmentBehavior)

    case .Ended:
      println("Your touch end position is \(location)")
      println("End location in image is \(boxLocation)")

      animator.removeAllBehaviors()

      // 1
      let velocity = sender.velocityInView(view)
      let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))

      if magnitude > ThrowingThreshold {
        // 2
        let pushBehavior = UIPushBehavior(items: [imageView], mode: .Instantaneous)
        pushBehavior.pushDirection = CGVector(dx: velocity.x / 10, dy: velocity.y / 10)
        pushBehavior.magnitude = magnitude / ThrowingVelocityPadding

        self.pushBehavior = pushBehavior
        animator.addBehavior(pushBehavior)

        // 3
        let angle = Int(arc4random_uniform(20)) - 10

        itemBehavior = UIDynamicItemBehavior(items: [imageView])
        itemBehavior.friction = 0.2
        itemBehavior.allowsRotation = true
        itemBehavior.addAngularVelocity(CGFloat(angle), forItem: imageView)
        animator.addBehavior(itemBehavior)

        // 4
        let timeOffset = Int64(0.4 * Double(NSEC_PER_SEC))
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeOffset), dispatch_get_main_queue()) {
            self.resetDemo()
        }
      } else {
        resetDemo()
        }

        default:
            attachmentBehavior.anchorPoint = sender.locationInView(view)
            redSquare.center = attachmentBehavior.anchorPoint
        }

override func viewDidLoad() {
    super.viewDidLoad()
    animator = UIDynamicAnimator(referenceView: view)
    originalBounds = imageView.bounds
    originalCenter = imageView.center

1 个答案:

答案 0 :(得分:0)

你可以使用手势的translationInView功能来获得一个CGPoint表示手势自开始以来(或自上次重置后)的移动。这会告诉您xy方向的相对移动。

如果您对整体动作感兴趣,那么您只需阅读此内容并使用xy值的符号来确定移动的方向。

如果您对瞬时移动方向感兴趣,那么您可以使用相同的方法,但在阅读翻译后,您可以使用setTranslation:inView:重置为CGPointZero。通过这种方式,下一个回调中提供的翻译是增量移动。