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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...