我有当前代码:
- (void) touchesMovedNSSet *)touches withEventUIEvent *)event {
self.objectPoint = [[touches anyObject] locationInView:self];
float x, y;
if (self.objectPoint.x > self.objectPoint.x) {
x = self.objectPoint.x + 1;
}
else x = self.objectPoint.x - 1;
if (self.fingerPoint.y > self.objectPoint.y) {
y = self.objectPoint.y + 1;
}
else y = self.minionPoint.y - 1;
self.objectPoint = CGPointMake(x, y);
[self setNeedsDisplay];
}
我的问题是我想让对象跟随您的手指,直到您将手指从屏幕上移开。只有当我的手指移动时它才会跟随。 touchesEnded 仅在我将手指从屏幕上移开时才起作用,所以这也不是我想要的。如何启用可以解决我的问题的功能?
Best Answer-推荐答案 strong>
如果您想触摸屏幕的某个部分,并且只要您的手指一直向下,就想朝那个方向移动绘制的对象,有几种方法。
On 方法是使用某种形式的计时器,当用户将手指放在屏幕上时,它会重复调用一个方法(因为,正如您所指出的,您只会获得对 touchesMoved的更新 移动时)。虽然 NSTimer 是您遇到的最常见的计时器,但在这种情况下,您需要使用一个称为显示链接的专用计时器,即 CADisplayLink ,它会触发何时可以执行屏幕更新。所以,你会:
在touchesBegan 中,捕捉用户在屏幕上触摸的位置并启动CADisplayLink ;
在 touchesMoved 中,您将更新用户的触摸位置(但仅在他们移动手指时调用);
在 touchesEnded 中,您可能会停止显示链接;和
在您的 CADisplayLink 处理程序中,您将更新位置(并且您需要知道您希望它移动的速度)。
所以,这看起来像:
- (void)touchesBeganNSSet *)touches withEventUIEvent *)event
{
self.velocity = 100.0; // 100 points per second
self.touchLocation = [[touches anyObject] locationInView:self];
[self startDisplayLink];
}
- (void)touchesMovedNSSet *)touches withEventUIEvent *)event
{
self.touchLocation = [[touches anyObject] locationInView:self];
}
- (void)touchesEndedNSSet *)touches withEventUIEvent *)event
{
[self stopDisplayLink];
}
- (void)startDisplayLink
{
self.displayLink = [CADisplayLink displayLinkWithTarget:self selectorselector(handleDisplayLink];
self.lastTimestamp = CACurrentMediaTime(); // initialize the `lastTimestamp`
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)stopDisplayLink
{
[self.displayLink invalidate];
self.displayLink = nil;
}
- (void)handleDisplayLinkCADisplayLink *)displayLink
{
// figure out the time elapsed, and reset the `lastTimestamp`
CFTimeInterval currentTimestamp = CACurrentMediaTime();
CFTimeInterval elapsed = currentTimestamp - self.lastTimestamp;
self.lastTimestamp = currentTimestamp;
// figure out distance to touch and distance we'd move on basis of velocity and elapsed time
CGFloat distanceToTouch = hypotf(self.touchLocation.y - self.objectPoint.y, self.touchLocation.x - self.objectPoint.x);
CGFloat distanceWillMove = self.velocity * elapsed;
// this does the calculation of the angle between the touch location and
// the current `self.objectPoint`, and then updates `self.objectPoint` on
// the basis of (a) the angle; and (b) the desired velocity.
if (distanceToTouch == 0.0) // if we're already at touchLocation, then just quit
return;
if (distanceToTouch < distanceWillMove) { // if the distance to move is less than the target, just move to touchLocation
self.objectPoint = self.touchLocation;
} else { // otherwise, calculate where we're going to move to
CGFloat angle = atan2f(self.touchLocation.y - self.objectPoint.y, self.touchLocation.x - self.objectPoint.x);
self.objectPoint = CGPointMake(self.objectPoint.x + cosf(angle) * distanceWillMove,
self.objectPoint.y + sinf(angle) * distanceWillMove);
}
[self setNeedsDisplay];
}
要使用它,您需要定义一些属性:
@property (nonatomic) CGFloat velocity;
@property (nonatomic) CGPoint touchLocation;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CFTimeInterval lastTimestamp;
关于ios - 如何继续drawRect : when finger on screen,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/22826244/
|