如何使用平移手势旋转文本字段?

时间:2012-10-23 03:14:30

标签: objective-c ios rotation xcode4.3 uipangesturerecognizer

如果我可以使用平移手势来旋转文本字段以使旋转更平滑,我会考虑可能性。请你帮帮我吗?

更新

    #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)];

}

1 个答案:

答案 0 :(得分:1)

您可以将UITextField视为UIView并应用合适的transform。您将从net获得大量示例代码。您需要为textfield.transform设置相同的属性。