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

标签 iphone ios objective-c

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

相关文章:

objective-c - 在 PC 上为 iPhone 开发?

ios - 计算海拔爬升的总英尺数给出奇怪的结果

iphone - 如何获取FB页面的feed?

ios - ABAddressBookCopyPeopleWithName 在模拟器上返回空数组

iphone - 使用 CATiledLayer 绘制文本

ios - Core Graphics cocoa 内存泄漏

ios - 淡入淡出 View

iPhone 4 标签栏图标

ios - 跟踪ios设备的振幅声音

ios - 在 iOS 8 中的 Facebook 上共享时 SLComposeViewController 自动关闭