iphone - 使用物镜 C 旋转图像

标签 iphone objective-c

我正在尝试旋转图像。我正在成功地做到这一点。问题是,当我向下单击并轻轻拖动时,图像会完全旋转以到达我单击的位置,然后在我顺时针拖动图像时缓慢旋转。我很关心为什么它会完全旋转到我拖动的地方。我希望拖动从找到的位置开始,而不是旋转到我手指按下的位置,然后从那里开始。

这是我的代码:

-

    (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {   
    }

    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *allTouches = [event allTouches];

        int len = [allTouches count]-1;

        UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
        CGPoint location = [touch locationInView:[self superview]];
        float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );

        totalRadians = theAngle;

        [self rotateImage:theAngle];

    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}

-(void) rotateImage:(float)angleRadians{

    self.transform = CGAffineTransformMakeRotation(angleRadians);
    CATransform3D rotatedTransform = self.layer.transform;
    self.layer.transform = rotatedTransform;    
}

我做错了什么吗?

谢谢!

最佳答案

不要从触摸得到的角度创建变换,而是尝试将 totalRadians 增加与新角度的差值,然后从中创建变换。

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *allTouches = [event allTouches];

        int len = [allTouches count]-1;

        UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
        CGPoint location = [touch locationInView:[self superview]];
        float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );

        totalRadians += fabs(theAngle - totalRadians);
        totalRadians = fmod(totalRadians, 2*M_PI);

        [self rotateImage:totalRadians];

    }

关于iphone - 使用物镜 C 旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4010779/

相关文章:

iphone - 如何创建全局导航堆栈?

iphone - 有没有办法在iPhone上的iOS中以编程方式设置UIBackgroundModes?

iphone - 如何在设备之间同步核心数据?

objective-c - 将一个数组分成四个元素的单独数组加上提醒

objective-c - 重新加载部分索引时出现奇怪的错误?

java - JSONObject Java 相当于 Objective C

ios - 是否有任何工作算法可以在所有 iOS 设备(有或没有 M7 芯片)上计算步数?

iphone - 使用 VLC,是否可以在同一台计算机上对 30 到 40 个基于 IP 的网络摄像头进行编码?

objective-c - 我可以让 AFNetworking 自动将 NULL 解析为 nil 吗?

ios - 从包含对象名称的字符串访问对象的属性