我想要做的是将手指在屏幕上移动(touchesMoved)并沿touchesMoved生成的点绘制均匀间隔的图像(可能是CGImageRefs)。我可以画线,但我想要生成的东西看起来像这样(对于这个例子,我使用的是一个箭头的图像,但它可能是任何图像,可能是我的狗的图片:))主要的是当用iPhone或iPad上的手指进行绘图时,图像均匀分布。在iOS中沿着路径均匀地绘制图像
回答
假设您已有追踪用户的触摸,因为他们把他们的触摸屏幕上的代码,这听起来像你想,当他们搬到等于图像的长度的距离来检测,此时你想在他们的触摸下绘制另一个图像副本。
要做到这一点,我想你将需要:
- 计算图像的长度(宽度)
- 执行代码来绘制图像的拷贝到您的视图,旋转到任何角度 每次用户的触摸移动时(例如在touchesMoved :)中:
- 每次移动触摸时计算触摸的增量,并生成该增量的“长度”(例如像sqrt(dx^2 + dy^2))
- 累积距上一幅图像的距离
- 如果距离已达到图像的长度,请在触摸的当前位置下绘制图像的副本,并进行适当旋转(可能根据从最后一个图像到当前位置)
你觉得如何?
首先是巨大的道具出去肯德尔。所以,根据他的回答,这里是取UIImage的代码,根据触摸之间的距离在屏幕上沿着路径绘制它(不是真实的路径参考,只是由点创建的逻辑路径),然后旋转图像正确地基于当前点和前一点的VECTOR。我希望你喜欢它:
首先你需要加载图像被一遍又一遍用作CGImage:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"arrow.png" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
image = CGImageRetain(img.CGImage);
请确保您的dealloc您致电
CGImageRelease(image);
然后在touchesBegan中,将起始点存储在方法外的var中(在此标头中声明它:)在这种情况下,我将绘制到UIView中
然后在触摸开始:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self];
}
终于在touchesMoved,绘制位图到屏幕上,然后当你的距离已经移动到足以(以我的情况73,因为我的图像是73像素x 73像素)将该图像绘制到屏幕上,保存新图像并将lastPoint设置为currentPoint
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self];
double deltaX = lastPoint.x - currentPoint.x;
double deltaY = lastPoint.y - currentPoint.y;
double powX = pow(deltaX,2);
double powY = pow(deltaY,2);
double distance = sqrt(powX + powY);
if (distance >= 73){
lastPoint = currentPoint;
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSaveGState(UIGraphicsGetCurrentContext());
float angle = atan2(deltaX, deltaY);
angle *= (M_PI/180);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 73, 73),[self CGImageRotatedByAngle:image angle:angle * -1]);
CGContextRestoreGState(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
distance = 0;
}
}
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{
CGFloat angleInRadians = angle * (M_PI/180);
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect imgRect = CGRectMake(0, 0, width, height);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bmContext = CGBitmapContextCreate(NULL,
rotatedRect.size.width,
rotatedRect.size.height,
8,
0,
colorSpace,
kCGImageAlphaPremultipliedFirst);
CGContextSetAllowsAntialiasing(bmContext, FALSE);
CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone);
CGColorSpaceRelease(colorSpace);
CGContextTranslateCTM(bmContext,
+(rotatedRect.size.width/2),
+(rotatedRect.size.height/2));
CGContextRotateCTM(bmContext, angleInRadians);
CGContextTranslateCTM(bmContext,
-(rotatedRect.size.width/2),
-(rotatedRect.size.height/2));
CGContextDrawImage(bmContext, CGRectMake(0, 0,
rotatedRect.size.width,
rotatedRect.size.height),
imgRef);
CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
CFRelease(bmContext);
[(id)rotatedImage autorelease];
return rotatedImage;
}
这将创建一个看起来像这样的图像:
将增加以下内容(以便尽量在空隙填补一些修改上面的代码,其中touchesMoved丢失当你快速移动时有一些点:
CGPoint point1 = CGPointMake(100, 200);
CGPoint point2 = CGPointMake(300, 100);
double deltaX = point2.x - point1.x;
double deltaY = point2.y - point1.y;
double powX = pow(deltaX,2);
double powY = pow(deltaY,2);
double distance = sqrt(powX + powY);
distance = 0;
for (int j = 1; j * 73 < distance; j++)
{
double x = (point1.x + ((deltaX/distance) * 73 * j));
double y = (point1.y + ((deltaY/distance) * 73 * j));
NSLog(@"My new point is x: %f y :%f", x, y);
}
要添加以下内容(对上述代码进行一些更改以尝试填充touchesMoved在快速移动时缺少一些点的空白处: – cdasher
感谢您的帮助,我该如何绘制x和y点建议我 – dineshprasanna
uper代码中的drawimage是什么 –
- 1. 在openlayer3中沿着地图的路径绘制折线
- 2. 在iOS中沿路径检测绘图
- 3. 沿路径绘制图案
- 4. 沿着路径绘制重复的图像(平铺)
- 5. 在安卓绘图中,如何沿多边形路径绘制均匀的笔画?
- 6. 沿着曲线路径放置图像
- 7. 沿着曲线绘制动画图像
- 8. 在KineticJS中沿着光标路径绘制线条
- 9. 沿路径绘制Threejs TextGeometry
- 10. 沿着地图上的路径绘制文本(如f.e.街道名称)
- 11. 沿着曲线路径在背景中移动的图像android
- 12. 如何在pygame中沿着路径散布图像?
- 13. 如何沿着路径制作动画
- 14. 沿着贝塞尔路径绘制渐变
- 15. 沿着绘制的路径生成物体?
- 16. 沿着路径的ActionScript 2
- 17. QGraphicsItem沿着路径移动
- 18. 沿着路径滚动Paper.js
- 19. 沿着路径拖动snap.svg
- 20. 在Android上沿着路径动画图像?
- 21. 沿着圆形路径移动图像视图
- 22. 沿着Bezier曲线路径的iPhone移动UI图像视图
- 23. 沿路径绘制平行线 - PHP GD
- 24. WPF绘制箭头沿路径
- 25. 如何在WPF中沿着路径绘制一行加三角形?
- 26. 在android地图上绘制路径
- 27. iOS的 - 如何绘制渐变沿路径
- 28. IOS:使用谷歌地图绘制最佳路线路径
- 29. 约束拖动沿着路径在iphone
- 30. iphone可可:如何沿着路径拖动图像
让我试试吧。我从来没有想过将图像的位置放在距离上。我想我需要添加或调整路径上的点。 – cdasher
正是我在找的东西,会在清理完后发布工作代码。 (你没有脱离你的头顶知道如何计算一个合适的矢量你:) :) – cdasher
想出了矢量谢谢! – cdasher