当用户执行UIRotationGesture时,我需要旋转一个正方形。
我把手势都准备好了。问题是,用户每次移动手指时,正方形都会返回到开始位置,然后再动画到新位置。而不是从以前的位置移动到新的位置。
即,如果正方形旋转90度,并且用户继续旋转到100度。正方形将回到0度,并设置为100度的动画。
从本质上讲,我希望当用户执行旋转手势时,正方形能够反映用户。
- (void)respondToGesture:(UIRotationGestureRecognizer *)rec{
NSLog(@"Rotation: %f", rec.rotation);
[self rotateWithRadian:rec.rotation];
if (rec.state == UIGestureRecognizerStateEnded) {
NSLog(@"gesture ended");
}
}
- (void)rotateWithRadian:(float)radian{
CABasicAnimation *spin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spin.removedOnCompletion = NO;
spin.fillMode = kCAFillModeForwards;
[spin setByValue:[NSNumber numberWithFloat:radian]];
[spin setDuration:1.0];
[squarelayer addAnimation:spin forKey:@"spinAnimation"];发布于 2012-02-01 19:41:04
有没有什么原因你不直接设置图层的变换,而是使用动画?
这应该是您想要的结果:
- (void)respondToGesture:(UIRotationGestureRecognizer *)rec {
if (rec.state == UIGestureRecognizerStateChanged) {
CGAffineTransform currentTransform = squareLayer.affineTransform;
squareLayer.affineTransform = CGAffineTransformRotate(currentTransform, gesture.rotation);
gesture.rotation = 0;
}
}如果你想让旋转发生在用户旋转的中心,而不是图层的中心,你也需要调整squareLayer.anchorPoint。
发布于 2012-02-01 19:25:41
有一个开源的( https://github.com/kirbyt/KTOneFingerRotationGestureRecognizer )用于单指旋转。你可以去看看,会更好的。即使你只想要普通的旋转手势,你也可以在代码中寻找更好的旋转代码。它使用中心作为原点。
https://stackoverflow.com/questions/9094906
复制相似问题