iPhone - 沿触摸和拖动路径移动对象

Pet*_*ete 4 cocoa-touch core-animation objective-c iphone-sdk-3.0 ios

我正在开发一个简单的动画,其中UIImageView沿着a移动UIBezierPath,现在我想提供用户对移动的交互,UIImageView以便用户可以UIImageView通过触摸UIImageView并拖动UIImageview屏幕来引导.

das*_*dom 10

touchesMoved:withEvent:中更改图像视图位置的框架.

编辑:一些代码

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch.view isEqual: self.view] || touch.view == nil) {
        return;
    }

    lastLocation = [touch locationInView: self.view];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch.view isEqual: self.view]) {
        return;
    }

    CGPoint location = [touch locationInView: self.view];

    CGFloat xDisplacement = location.x - lastLocation.x;
    CGFloat yDisplacement = location.y - lastLocation.y;

    CGRect frame = touch.view.frame;
    frame.origin.x += xDisplacement;
    frame.origin.y += yDisplacement;
    touch.view.frame = frame;
    lastLocation=location;
}
Run Code Online (Sandbox Code Playgroud)

你也应该实现touchesEnded:withEvent:touchesCanceled:withEvent:.


Dun*_*n C 5

因此,您希望用户能够沿着弯曲路径触摸关键帧动画中间的图像,并将其拖动到其他位置?你想在那时发生什么动画?

你有很多挑战.

首先是在关键帧动画"飞行中"时检测对象上的触摸.

为此,您需要使用父视图的图层的表示层的hitTest方法.

图层的表示层表示任何给定时刻的图层状态,包括动画.

一旦检测到视图上的触摸,您将需要从表示层获取图像的当前位置,停止动画,并使用基于touchesMoved/touchesDragged的动画接管.

我编写了一个演示应用程序,演示如何检测沿路径动画的对象的触摸.那将是一个很好的起点.

看看这里:

核心动画演示,包括在动画"飞行中"时检测视图上的触摸.