[英]How to adjust UIImageView margin programmatically with Swift
I have a UIImageView as part of a UICollectionViewCell that I would like to use to visually display a quantity (like a progress bar). 我有一个UIImageView作为UICollectionViewCell的一部分,我想用它来可视地显示数量(例如进度条)。 In this case the ImageView and its offset represent the percentage remaining of a given item represented by the cell.
在这种情况下,ImageView及其偏移量表示该单元格表示的给定项目的剩余百分比。
Imagine an image of water inside a glass. 想象一下玻璃杯中水的图像。 When the glass is full you see all of the water texture.
当杯子装满时,您会看到所有的水纹理。 When the glass is half full, you see half of the water image.
当杯子装满一半时,您会看到一半的水图像。 so on and so forth...
等等等等...
I want to adjust the top margin of the ImageView based on the percentage remaining. 我想根据剩余百分比调整ImageView的上边距。 for example:
例如:
var image : UIImageView
var item = inventoriedItem(percentageRemaining : 100)
var pctLeft = item.percentageRemaining
pctLeft = 42
// PSEUDOCODE:
image.topMarginOffset = 100 - pctLeft
You could try adjusting the frame of the UIImageView based on the percentage left. 您可以尝试根据剩余百分比调整UIImageView的框架。
var image = UIImageView(frame: CGRect(x: 20, y: 10, width: 50, height: 100))
var pctLeft = 0.42
//assuming you want a vertical progress bar (like a glass of water).
image.frame = CGRect(x: 20, y: 10 + (100 * (1 - pctLeft)), width: 50, height: 100 * pctLeft)
Here I programmatically push the Y value down based on the reduced size of the bar. 在此,我根据条形的减小大小以编程方式将Y值向下推。 Let me know if this is what you were looking for...
让我知道这是否是您想要的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.