我有一个程序,其中CALayer必须旋转到某个值.
如何确定CALayer的当前旋转?
我有一个UIRotationGestureRecognizer来旋转图层:
如何确定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];
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