我想将 uiimage 从右侧(屏幕外)滑到中间,然后从中间滑到左侧(屏幕外)。当图像到达中间(动画路径的第一个点)时,我想调用一个自定义函数。我怎么能意识到这一点?
感谢帮助。
这就是我尝试过的,但它在中间有点滞后(第二个动画的开始)。我觉得最好有一部动画
我的代码:
- (void) animateFromRightToMiddlePath {
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 3;
pathAnimation.repeatCount = 1;
pathAnimation.delegate = self;
[pathAnimation setValue:@"rightToMiddle" forKey:@"AnimationType"];
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, x + (picture.size.width/2), 250);
CGPathAddLineToPoint(curvedPath, NULL, (picture.size.width/2), 250);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
imageView = [[UIImageView alloc] initWithImage:picture];
imageView.tag = 1;
imageView.frame = CGRectMake(1, 1, picture.size.width, picture.size.height);
[self addSubview:imageView];
[imageView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
}
- (void) animateFromMiddleToLeftPath {
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 3;
pathAnimation.repeatCount = 1;
pathAnimation.delegate = self;
[pathAnimation setValue:@"middleToLeft" forKey:@"AnimationType"];
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, (picture.size.width/2), 250);
CGPathAddLineToPoint(curvedPath, NULL, -(picture.size.width/2), 250);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
[imageView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
NSString *aniType = [theAnimation valueForKey:@"AnimationType"];
if ([aniType isEqualToString:@"rightToMiddle"]) {
[self animateFromMiddleToLeftPath];
// CUSTOM FUNCTION CALL
}
if ([aniType isEqualToString:@"middleToLeft"]) {
// [self animateFromRightToMiddlePath];
}
}