菜鸟教程小白 发表于 2022-12-12 21:11:12

ios - 如何继续drawRect : when finger on screen


                                            <p><p>我有当前代码:</p>

<pre><code>- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    self.objectPoint = [ locationInView:self];
    float x, y;
    if (self.objectPoint.x &gt; self.objectPoint.x) {
      x = self.objectPoint.x + 1;
    }
    else x = self.objectPoint.x - 1;
    if (self.fingerPoint.y &gt; self.objectPoint.y) {
      y = self.objectPoint.y + 1;
    }
    else y = self.minionPoint.y - 1;
    self.objectPoint = CGPointMake(x, y);

    ;
}
</code></pre>

<p>我的问题是我想让对象跟随您的手指,直到您将手指从屏幕上移开。只有当我的手指移动时它才会跟随。 <code>touchesEnded</code> 仅在我将手指从屏幕上移开时才起作用,所以这也不是我想要的。如何启用可以解决我的问题的功能?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您想触摸屏幕的某个部分,并且只要您的手指一直向下,就想朝那个方向移动绘制的对象,有几种方法。 </p>

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

<ul>
<li><p>在<code>touchesBegan</code>中,捕捉用户在屏幕上触摸的位置并启动<code>CADisplayLink</code>;</p></li>
<li><p>在 <code>touchesMoved</code> 中,您将更新用户的触摸位置(但仅在他们移动手指时调用);</p></li>
<li><p>在 <code>touchesEnded</code> 中,您可能会停止显示链接;和</p></li>
<li><p>在您的 <code>CADisplayLink</code> 处理程序中,您将更新位置(并且您需要知道您希望它移动的速度)。</p></li >
</ul>

<p>所以,这看起来像:</p>

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

    ;
}

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

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    ;
}

- (void)startDisplayLink
{
    self.displayLink = ;
    self.lastTimestamp = CACurrentMediaTime();   // initialize the `lastTimestamp`
    forMode:NSDefaultRunLoopMode];
}

- (void)stopDisplayLink
{
    ;
    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&#39;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&#39;re already at touchLocation, then just quit
      return;
    if (distanceToTouch &lt; distanceWillMove) {    // if the distance to move is less than the target, just move to touchLocation
      self.objectPoint = self.touchLocation;
    } else {                                     // otherwise, calculate where we&#39;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);
    }
    ;
}
</code></pre>

<p>要使用它,您需要定义一些属性:</p>

<pre><code>@property (nonatomic) CGFloat velocity;
@property (nonatomic) CGPoint touchLocation;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CFTimeInterval lastTimestamp;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何继续drawRect : when finger on screen,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22826244/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22826244/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何继续drawRect : when finger on screen