Matan
Matan

Reputation: 456

Xcode Rotate image torwads a CGPoint without transform

I made an app: you got an image and it follows your finger where ever you are moving it. I also want it to rotate towards a CGPoint.

I tested some things and when I tried at first with the transform the image always jumped back to its original position, rotated and continued following the finger.

How do I rotate without transform (or if you know how to fix what I said it's also good) ?

my code =

//this is called every 0.01 seconds
CGPoint center = self.im.center;
if (!CGPointEqualToPoint(self.im.center, i))
{
    x = center.x;
    y = center.y;
    double distance = sqrtf(powf(i.x - x, 2) + powf(i.y - y, 2));
    float speedX = (2 * (i.x - x)) / distance;
    float speedY = (2 * (i.y - y)) / distance;
    //float degree = (angle) % 360;

    //self.im.center = CGPointMake(center.x+speedX, center.y+speedY);
    CGAffineTransform translate = CGAffineTransformMakeTranslation(center.x+speedX,     center.y+speedY);
    CGAffineTransform rotate = CGAffineTransformMakeRotation(angle);
    [self.im setTransform:CGAffineTransformConcat(rotate, translate)];
}
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    i = [touch locationInView:self.view];
    angle = atan2f(self.im.center.y-i.y, self.im.center.x-i.x);
    //[self.im setTransform:CGAffineTransformRotate(self.im.transform, angle)];

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    i=[touch locationInView:self.view];
    angle = atan2f(self.im.center.y-i.y, self.im.center.x-i.x);
}

Upvotes: 0

Views: 1604

Answers (1)

Alexander Vasenin
Alexander Vasenin

Reputation: 13053

You can apply any number of transformations to you image view with CGAffineTransformConcat. Something like:

CGAffineTransform translate = CGAffineTransformMakeTranslation(dx, dy);
CGAffineTransform rotate = CGAffineTransformMakeRotation(angle);
[yourImageView setTransform:CGAffineTransformConcat(rotate, translate)];

Upvotes: 1

Related Questions