2

我正在考虑是否可以使用平移手势来旋转文本字段以进行更平滑的旋转。你能帮帮我吗?拜托。

更新:

    #import <UIKit/UIKit.h>

    KTOneFingerRotationGestureRecognizer.h


    @interface KTOneFingerRotationGestureRecognizer : UIGestureRecognizer 
    {

    }

    /**
     The rotation of the gesture in radians since its last change.
     */
    @property (nonatomic, assign) CGFloat rotation;

    @end



    KTOneFingerRotationGestureRecognizer.m

    #import "KTOneFingerRotationGestureRecognizer.h"
    #import <UIKit/UIGestureRecognizerSubclass.h>


    @implementation KTOneFingerRotationGestureRecognizer

    @synthesize rotation = rotation_;

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
       // Fail when more than 1 finger detected.
       if ([[event touchesForGestureRecognizer:self] count] > 1) {
          [self setState:UIGestureRecognizerStateFailed];
       }
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
       if ([self state] == UIGestureRecognizerStatePossible) {
          [self setState:UIGestureRecognizerStateBegan];
       } else {
          [self setState:UIGestureRecognizerStateChanged];
       }

       // We can look at any touch object since we know we 
       // have only 1. If there were more than 1 then 
       // touchesBegan:withEvent: would have failed the recognizer.
       UITouch *touch = [touches anyObject];

       // To rotate with one finger, we simulate a second finger.
       // The second figure is on the opposite side of the virtual
       // circle that represents the rotation gesture.

       UIView *view = [self view];
       CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
       CGPoint currentTouchPoint = [touch locationInView:view];
       CGPoint previousTouchPoint = [touch previousLocationInView:view];

       CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

       [self setRotation:angleInRadians];
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
       // Perform final check to make sure a tap was not misinterpreted.
       if ([self state] == UIGestureRecognizerStateChanged) {
          [self setState:UIGestureRecognizerStateEnded];
       } else {
          [self setState:UIGestureRecognizerStateFailed];
       }
    }

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
       [self setState:UIGestureRecognizerStateFailed];
    }

在我的视图控制器上

   - (IBAction)handleRotate:(KTOneFingerRotationGestureRecognizer *)recognizer {    
    NSLog(@"Rotation");

    choosePhotoView = [recognizer view];
    [choosePhotoView setTransform:CGAffineTransformRotate([choosePhotoView transform], [recognizer rotation])];
    [choosePhotoView setTransform:CGAffineTransformMakeRotation(0)];

}
4

1 回答 1

1

您可以考虑UITextField作为UIView应用合适的transform. 你会从网上得到很多示例代码。您需要设置textfield.transform相同的属性。

于 2012-10-23T03:59:38.627 回答