2015-09-18 7 views
1

У меня есть UIImageView в файле .xib, который должен иметь свой размер, когда пользователь выбирает ячейку коллекции. Файл .xib загружается в контроллер просмотра и имеет IBOutlet, соединяющий два. В текущем методе я хорошо работал с UIView, но не с UIImageView.Рисование границ UIImageView программно при создании исходного экземпляра View Controller

Код не работает только при первой загрузке экрана. Если пользователь выбирает любую ячейку коллекции, обновления 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() 

Я бы приложил изображение, но мне не хватает репутации. Спасибо, что посмотрели!

+0

Правильно ли это, если вы его не оживляете? –

+0

@GregPrice это не к сожалению. – sl0anage

ответ

0

Вот что я сделал, чтобы решить эту проблему:

переписал функцию viewDidAppear(animated: Bool) и поставить поведение, анимированное UIImageViews в эту функцию. Я думаю, это сработало, потому что без ограничений изображения загружались после функции, которая учитывала их размер и положение в своем супервизоре.

Смежные вопросы