开发者

Rotate view using CGAfffineTransformRotate

I have a circular image which I am trying to rotate, so that the yellow and black striped circle stays under the user's finger and rotates in both directions. I have this so far:

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

    UIView *shuttle = [recognizer view];

    if ([recognizer state] == UIGestureRecognizerStateBegan ||
        [recognizer state] == UIGestureRe开发者_开发百科cognizerStateChanged) {

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

Rotate view using CGAfffineTransformRotate

Also, currently, the slightest movement can cause the view to rotate in a full circle, which is obviously not desired.


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


I suspect this method gets called A LOT. Try using NSLog and check how many time it does. Anyway, you are not calculating the angle properly, you could try using

-(CGPoint)translationInView:(UIView *)view

from the recognizer to calculate the correct angle to rotate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜