两个gestureRecognizer相互干扰?

时间:2015-06-09 20:36:21

标签: ios swift uiimageview uigesturerecognizer

我有UIImageView用户可以使用下面的handleAttachmentGesture功能拖动。我还有一个滑动检测器功能,当用户在一个方向上滑动某些东西时我想要println。代码一直有效,直到我输入使UIImage可拖动的代码。我认为这两个gestureRecognizers互相干扰,因为他们不会一起工作。有什么方法可以解决这个问题吗?

    class theGame: UIViewController {

        @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!

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

//DRAGGABLE IMAGE


        @IBAction func handleAttachmentGesture(sender: UIPanGestureRecognizer) {
            let location = sender.locationInView(self.view)
            let boxLocation = sender.locationInView(self.imageView)

            switch sender.state {
            case .Began:

                // 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:

                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

            }
        }

        func resetDemo() {
            animator.removeAllBehaviors()

            UIView.animateWithDuration(0.45) {
                self.imageView.bounds = self.originalBounds
                self.imageView.center = self.originalCenter
                self.imageView.transform = CGAffineTransformIdentity
            }
        }




//SWIPE DETECTOR

        func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {
            switch sender.direction {
            case UISwipeGestureRecognizerDirection.Left:
                if self.imageView.tag == 1 {
                    println("1 point!")
                } else {
                    if self.imageView.tag == 5 {
                        println("1 point!")
                    } else {
                        println("Game Over!")
                    }
                }
            case UISwipeGestureRecognizerDirection.Down:
                if self.imageView.tag == 2 {
                    println("1 point!")
                } else {
                    if self.imageView.tag == 8 {
                        println("1 point!")
                    } else {
                        println("Game Over!")
                    }
                }
            case UISwipeGestureRecognizerDirection.Right:
                if self.imageView.tag == 3 {
                    println("1 point!")
                } else {
                    if self.imageView.tag == 7 {
                        println("1 point!")
                    } else {
                        println("Game Over!")
                    }
                }
            case UISwipeGestureRecognizerDirection.Up:
                if self.imageView.tag == 4 {
                    println("1 point!")
                } else {
                    if self.imageView.tag == 6 {
                        println("1 point!")
                    } else {
                        println("Game Over!")
                    }
                }
            default:
                break
            }

        }


        override func viewDidLoad() {
            super.viewDidLoad()

            animator = UIDynamicAnimator(referenceView: view)
            originalBounds = imageView.bounds
            originalCenter = imageView.center

            var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
            swipeRight.direction = UISwipeGestureRecognizerDirection.Right
            self.view.addGestureRecognizer(swipeRight)
            self.view.userInteractionEnabled = true

            var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
            swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
            self.view.addGestureRecognizer(swipeLeft)
            self.view.userInteractionEnabled = true 

            var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
            swipeDown.direction = UISwipeGestureRecognizerDirection.Down
            self.view.addGestureRecognizer(swipeDown)
            self.view.userInteractionEnabled = true 

            var swipeUp = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
            swipeUp.direction = UISwipeGestureRecognizerDirection.Up
            self.view.addGestureRecognizer(swipeUp)
            self.view.userInteractionEnabled = true 

        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()

        }

    }

0 个答案:

没有答案