【发布时间】:2018-08-18 05:47:15
【问题描述】:
我有一个应用程序,用户可以在其中将形状放置到图像上。图像位于设置为 AspectFit 的 UIImageView 中。 UIImageView 位于 UIScrollView 中以允许缩放和平移。形状是添加到 UIImageView 的 UIView。除非设备方向发生变化,否则一切正常。由于图像设置为 AspectFit 并且方向发生变化,因此纵横比也会发生变化。因此,我的形状不会在图像上保持相同的位置。
ViewController 代码:
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = markupUIImage;
scrollView.maximumZoomScale=5;
scrollView.minimumZoomScale=0.5;
scrollView.bounces=true;
scrollView.bouncesZoom=true;
scrollView.contentSize=CGSize(width: imageView.frame.size.width, height: imageView.frame.size.height);
scrollView.showsHorizontalScrollIndicator=true;
scrollView.showsVerticalScrollIndicator=true;
scrollView.delegate=self;
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return self.imageView
}
形状类:
class ShapeUIView: UIView {
override public class var layerClass: Swift.AnyClass {
get {
return CAShapeLayer.self;
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeView()
}
private func initializeView(){
let shapeLayer = (layer as! CAShapeLayer);
shapeLayer.opacity = 1.0
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineWidth = 2.0
shapeLayer.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor
shapeLayer.fillColor = UIColor(red: 0, green: 128, blue: 0, alpha: 0.3).cgColor
self.isUserInteractionEnabled = true;
self.alpha = 1.0;
self.isHidden = false;
}
public func setPath(_ path: UIBezierPath) {
(layer as! CAShapeLayer).path = path.cgPath;
}
}
如何让形状在旋转后保持在图像上的相同位置?
【问题讨论】:
-
您是否需要将形状保留为 UIView,以防您不在乎。您可以在 UIView 内添加图像视图,并且每次向图像添加形状时,您都会从 UIView 内部的内容创建图像 - 在这种情况下,将是图像视图和正确位置的形状 - 并用新的替换图像视图的图像图片。
-
我需要将形状保留为 UIView。我尝试过这样的事情,但是我正在处理的图像相当大,并且在创建带有形状的图像时存在延迟,并且在创建如此大的图像时遇到了内存问题
标签: ios swift xcode cocoa-touch