使用 TouchMoved 进行绘图

发布于 2024-12-10 20:31:08 字数 759 浏览 1 评论 0原文

我正在尝试创建代码来绘制用户用手指绘制的内容。我为此使用了以下代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
    [myImage setHidden:YES];
}
CGPoint currentTouch = [touch locationInView:self.view];
if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) {
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"dot.png"]];
[self.view addSubview:myImage];
}

但问题是,touchesMoved 不会被每个像素调用,因此每个点与下一个点之间存在很大的差距。所以我需要以某种方式填补这些空白。有人可以帮我用一些代码来做到这一点吗?

提前致谢。

I'm trying to create code for drawing what the user draws with his finger. I used the following code for this:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
    [myImage setHidden:YES];
}
CGPoint currentTouch = [touch locationInView:self.view];
if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) {
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"dot.png"]];
[self.view addSubview:myImage];
}

But the problem is that the touchesMoved doesn't get called every single pixel, so there is a big gap between every point and the next. So I need to somehow fill those gaps. Can someone help me do that with some code?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-12-17 20:31:08

不再需要答案了,我回答了我自己的问题:p

对于任何想要答案的人,这里是代码:

UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];

if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) {
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"dot.png"]];
    myImage.tag = tag;
[self.view addSubview:myImage];
[myImage release];
    if (!CGPointEqualToPoint(lastTouch, CGPointMake(0, 0))) { 
        CGPoint nextPoint;
            for (double h = 0.0; h<25.0; h++) {
            double blend = h/25;
            nextPoint.x = currentTouch.x + (blend * (lastTouch.x - currentTouch.x));
            nextPoint.y = currentTouch.y + (blend * (lastTouch.y - currentTouch.y));
            myImageRect = CGRectMake(nextPoint.x, nextPoint.y, 5.0f, 5.0f);
            myImage = [[UIImageView alloc] initWithFrame:myImageRect];
            [myImage setImage:[UIImage imageNamed:@"dot.png"]];
                myImage.tag = tag;
            [self.view addSubview:myImage];
            [myImage release];
    }

    }
lastTouch = currentTouch;
}
}

我添加了一个名为“最后一次触摸”的点来记录最后一个点,然后添加一个(for)循环来填充当前点和最后一个点之间的空格。

No need for an answer anymore, I answered my own question :p

For anyone who wants the answer, though, here's the code:

UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];

if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) {
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"dot.png"]];
    myImage.tag = tag;
[self.view addSubview:myImage];
[myImage release];
    if (!CGPointEqualToPoint(lastTouch, CGPointMake(0, 0))) { 
        CGPoint nextPoint;
            for (double h = 0.0; h<25.0; h++) {
            double blend = h/25;
            nextPoint.x = currentTouch.x + (blend * (lastTouch.x - currentTouch.x));
            nextPoint.y = currentTouch.y + (blend * (lastTouch.y - currentTouch.y));
            myImageRect = CGRectMake(nextPoint.x, nextPoint.y, 5.0f, 5.0f);
            myImage = [[UIImageView alloc] initWithFrame:myImageRect];
            [myImage setImage:[UIImage imageNamed:@"dot.png"]];
                myImage.tag = tag;
            [self.view addSubview:myImage];
            [myImage release];
    }

    }
lastTouch = currentTouch;
}
}

I added a point called last touch to record the last point, and then a (for) loop to fill the spaces between the current point and the last one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文