Drag Rotate UIImageView by touch

Posted by rockstardev on Stack Overflow See other posts from Stack Overflow or by rockstardev
Published on 2010-05-04T15:25:00Z Indexed on 2010/05/04 15:28 UTC
Read the original article Hit count: 334

Filed under:
|
|

I have an UIImageView that is a subview of an UIView. I'm trying to create a drag rotate effect with the UIImageView similar to spinning a bottle but I'm having trouble calculating the radians to rotate the image by. My code below seems like it should work because I've seen a similar calculation in Flash examples. Can anyone shed some light on what I'm doing wrong?

Thanks.

This is the code contained in my image view subclass:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];
        lastLocation = [touch locationInView:[self superview]];

    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:[self superview]];

        CGFloat xDisplacement = location.x - lastLocation.x;
        CGFloat yDisplacement = location.y - lastLocation.y;

        CGFloat rad = atan2f(yDisplacement,xDisplacement); 

        self.transform = CGAffineTransformMakeRotation(rad);
    }

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiimageview