[英]Setting the size of UIImageView proportionally
I have a question about how to set the size of UIImageView based on iPhone's display size dynamically.我有一个关于如何根据 iPhone 的显示大小动态设置 UIImageView 大小的问题。
If the size of a display of iPhone decreases, I want to make the size of UIImageView decreased proportionally.如果 iPhone 的显示屏尺寸减小,我想让 UIImageView 的尺寸按比例减小。
I'm setting lowerThumbImageView and upperThumbImageView like below for now.我现在正在设置如下所示的lowerThumbImageView 和 upperThumbImageView 。
override init(frame: CGRect) {
super.init(frame: frame)
trackLayer.rangeSlider = self
// contentsScale: CGFloat - The scale factor applied to the layer.
// scale: CGFloat - The natural scale factor associated with the screen.
trackLayer.contentsScale = UIScreen.main.scale
// UIControl -> UIView -> layer
layer.addSublayer(trackLayer)
lowerThumbImageView.image = thumbImageA
addSubview(lowerThumbImageView)
upperThumbImageView.image = thumbImageB
addSubview(upperThumbImageView)
}
// 1
private func updateLayerFrames() {
// insetBy(dx:dy:) - Returns a rectangle that is smaller or larger
// than the source rectangle, with the same center point.
trackLayer.frame = bounds.insetBy(dx: 0.0, dy: bounds.height / 3)
trackLayer.setNeedsDisplay()
lowerThumbImageView.frame = CGRect(origin: thumbOriginForValue(lowerValue),
size: thumbImageA.size)
upperThumbImageView.frame = CGRect(origin: thumbOriginForValue(upperValue),
size: thumbImageB.size)
CATransaction.begin()
CATransaction.setDisableActions(true)
CATransaction.commit()
}
// 2
func positionForValue(_ value: CGFloat) -> CGFloat {
return bounds.width * value
}
// 3
private func thumbOriginForValue(_ value: CGFloat) -> CGPoint {
let x = positionForValue(value) - thumbImageA.size.width / 2.0
return CGPoint(x: x, y: (bounds.height * 0.001 - thumbImageA.size.height) / 2.0)
}
Could you give me your advice?你能给我你的建议吗?
Thanks for reading.谢谢阅读。
You can set size of UIImageView proportional to any other View.您可以将 UIImageView 的大小设置为与任何其他 View 成比例。 Here are the steps to do it using Interface Builder Autolayout Constraints.以下是使用 Interface Builder Autolayout Constraints 执行此操作的步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.