2010-07-30 46 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]; 
}