Rotate the image corresponding to the touch drag in iPhone

前端 未结 3 1418
無奈伤痛
無奈伤痛 2020-12-31 15:49

I want to rotate the image in clockwise or anticlockwise direction corresponding to the user touch drag with its speed. I think this can be done with some math and logic. Wh

3条回答
  •  星月不相逢
    2020-12-31 16:14

    If you're targeting iOS 3.2 or greater, you can use UIRotationGestureRecognizer. The code would look something like this:

    In your setup method (viewDidLoad or init, etc,):

    UIRotationGestureRecognizer *rotationGesture = 
      [[UIRotationGestureRecognizer alloc] initWithTarget:self
                                                   action:@selector(handleRotate:)];
    rotationGesture.delegate = self;
    [myImageView addGestureRecognizer:rotationGesture];
    [rotationGesture release];
    

    The event handler:

    - (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
      if(recognizer.state == UIGestureRecognizerStateBegan || 
         recognizer.state == UIGestureRecognizerStateChanged)
      {
        recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, 
                                                            recognizer.rotation);
        [recognizer setRotation:0];
      }
    }
    

    I have some more examples of gesture recognizers (including the one above) on github.

提交回复
热议问题