Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拖动并缩放使用UIBezierPath IOS/Swift绘制的矩形_Ios_Swift_Uibezierpath_Cashapelayer - Fatal编程技术网

拖动并缩放使用UIBezierPath IOS/Swift绘制的矩形

拖动并缩放使用UIBezierPath IOS/Swift绘制的矩形,ios,swift,uibezierpath,cashapelayer,Ios,Swift,Uibezierpath,Cashapelayer,我正在开发一个程序,识别图像中的一个矩形,并在该矩形的边界上绘制一条路径。现在我想重新定位该路径,以防它不在正确的位置上。例如,请看这张图片 在这种情况下,我需要拖动路径的角点,并将其重新定位为适合矩形 为了绘制路径,我使用了CAShapeLayer和UIBezierPath。下面是我用来绘制路径的代码 // imgView is the UIImageView which contains the image with the rectangle let line: CAShapeLaye

我正在开发一个程序,识别图像中的一个矩形,并在该矩形的边界上绘制一条路径。现在我想重新定位该路径,以防它不在正确的位置上。例如,请看这张图片

在这种情况下,我需要拖动路径的角点,并将其重新定位为适合矩形

为了绘制路径,我使用了CAShapeLayer和UIBezierPath。下面是我用来绘制路径的代码

// imgView is the UIImageView which contains the image with the rectangle

let line: CAShapeLayer = CAShapeLayer();
line.frame = imgView.bounds; 
let linePath: UIBezierPath = UIBezierPath();

linePath.moveToPoint(CGPointMake(x1, y1);
linePath.addLineToPoint(CGPointMake(x2, y2);
linePath.addLineToPoint(CGPointMake(x3, y3);
linePath.addLineToPoint(CGPointMake(x4, y4);
linePath.addLineToPoint(CGPointMake(x1, y1);
linePath.closePath();

line.lineWidth = 5.0;
line.path = linePath.CGPath;
line.fillColor = UIColor.clearColor().CGColor;
line.strokeColor = UIColor.blueColor().CGColor;

imgView.layer.addSublayer(line);

问题是我试着给UIBezierPath添加一个手势。但我注意到没有这样的事。找不到与此相关的任何信息。有人能告诉我一个完成工作的方法吗。任何帮助都将不胜感激

您是对的,没有办法将手势识别器附加到
UIBezierPath
。手势识别器附加到
UIView
对象,并且
UIBezierPath
不是视图对象

没有内置的机制来实现这一点。你需要自己做。我建议建立一个类家族来处理这个问题。创建一个矩形视图类。它将在内部使用贝塞尔路径,并在顶点上放置4个角点视图,并在每个角点视图上安装平移手势识别器


请注意,Cocoa矩形(CGRects)不能旋转。您需要使用一系列连接的线段,并编写逻辑以使其保持方形。

感谢Duncan的建议。我会试试看。顺便说一句,再次感谢您的快速回复,并对您的回复迟到表示歉意。干杯