在初始View Controller的实例化时以编程方式绘制UIImageView边界

时间:2015-09-18 18:53:15

标签: ios swift uiimageview uicollectionviewcell xcode6.4

我在.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()

}

最后,这是我目前尝试的解决方案:在didSelectItemAtIndexPath函数中包含viewDidLoad()函数调用。

//Initialize the view with the 0th indexed pot's data
    collectionView(collectionView, didSelectItemAtIndexPath: NSIndexPath(forItem: currentPotIndex, inSection: 0))
    self.dataView.layoutIfNeeded()

我会附上图片,但我没有足够的代表。 谢谢你的期待!

1 个答案:

答案 0 :(得分:0)

这是我为解决问题所做的工作:

覆盖了函数viewDidAppear(animated: Bool) 并将动画UIImageViews的行为放入此函数中。我猜这是有效的,因为无限制的图像是在功能之后加载的,这些功能在超视图中解决了它们的大小和位置。