我制作了一个应用程序:你得到了一张图像,无论你在哪里移动它都会跟随你的手指。我也希望它向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);
}