我正在重写我的问题,因为它被修改了很多,现在它变成了一个新问题。
- (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;
}
我正在尝试基于 UITTouch touchesMoved 事件捕获的名为 ball 的视图类。LocArray 保存移动触摸的 CGPoint 值。我试图将这些值放入我的 CGMutablePathRef 中。然后,我尝试在由用户手指移动输入创建的路径上为球设置动画。
我不能让我的动画工作。它有什么问题?
编辑:
我改变了 :
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animationWithKeyPath 值现在是位置,现在它正在工作:)
我有一些小问题,但我已经解决了。
但我的新问题是:
完成动画并停止后,球视图正在返回其起点。
我希望它停留在路径的最后一点。
解决方案:我终于找到了解决方案。
我添加了一个 CGPoint finalPoint = [locArray count]-1 值。然后,
animLayer.position = finalPoint;
这一切都解决了。现在球停留在 touchesMoved 事件捕获的手指触摸的最终位置。