使用 TouchMoved 进行绘图
我正在尝试创建代码来绘制用户用手指绘制的内容。我为此使用了以下代码:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不再需要答案了,我回答了我自己的问题:p
对于任何想要答案的人,这里是代码:
我添加了一个名为“最后一次触摸”的点来记录最后一个点,然后添加一个(for)循环来填充当前点和最后一个点之间的空格。
No need for an answer anymore, I answered my own question :p
For anyone who wants the answer, though, here's the code:
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.