私はアプリを作りました: あなたはイメージを持っていて、あなたがそれを動かしているときはいつでもあなたの指に従います. また、 に向かって回転させたいですCGPoint
。
私はいくつかのことをテストしましたが、最初に変換を試みたとき、画像は常に元の位置に戻り、回転し、指をたどり続けました。
変換せずに回転するにはどうすればよいですか (または、私が言ったことを修正する方法を知っていれば)。
私のコード=
//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);
}