2012-10-23 44 views
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)]; 

} 
+0

嗨,剛剛谷歌了這個..... http://rogchap.com/2011/06/10/ios-image-manipulation-with-uigesturerecognizer-scale-move-rotate/ – Spynet

+0

謝謝。但我想使用平移手勢嘗試一個手指旋轉? – chineseGirl

+0

嗨請通過此鏈接和谷歌出http://blog.mellenthin.de/archives/2012/02/13/an-one-finger-rotation-gesture-recognizer/ https://github.com/kirbyt/ KTOneFingerRotationGestureRecognizer – Spynet

回答

1

你可以考慮UITextFieldUIView並應用適當的transform。你會從網上得到很多樣本代碼。您需要將textfield.transform屬性設置爲相同。

相關問題