UITouch触摸移动手指位置通过CAKeyframeAnimation为视图设置动画

时间:2012-04-23 10:03:45

标签: ios uitouch cakeyframeanimation

我正在重写我的问题,因为它经过了很多修改,现在又变成了一个新问题。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    location = [touch locationInView:self.view];
    CGPoint prevLocation = [touch previousLocationInView:self.view];
    CGFloat distanceFromPrevious = [self distanceBetweenPoints:location :prevLocation];
    NSTimeInterval timeSincePrevious = event.timestamp - self.previousTimestamp;
    if (locArray == NULL) {
        locArray = [[NSMutableArray alloc] init];
    }
    [locArray addObject:[NSValue valueWithCGPoint:prevLocation]];
    CGFloat speed = distanceFromPrevious/timeSincePrevious;
    self.previousTimestamp = event.timestamp;

    //NSLog(@"speed %f | time %f", speed, timeSincePrevious);

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CALayer *animLayer = ball.layer;
    animLayer.bounds = ball.bounds;
    animLayer.position = CGPointMake(self.view.frame.size.width/2, 0);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.view.frame.size.width/2, 380);

    for (int i=0; i<[locArray count]; i++) {
        NSValue *val = [locArray objectAtIndex:i];
        CGPoint p = [val CGPointValue];
        CGPathAddLineToPoint(path, NULL, p.x, p.y);

        NSLog(@"Points %f %d", p.y, i);
    }
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
    animation.duration = 4.0f;
    animation.path = path;
    animation.removedOnCompletion = NO;
    CGPathRelease(path);

    [animLayer addAnimation:animation forKey:@"path"];
    locArray = nil;

}

我正在尝试基于UITOuch touchesMoved事件捕获的名为ball的视图类。 LocArray持有移动触摸的CGPoint值。 我试图将这些值放入我的CGMutablePathRef中。 而且,我尝试在用户手指移动输入创建的路径上设置球的动画。

我不能让我的动画工作。这有什么问题?

编辑:

我改变了:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

animationWithKeyPath值现在是位置,现在它正在工作:)

我有一些小问题,但我已经解决了。

但我的新问题是:

完成动画后,球视图将返回其起点并停止。

我希望它保持在路径的最后一点。

解决方案:我最终找到了解决方案。

我添加了一个CGPoint finalPoint = [locArray count] -1值。 然后,

animLayer.position = finalPoint;

这解决了这一切。现在球停留在touchesMoved事件捕获的手指触摸的最终位置。

0 个答案:

没有答案