如何通过点击并按住来旋转 UIButton(按住 - 用手指旋转)

发布于 2024-10-16 11:19:48 字数 96 浏览 1 评论 0原文

我是ios开发菜鸟。我需要一些帮助。我有带有“箭头”图片的自定义 UIButton,因此我需要通过按下并在 +360 gr 中移动手指来旋转此按钮。 -360 克,如指南针箭头。

I'm noob in ios development. I need some help. I have custom UIButton with picture "arrow", so I need to rotate this button by pressing and moving finger in +360 gr. and -360 gr., like compass arrow.

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

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

发布评论

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

评论(2

烦人精 2024-10-23 11:19:48

这是进行旋转的代码。

-(void)LongPress:(UILongPressGestureRecognizer *)gesture {

    CGPoint p = [gesture locationInView:self.view];

    CGPoint zero;
    zero.x = self.view.bounds.size.width / 2.0;
    zero.y = self.view.bounds.size.height / 2.0;

    CGPoint newPoint;

    newPoint.x = p.x - zero.x;
    newPoint.y = zero.y - p.y;
    CGFloat angle;
    angle = atan2(newPoint.x, newPoint.y);
    self.myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle);

}

Here is code that makes rotation.

-(void)LongPress:(UILongPressGestureRecognizer *)gesture {

    CGPoint p = [gesture locationInView:self.view];

    CGPoint zero;
    zero.x = self.view.bounds.size.width / 2.0;
    zero.y = self.view.bounds.size.height / 2.0;

    CGPoint newPoint;

    newPoint.x = p.x - zero.x;
    newPoint.y = zero.y - p.y;
    CGFloat angle;
    angle = atan2(newPoint.x, newPoint.y);
    self.myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle);

}
暮凉 2024-10-23 11:19:48

具体来说,可以自定义一个rotateView,然后:

1:在“touchesBegan”的委托方法中,获取手指的initialPointinitialAngle

2:在“touchesMoved”中,获取手指的newPoint

CGPoint newPoint = [[touches anyObject] locationInView:self];
  [self pushTouchPoint:thePoint date:[NSDate date]];
  double angleDif = [self angleForPoint:newPoint] - [self angleForPoint:initialPoint];
  self.angle = initialAngle + angleDif;
  [[imageView layer] setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];

3:最后,在“touchesEnded”中可以计算出最终的角速度。

如果有任何困惑,为了了解更多详细信息,您可以写信回来。

In detail, you can custom a rotateView,then:

1: In the delegate method of "touchesBegan", get initialPoint of finger and initialAngle.

2: During "touchesMoved",get the newPoint of finger:

CGPoint newPoint = [[touches anyObject] locationInView:self];
  [self pushTouchPoint:thePoint date:[NSDate date]];
  double angleDif = [self angleForPoint:newPoint] - [self angleForPoint:initialPoint];
  self.angle = initialAngle + angleDif;
  [[imageView layer] setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];

3: At last, in "touchesEnded" you can calculate final AngularVelocity.

If anything being confused, for more detail, you can write back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文