我正在努力在动画结束时从视图中删除一个对象(blueArrow)。通过使用延迟后跟一个方法将其从superView中删除它来删除它对我来说不起作用,因为我希望在屏幕上设置这些箭头的动画流。目的是调用多次添加箭头并为箭头添加动画的方法,每次动画只发生一次,并从视图中删除它创建的箭头。我已经设置了animationDidStop
方法,但似乎永远不会被调用,思想?提前谢谢!
-(void)blueArrowAnimation{
UIImage *blueArrowImage = [UIImage imageNamed:@"blueArrow.png"];
//Prepare the animation - we use keyframe animation for animations of this complexity
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.rotationMode = kCAAnimationRotateAuto;
//Set some variables on the animation
pathAnimation.calculationMode = kCAAnimationPaced;
//We want the animation to persist - not so important in this case - but kept for clarity
//If we animated something from left to right - and we wanted it to stay in the new position,
//then we would need these parameters
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = YES;
pathAnimation.duration = 3.0;
//Lets loop continuously for the demonstration
pathAnimation.repeatCount = 1;
//Setup the path for the animation - this is very similar as the code the draw the line
//instead of drawing to the graphics context, instead we draw lines on a CGPathRef
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 10, 40);
CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 300, 130, 300);
//CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);
//Now we have the path, we tell the animation we want to use this path - then we release the path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
self.blueArrow = [[UIImageView alloc] initWithImage:blueArrowImage];
self.blueArrow .frame = CGRectMake(1, 1, 20, 20);
pathAnimation.delegate = self;
[self.view addSubview:self.blueArrow];
[self.blueArrow.layer addAnimation:pathAnimation forKey:@"moveTheArrow"];
}
- (void)animationDidStop:(CAAnimation *)pathAnimation finished:(BOOL)flag
{
CALayer *layer = [pathAnimation valueForKey:@"moveTheArrow"];
if (layer) {
NSLog(@"removed %@ (%@) from superview", layer, [layer name]);
[layer removeFromSuperlayer];
}
}
答案 0 :(得分:0)
我创建了一个这样的类:
class ViewController: UIViewController, CAAnimationDelegate {
func creatAnimation() {
...some code
animation.delegate = self
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print("done")
}
}
animationDidStop 在动画结束时启动。
但是我在完成动画后删除图像时遇到问题。所以最后,这对我有用: let projectile = UIImageView(image: UIImage(named: assetName)) projectile.frame = CGRect(x:boardFields[indexField].fieldImage.frame.minX, y:boardFields[indexField].fieldImage.frame.minY,width:width,height:height) projectile.transform = projectile.transform.rotated(by: -angle) self.view.addSubview(projectile)
UIView.animate(withDuration: 2, animations: {
let animation = CAKeyframeAnimation()
animation.keyPath = "position.x" // move along x axis
animation.values = [0, xTranslation]
animation.keyTimes = [0, 1]
animation.duration = duration
animation.isAdditive = true
projectile.layer.add(animation, forKey: "moveX")
animation.keyPath = "position.y" // move along y axis
animation.values = [0, yTranslation]
animation.keyTimes = [0, 1]
animation.duration = duration
animation.isAdditive = true
projectile.layer.add(animation, forKey: "moveY")
animation.keyPath = "bounds.size.width" // grow and than shrink width of image
animation.values = [0, 200, 0]
animation.keyTimes = [0, 0.5, 1]
animation.duration = duration
animation.isAdditive = true
projectile.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
projectile.layer.add(animation, forKey: "resize.width")
animation.keyPath = "bounds.size.height" // grow and than shrink width of image
animation.values = [0, 100, 0]
animation.keyTimes = [0, 0.5, 1]
animation.duration = duration
animation.isAdditive = true
animation.isRemovedOnCompletion = true
animation.delegate = self
projectile.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
projectile.layer.add(animation, forKey: "resize.width")
}, completion: { finished in
projectile.removeFromSuperview()
})