简体   繁体   English

UIScrollView 中的 CALayer 大小错误

[英]CALayer in UIScrollView has a wrong size

I'm trying to zoom UIImageView and UIView , which are inside the UIScrollView , but it doesn't work.我正在尝试放大UIImageViewUIView ,它们位于UIScrollView内部,但它不起作用。

I have an UIImage and CALayer , both of them have an equal size ( 2048×2155 ).我有一个UIImage and CALayer ,它们都有相同的大小( 2048×2155 )。 These components wrapped into UIImageView (image view) and UIView (canvas) and displayed inside UIScrollView.这些组件封装在 UIImageView(图像视图)和 UIView(画布)中,并显示在 UIScrollView 中。

The image view and canvas have the same content scale factor , which equals 2.0 (UIScreen.main.scale), but the canvas has the wrong size (see picture below).图像视图和 canvas 具有相同的content scale factor ,等于 2.0 (UIScreen.main.scale),但 canvas 的大小错误(见下图)。

在此处输入图像描述

CALayer sublayers were built by using CAShapeLayer and UIBezierPath . CALayer 子层是使用CAShapeLayer and UIBezierPath的。

The UIScrollViewDelegate looks next: UIScrollViewDelegate看起来接下来:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    canvas
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    self.scrollView.centerContentView()
}

The center content view looks next (method inside custom UIScrollView):接下来是中心内容视图(自定义 UIScrollView 中的方法):

func centerContentView() {        
    let boundsSize = bounds.size
    var centerFrame = canvas.frame
    
    if centerFrame.size.width < boundsSize.width {
        centerFrame.origin.x = (boundsSize.width - centerFrame.size.width) / 2.0
    } else {
        centerFrame.origin.x = 0
    }
    
    if centerFrame.size.height < boundsSize.height {
        centerFrame.origin.y = (boundsSize.height - centerFrame.size.height) / 2.0
    } else {
        centerFrame.origin.y = 0
    }
    
    imageView.frame = centerFrame
    canvas.frame = centerFrame
}

All my efforts have failed.我所有的努力都失败了。 I tried to transform the CALayer with canvas but to no avail.我试图用 canvas 转换 CALayer 但无济于事。 I will be grateful for any suggestions or help.我将不胜感激任何建议或帮助。 Thank you.谢谢你。

The problem was fixed by using the next lines:通过使用下一行解决了该问题:

let scale = UIScreen.main.bounds.width / imageSize.height
let scaleTransformation = CATransform3DMakeScale(scale, scale, 1)

for layer in layers { 
   layer.transform = scaleTransformation
}

where the imageSize equals to the 2048×2155 and everything works fine.其中imageSize等于2048×2155并且一切正常。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM