在初始视图控制器实例化时以编程方式绘制 ImageView 边界
Drawing UIImageView bounds programatically upon initial View Controller's instantiation
我在 .xib 文件中有一个 UIImageView,当用户选择 Collection View 单元格时需要更改其大小。 .xib 文件被加载到视图控制器中,并有一个 IBOutlet 连接两者。当前的方法我在 UIView 上运行良好,但在 UIImageView 上失败了。
代码仅在首次加载屏幕时失败。如果用户选择任何 Collection View 单元格,UIImageView 就会更新。
func displayDataForCell(index: Int) {
// Set the box fill amount for Anode, Feeder, and Wave
let anodePercentAmount = Double(pot.anode!)/100
let feederPercentAmount = Double(pot.feeder!)/100
let wavePercentAmount = Double(pot.wave!)/100
dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
dataView.waveBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
dataView.feederBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*anodePercentAmount)), width: 120.0, height: (180.0*anodePercentAmount))
self.dataView.feederBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*feederPercentAmount)), width: 120.0, height: (180.0*feederPercentAmount))
self.dataView.waveBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*wavePercentAmount)), width: 120.0, height: (180.0*wavePercentAmount))
})
}
我在函数collectionView(_, didSelectItemAtIndexPath indexPath:_)
方法中调用上面的方法如下:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// This makes sure only one cell can be selected at once
if currentPotIndex != indexPath.row {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.highlighted
let cell0 = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: currentPotIndex, inSection: 0))
cell0?.highlighted = false
}
self.currentPotIndex = indexPath.row
displayDataForCell(currentPotIndex)
bottomCollection.reloadData()
}
最后,这是我目前尝试的解决方案:在 viewDidLoad()
函数中包含 didSelectItemAtIndexPath
函数调用。
//Initialize the view with the 0th indexed pot's data
collectionView(collectionView, didSelectItemAtIndexPath: NSIndexPath(forItem: currentPotIndex, inSection: 0))
self.dataView.layoutIfNeeded()
我会附上一张图片,但我没有足够的代表。
感谢观看!
以下是我为解决该问题所做的工作:
重写函数viewDidAppear(animated: Bool)
并将 UIImageViews 的动画行为放入此函数中。我猜这是可行的,因为不受约束的图像是在解决它们在超级视图中的大小和位置的函数之后加载的。
我在 .xib 文件中有一个 UIImageView,当用户选择 Collection View 单元格时需要更改其大小。 .xib 文件被加载到视图控制器中,并有一个 IBOutlet 连接两者。当前的方法我在 UIView 上运行良好,但在 UIImageView 上失败了。
代码仅在首次加载屏幕时失败。如果用户选择任何 Collection View 单元格,UIImageView 就会更新。
func displayDataForCell(index: Int) {
// Set the box fill amount for Anode, Feeder, and Wave
let anodePercentAmount = Double(pot.anode!)/100
let feederPercentAmount = Double(pot.feeder!)/100
let wavePercentAmount = Double(pot.wave!)/100
dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
dataView.waveBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
dataView.feederBoxFill.frame = CGRect(x: 0.0, y: 180.0, width: 120.0, height: 0.0)
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.dataView.anodeBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*anodePercentAmount)), width: 120.0, height: (180.0*anodePercentAmount))
self.dataView.feederBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*feederPercentAmount)), width: 120.0, height: (180.0*feederPercentAmount))
self.dataView.waveBoxFill.frame = CGRect(x: 0.0, y: (180.0 - (180.0*wavePercentAmount)), width: 120.0, height: (180.0*wavePercentAmount))
})
}
我在函数collectionView(_, didSelectItemAtIndexPath indexPath:_)
方法中调用上面的方法如下:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// This makes sure only one cell can be selected at once
if currentPotIndex != indexPath.row {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.highlighted
let cell0 = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: currentPotIndex, inSection: 0))
cell0?.highlighted = false
}
self.currentPotIndex = indexPath.row
displayDataForCell(currentPotIndex)
bottomCollection.reloadData()
}
最后,这是我目前尝试的解决方案:在 viewDidLoad()
函数中包含 didSelectItemAtIndexPath
函数调用。
//Initialize the view with the 0th indexed pot's data
collectionView(collectionView, didSelectItemAtIndexPath: NSIndexPath(forItem: currentPotIndex, inSection: 0))
self.dataView.layoutIfNeeded()
我会附上一张图片,但我没有足够的代表。 感谢观看!
以下是我为解决该问题所做的工作:
重写函数viewDidAppear(animated: Bool)
并将 UIImageViews 的动画行为放入此函数中。我猜这是可行的,因为不受约束的图像是在解决它们在超级视图中的大小和位置的函数之后加载的。