2010-07-30 44 views
0

我有一個圓形圖像,我試圖旋轉,以便黃色和黑色條紋圓停留在用戶手指的下方並在兩個方向上旋轉。我有這個至今:使用CGAfffineTransformRotate旋轉視圖

- (void)handleJogShuttle:(UIPanGestureRecognizer *)recognizer { 

    UIView *shuttle = [recognizer view]; 

    if ([recognizer state] == UIGestureRecognizerStateBegan || 
     [recognizer state] == UIGestureRecognizerStateChanged) { 

     [recognizer view].transform = CGAffineTransformRotate([[recognizer view] transform],M_PI/20.0f); 
     [recognizer setTranslation:CGPointZero inView:[shuttle superview]]; 
    } 
} 

http://img227.imageshack.us/img227/2396/jog2.png

而且,目前,輕微的動作可以使視圖在一個完整的圓,這顯然是不希望的旋轉。

回答

0

我懷疑這個方法被稱爲很多。嘗試使用NSLog並檢查它做了多少次。 不管怎麼說,你是不是計算的角度正確,你可以嘗試使用

-(CGPoint)translationInView:(UIView *)view 

從識別計算出正確的角度旋轉。

+0

我將如何使用translationInView,因爲它返回一個CGPoint,而不是一個角度?謝謝 – joec 2010-08-02 13:14:31

+0

找出你的圓的中心,並使用反正弦我們的反餘弦計算從前一點到當前的角度。它不是太複雜,但你必須補償徑向平移。我很難在這裏解釋它。 – 2010-08-16 04:25:36

0
#import "RotationTestViewController.h" 

@interface RotationTestViewController() 
-(void)transformSpinnerwithTouches:(UITouch *)touchLocation; 
-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint; 
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform; 
@end 
@implementation RotationTestViewController 

- (void)viewDidLoad { 
[super viewDidLoad];} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) 
    { 
     // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
     // currentTouch=touch; 
    } 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches){ 
     // Sends to the dispatch method, which will make sure the appropriate subview is acted upon 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch=[[event allTouches]anyObject]; 
    if (touch.view==Spinner) 
    { 
     [self transformSpinnerwithTouches:touch]; 
    } 
} 

-(void)transformSpinnerwithTouches:(UITouch *)touchLocation 
{ 
    CGPoint touchLocationpoint = [touchLocation locationInView:self.view]; 
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view]; 
    //origin is the respective piont from that i gonna measure the angle of the current position with respective to previous position .... 
    CGPoint origin; 
    origin.x=Spinner.center.x; 
    origin.y=Spinner.center.y; 
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint]; 
    CGAffineTransform newTransform =CGAffineTransformScale(Spinner.transform, 1, 1); 
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x); 
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint]; 
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x); 
    CGFloat newAngle = currentRotation- previousRotation; 
    temp1=temp1+newAngle; 
    NSLog(@"Angle:%F\n",(temp1*180)/M_PI); 
    newTransform = CGAffineTransformRotate(newTransform, newAngle); 
    [self animateView:Spinner toPosition:newTransform]; 
} 

-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform 
{ 
    [UIView setAnimationsEnabled:YES]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.0750]; 
    Spinner.transform = newTransform; 
    [UIView commitAnimations]; 
} 

//-(CGFloat)distanceFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint 
// 
//{ 
//float x = toPoint.x - fromPoint.x; 
//float y = toPoint.y - fromPoint.y; 
//return hypot(x, y); 
//} 

-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint 
{ 
    CGFloat x = secondPoint.x - firstPoint.x; 
    CGFloat y = secondPoint.y - firstPoint.y; 
    CGPoint result = CGPointMake(x, y); 
    //NSLog(@"%f %f",x,y); 
    return result; 
} 

-(void) dealloc 
{ 
    [Spinner release]; 
    [super dealloc]; 
}