0

我想将 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];

    }

}
4

1 回答 1

0

一种对这种交互进行建模的方法如下:

  1. 您的图像根据其自身的逻辑在屏幕上移动(固定动画,您拖动它等);

  2. 您定义一个计时器,以便在每次滴答时调用一个处理函数;此处理程序有责任(在您的特定情况下)检查图像的位置;

  3. 当位置是您想要的位置时,处理程序会触发您的其他功能。

这可能看起来很复杂,但事实并非如此。它允许你以模块化和可重用的方式解决这个问题,它的灵感来自于游戏如何处理这类问题。

于 2011-07-19T08:35:31.310 回答