gpt4 book ai didi

iphone - Xcode 旋转图像无需变换即可旋转 CGPoint

转载 作者:行者123 更新时间:2023-11-29 03:44:50 26 4
gpt4 key购买 nike

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

最佳答案

您可以使用 CGAffineTransformConcat 对 ImageView 应用任意数量的转换.像这样的东西:

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

关于iphone - Xcode 旋转图像无需变换即可旋转 CGPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com