ios – 确定CALayer已经旋转了多少

前端之家收集整理的这篇文章主要介绍了ios – 确定CALayer已经旋转了多少前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个程序,其中CALayer必须旋转到某个值.
如何确定CALayer的当前旋转?
我有一个UIRotationGestureRecognizer来旋转图层:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == rotationGestureRecognizer) {
        NSLog(@"gestureRecRotation: %f",rotationGestureRecognizer.rotation);
        CATransform3D current = _baseLayer.transform;
        _baseLayer.transform = CATransform3DRotate(current,rotationGestureRecognizer.rotation * M_PI / 180,1.0);
    }
}

我从一层必须旋转一定数量的拼图开始.那么如何获得当前图层的旋转?

解决方法

你可以做数学,得到这样的角度:
CATransform3D transform = _baseLayer.transform;
CGFloat angle = atan2(transform.m12,transform.m11);

但是更容易做到如下:

CGFloat angle = [(NSNumber *)[_baseLayer valueForKeyPath:@"transform.rotation.z"] floatValue];

documentation

Core Animation extends the key-value coding protocol to allow getting and setting of the common values of a layer’s CATransform3D matrix through key paths. Table 4 describes the key paths for which a layer’s transform and sublayerTransform properties are key-value coding and observing compliant

原文链接:https://www.f2er.com/iOS/329829.html

猜你在找的iOS相关文章