我正在尝试创建代码来绘制用户用手指绘制的内容。为此,我使用了以下代码:
-(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并不是每一个像素都被调用,所以每个点和下一个点之间有很大的差距。所以我需要以某种方式填补这些空白。有没有人可以帮我写一些代码呢?
提前谢谢。
发布于 2011-10-21 22:57:52
不再需要回答了,我回答了我自己的问题:
然而,对于任何想要答案的人,这里有代码:
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;
}
}我添加了一个名为last touch的点来记录最后一个点,然后添加一个(for)循环来填充当前点和最后一个点之间的空格。
https://stackoverflow.com/questions/7845059
复制相似问题