• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:11:12 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有当前代码:

- (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-推荐答案


如果您想触摸屏幕的某个部分,并且只要您的手指一直向下,就想朝那个方向移动绘制的对象,有几种方法。

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap