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

时间:2011-02-07 21:59:32

标签: objective-c ios iphone iphone-sdk-4.1

我是ios开发中的noob。我需要一些帮助。我有自定义UIButton与图片“箭头”,所以我需要通过按下并在+360 gr移动手指旋转此按钮。和罗盘-360克一样。

2 个答案:

答案 0 :(得分:0)

以下是进行轮换的代码。

-(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);

}

答案 1 :(得分:0)

详细说明,您可以自定义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”中,您可以计算最终AngularVelocity

如果有任何疑问,可以回复一下。