我一直在玩斯坦福大学讲座/WWDC 视频,该视频向您展示了如何创建同时捏/缩放的手势。它返回一个CGAffineTransform
然后你应用到 UIView。
这一切都很好,但我试图让它更流畅一些,并为代码添加一些减速。我试图通过在每次UIGestureRecognizerStateChanged
调用时存储转换矩阵来做到这一点,然后当我收到时,UIGestureRecognizerStateEnded
我存储最后一个 Changed 和 Ended 矩阵之间的增量:
- (void)handleTransform:(TransformGestureRecognizer *)transformRecognizer
{
CGAffineTransform transform = transformRecognizer.transform;
// cancel any previous animation updates
[UIView cancelPreviousPerformRequestsWithTarget:self selector:@selector(decelerateView:) object:transformRecognizer.view];
if(transformRecognizer.state == UIGestureRecognizerStateBegan)
{
transform = CGAffineTransformConcat(transformRecognizer.view.transform, transform);
transformRecognizer.transform = transform;
}
else if(transformRecognizer.state == UIGestureRecognizerStateChanged)
{
changingTransform = transform;
}
else if(transformRecognizer.state == UIGestureRecognizerStateEnded)
{
// get transform difference
releaseTransformDiff.a = (transform.a - changingTransform.a);
releaseTransformDiff.b = (transform.b - changingTransform.b);
releaseTransformDiff.c = (transform.c - changingTransform.c);
releaseTransformDiff.d = (transform.d - changingTransform.d);
releaseTransformDiff.tx = (transform.tx - changingTransform.tx);
releaseTransformDiff.ty = (transform.ty - changingTransform.ty);
// start updating deceleration animation
[self performSelector:@selector(decelerateView:) withObject:transformRecognizer.view afterDelay:1.0/60.0];
}
transformRecognizer.view.transform = transform;
}
然后,我有一个decelerateView:
动画减速的方法:
#define DECELERATION_RATE 0.9
- (void)decelerateView:(UIView *)view
{
releaseTransformDiff.a = releaseTransformDiff.a * DECELERATION_RATE;
releaseTransformDiff.b = releaseTransformDiff.b * DECELERATION_RATE;
releaseTransformDiff.c = releaseTransformDiff.c * DECELERATION_RATE;
releaseTransformDiff.d = releaseTransformDiff.d * DECELERATION_RATE;
releaseTransformDiff.tx = releaseTransformDiff.tx * DECELERATION_RATE;
releaseTransformDiff.ty = releaseTransformDiff.ty * DECELERATION_RATE;
view.transform = CGAffineTransformMake(view.transform.a + releaseTransformDiff.a,
view.transform.b + releaseTransformDiff.b,
view.transform.c + releaseTransformDiff.c,
view.transform.d + releaseTransformDiff.d,
view.transform.tx + releaseTransformDiff.tx,
view.transform.ty + releaseTransformDiff.ty);
[self performSelector:@selector(decelerateView:) withObject:view afterDelay:1.0/60.0];
}
这有时有效,但有时它会突然停止,并且几乎从不减慢我所做的任何旋转旋转......我想我没有正确计算两个矩阵的增量。这样做的正确方法是什么?