Question

As the title says, how can I make a rotation gesture only apply once? for example, I want a square to rotate 90 degrees after a rotation gesture is regonized. This is the code I'm currently using. It almost works because sometimes it rotates twice or more when it should apply the rotation once. What is wrong with my code or how can I fix this?

if (rotationGestureRegonizer.rotation.toDegree >= 30 && !neveragain) {
            rot += Float(M_PI/2.5)
        redArrow.position = CGPointMake(shapeLayer.position.x+CGFloat(sinf(rot)*70),shapeLayer.position.y+CGFloat(cosf(rot)*70))

        var transform = CATransform3DIdentity;
        transform = CATransform3DRotate(shapeLayer.transform, CGFloat(M_PI/2.5), 0.0, 0.0, 1.0);
            var transform2 = CATransform3DIdentity;
            transform2 = CATransform3DRotate(redArrow.transform, CGFloat(M_PI/2.5), 0.0, 0.0, -1.0);
        shapeLayer.transform = transform
            redArrow.transform = transform2
            neveragain = true

        }

 override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
        println("touch up")
        neveragain = false
    }
Was it helpful?

Solution

In the gesture callback function, run your code when gestureRecognizer.state == UIGestureRecognizerStateBegan is true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top