我有一系列沿弧线制作动画的 UIView。每个视图上都有一个 UIPanGestureRecognizer,它在任何动画发生之前按预期工作。
然而,在对视图进行动画处理后(使用下面的方法对视图层进行动画处理),手势无法按预期工作;事实上,它们看起来好像在它们原来的屏幕位置上工作。
我尝试了各种方法,包括将视图框架和/或边界与动画层上的相匹配,甚至删除现有手势并再次创建和添加它们(下面未显示)。
如果有人知道发生了什么或能让我走上解决问题的道路,将不胜感激。
编辑: iMartin 的改变建议
pathAnimation.removedOnCompletion = NO;
到
pathAnimation.removedOnCompletion = YES;
和
view.layer.position = ... //
让我走上了解决问题的道路。
原始代码:
- (void)animateUIViewAlongArc:(UIView*)view clockwise:(BOOL)clockwise duration:(float)duration
{
[UIView animateWithDuration:duration animations:^{
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.repeatCount = 1;
CGPoint viewCenter = view.objectCenter;
CGMutablePathRef arcPath = CGPathCreateMutable();
CGPathMoveToPoint(arcPath, NULL, viewCenter.x, viewCenter.y);
float angleOfRotation = self.angleIncrement;
if (clockwise == NO)
{
angleOfRotation *= -1.0f;
}
float fullwayAngle = view.angle + angleOfRotation;
CGPoint fullwayPoint = pointOnCircle(self.center, self.radius, fullwayAngle);
CGPathAddRelativeArc(arcPath, NULL, self.center.x, self.center.y, self.radius, view.angle, angleOfRotation);
pathAnimation.path = arcPath;
CGPathRelease(arcPath);
[view.layer addAnimation:pathAnimation forKey:@"arc"];
view.angle = fullwayAngle;
view.objectCenter = fullwayPoint;
} completion:^(BOOL finished){
if (finished)
{
CGRect frame = view.layer.frame;
CGRect bounds = view.layer.bounds;
view.frame = frame;
view.bounds = bounds;
}
}];
}