Skip to content

UIRotationGestureRecognizer ​

UIRotationGestureRecognizer 表示一个旋转识别器:两根手指绕着彼此转动时认出手势。用它做两指旋转。

旋转是连续手势,派发 began、changed、ended、cancelled。事件对象带相对手势开始时转过的角度 rotation(弧度)和旋转速度 velocity。每次手势的 rotation 都从 0 开始。

旋转还没有用真实触摸核对过,手势本身和事件带的数据都未在模拟器验证。

继承关系:EventTarget → NSObject → UIGestureRecognizer → UIRotationGestureRecognizer

怎么用它,见指南响应手势。

构造函数 ​

UIRotationGestureRecognizer()

创建一个识别两指旋转的识别器。

实例属性 ​

还继承 UIGestureRecognizer 的属性,常用的是 isEnabled、view、cancelsTouchesInView。方法也都继承自它:addEventListener()、removeEventListener()、requireGestureRecognizerToFail()。

属性说明
rotation相对手势开始时转过的角度,单位是弧度
velocity 只读旋转的速度,单位是弧度每秒

事件 ​

事件来自 UIGestureRecognizer,用 addEventListener() 监听。它 派发的是:

事件什么时候派发
began旋转被认出时
changed之后角度每变化一次
ended手指抬起时
cancelled被打断时

示例 ​

js
const card = new UIView();
const twist = new UIRotationGestureRecognizer();
let baseAngle = 0;
twist.addEventListener('changed', (e) => {
  const { rotation } = /** @type {UIRotationGestureRecognizerEvent} */ (e);
  card.transform = CGAffineTransform.rotation(baseAngle + rotation);
});
twist.addEventListener('ended', (e) => {
  const { rotation } = /** @type {UIRotationGestureRecognizerEvent} */ (e);
  baseAngle = baseAngle + rotation;
});
card.addGestureRecognizer(twist);

效果:两根手指在 card 上转动,card 跟着转,松手后保持角度。未在模拟器验证。

对应的 iOS API ​

ObjCJS
rotation.rotation事件对象的 rotation,或 twist.rotation
rotation.velocity事件对象的 velocity,或 twist.velocity

参见 ​