gpt4 book ai didi

ios - 如何继续绘制Rect : when finger on screen

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:15 25 4
gpt4 key购买 nike

我有当前代码:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)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 只有当我的手指离开屏幕时才有效,所以这也不是我想要的。如何启用可以解决我的问题的功能?

最佳答案

如果您想要触摸屏幕的一部分,并且想要在按住手指的情况下沿该方向移动绘制的对象,则有两种方法。

一种方法是使用某种形式的计时器,当用户将手指按住屏幕时,它会重复调用一个方法(因为,正如您所指出的,您只会获得 touchesMoved 当你移动时)。虽然 NSTimer 是您遇到的最常见的计时器,但在这种情况下,您需要使用一个称为显示链接的专用计时器,即 CADisplayLink,它会触发何时可以执行屏幕更新。所以,你会:

  • touchesBegan中,捕获用户在屏幕上触摸的位置并启动CADisplayLink

  • touchesMoved 中,您将更新用户的触摸位置(但仅在他们移动手指时调用);

  • touchesEnded 中,您可能会停止显示链接;和

  • 在您的 CADisplayLink 处理程序中,您需要更新位置(并且您需要知道您希望它移动的速度)。

所以,这看起来像:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.velocity = 100.0; // 100 points per second
self.touchLocation = [[touches anyObject] locationInView:self];

[self startDisplayLink];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchLocation = [[touches anyObject] locationInView:self];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self stopDisplayLink];
}

- (void)startDisplayLink
{
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
self.lastTimestamp = CACurrentMediaTime(); // initialize the `lastTimestamp`
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink
{
[self.displayLink invalidate];
self.displayLink = nil;
}

- (void)handleDisplayLink:(CADisplayLink *)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 - 如何继续绘制Rect : when finger on screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com