Ios UITouch触摸移动的手指位置通过关键帧动画设置视图动画
我正在重新写我的问题,因为它被修改了很多,现在它变成了一个新问题Ios UITouch触摸移动的手指位置通过关键帧动画设置视图动画,ios,uitouch,cakeyframeanimation,Ios,Uitouch,Cakeyframeanimation,我正在重新写我的问题,因为它被修改了很多,现在它变成了一个新问题 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; location = [touch locationInView:self.view]; CGPoint prevLocation = [touch previousLocationInView:se
- (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;
}
animationWithKeyPath值现在是位置,现在正在工作:)
我有一些小问题,但我已经解决了
但我的新问题是:
完成动画并停止后,“球视图”将返回到其起点
我希望它停留在路径的最后一点。
解决方案:我终于找到了解决方案
我添加了一个CGPoint finalPoint=[locArray count]-1值。
那么
这一切都解决了。现在,球停留在TouchsMoved事件捕获的手指触摸的最终位置。更改中心属性Yok后,您无需重新绘制球!丹克,我删除了重画,但它仍然没有给我想要的。我希望动画在我完成触摸事件后开始。若要沿路径移动球,需要创建一个CAKeyframeAnimation并为其路径属性指定一个CGPath。它将无法与animateWithDuration一起工作。再次感谢!我现在正在尝试并将在这里告知结果。Phix,我尝试添加CAKeyframeAnimation,但它不起作用。这是我有生以来第一次尝试。我不知道现在有什么问题。
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animLayer.position = finalPoint;