使用 UITouch 旋转 UIView

发布于 2024-09-24 12:41:47 字数 287 浏览 5 评论 0原文

我有一个关于如何使用基于触摸的事件旋转 UIView 的问题。

很简单,我想围绕锚点旋转 UIView,具体取决于我在该视图中触摸和关闭的位置。想象一下高保真音响上的音量旋钮,您可以用一根手指转动它。

我已经使用 CAKeyframeAnimation 构建了一个旋转方法,该方法执行 transform.rotation.z,但是我不确定是否(a)我应该将其与触摸方法结合使用,以及(b)如何获取坐标/触摸相对于底层 UIView 的角度,然后将其旋转为指向触摸。

我希望我说得有道理……有人能提出任何建议吗?

I have a question on how I should approach rotating a UIView with touch based events.

Quite simply, I want to rotate a UIView around an anchorpoint, depending on where I touch on and off within that view. Imagine a volume knob on a hifi, which you turn with one finger.

I've built a rotation method using CAKeyframeAnimation which carries out a transform.rotation.z, however what I am not sure is if (a) I should use that in conjunction with touch methods, and (b) how I can get the coordinate/angle of the touch in relation to the underlying UIView, and subsequently turn it to point to the touch.

I hope I am making sense… Can anyone make any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-10-01 12:41:47
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

CGPoint newLocationPoint = [[touches anyObject] locationInView:self.superview];

int x = self.superview.center.x;
int y = self.superview.center.y;

float dx = newLocationPoint.x - x;
float dy = newLocationPoint.y - y;

double angle = atan2(-dx,dy);

self.layer.position = self.superview.center;
self.layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);

NSLog(@"%f,%f ", dx,dy);

将其放入您的 UIView 子类中,瞧!

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

CGPoint newLocationPoint = [[touches anyObject] locationInView:self.superview];

int x = self.superview.center.x;
int y = self.superview.center.y;

float dx = newLocationPoint.x - x;
float dy = newLocationPoint.y - y;

double angle = atan2(-dx,dy);

self.layer.position = self.superview.center;
self.layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1);

NSLog(@"%f,%f ", dx,dy);

}

Put this in your UIView Subclass and Voila!

心房敞 2024-10-01 12:41:47
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

CGPoint Location = [[touches anyObject] locationInView:self];

int x = ImageView.center.x;
int y = ImageView.center.y;

float dx = Location.x - x;
float dy = Location.y - y;

double a = atan2(-dx,dy);
ImageView.layer.position = diagramImageView.center;
ImageView.layer.transform = CATransform3DMakeRotation(a, 0, 0, 1);
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

CGPoint Location = [[touches anyObject] locationInView:self];

int x = ImageView.center.x;
int y = ImageView.center.y;

float dx = Location.x - x;
float dy = Location.y - y;

double a = atan2(-dx,dy);
ImageView.layer.position = diagramImageView.center;
ImageView.layer.transform = CATransform3DMakeRotation(a, 0, 0, 1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文