CGAffineTransformMakeRotation goes the other way after 180 degrees (-3.14)

Posted by TheKillerDev on Stack Overflow See other posts from Stack Overflow or by TheKillerDev
Published on 2013-10-26T21:48:55Z Indexed on 2013/10/26 21:53 UTC
Read the original article Hit count: 191

Filed under:
|
|

So,

i am trying to do a very simple disc rotation (2d), according to the user touch on it, just like a DJ or something. It is working, but there is a problem, after certain amount of rotation, it starts going backwards, this amount is after 180 degrees or as i saw in while logging the angle, -3.14 (pi).

I was wondering, how can i achieve a infinite loop, i mean, the user can keep rotating and rotating to any side, just sliding his finger? Also a second question is, is there any way to speed up the rotation?

Here is my code right now:

#import <UIKit/UIKit.h>

@interface Draggable : UIImageView {
    CGPoint firstLoc;
    UILabel * fred;
    double angle;
}

@property (assign) CGPoint firstLoc;
@property (retain) UILabel * fred;

@end


@implementation Draggable

@synthesize fred, firstLoc;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    angle = 0;
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)handleObject:(NSSet *)touches
          withEvent:(UIEvent *)event
             isLast:(BOOL)lst
{
    UITouch *touch =[[[event allTouches] allObjects] lastObject];
    CGPoint curLoc = [touch locationInView:self];

    float fromAngle = atan2( firstLoc.y-self.center.y,
                            firstLoc.x-self.center.x );
    float toAngle = atan2( curLoc.y-(self.center.y+10),
                          curLoc.x-(self.center.x+10));
    float newAngle = angle + (toAngle - fromAngle);

    NSLog(@"%f",newAngle);

    CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);

    self.transform = cgaRotate;

    if (lst)
        angle = newAngle;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch =[[[event allTouches] allObjects] lastObject];
    firstLoc = [touch locationInView:self];
};


-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleObject:touches withEvent:event isLast:NO];
};

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self handleObject:touches withEvent:event isLast:YES];
}

@end

And in the ViewController:

UIImage *tmpImage = [UIImage imageNamed:@"theDisc.png"];
CGRect cellRectangle;
    cellRectangle = CGRectMake(-1,self.view.frame.size.height,tmpImage.size.width ,tmpImage.size.height );
    dragger = [[Draggable alloc] initWithFrame:cellRectangle];
    [dragger setImage:tmpImage];
    [dragger setUserInteractionEnabled:YES];

    dragger.layer.anchorPoint = CGPointMake(.5,.5);

    [self.view addSubview:dragger];

I am open to new/cleaner/more correct ways of doing this too.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about ios

Related posts about objective-c