更改 UICollectionViewCell 子视图框架

Change UICollectionViewCell subview frame

我有一个带有多个子视图的 UICollectionView subclass。单元格 class 包含一个方法,该方法应该导致单元格(timerView)中的一个子视图动画。问题在于,尽管 NSLog() 语句表明框架正在更改,但什么也没有发生。子视图的框架没有变化。我怎样才能使子视图的框架发生变化?

UIView *superView = self.timerView.superview;

//Force the border view to layout so we can get the size
[superView layoutIfNeeded];

NSLog(@"%@", NSStringFromCGRect(superView.frame));


self.timerView.frame = CGRectMake(0, 0, 0, superView.frame.size.height);

NSLog(@"%@", NSStringFromCGRect(self.timerView.frame));

[UIView animateWithDuration:duration animations:^{
    self.timerView.frame = self.timerView.superview.frame;
} completion:^(BOOL finished) {
    if (completion) {
        completion();
    }
}];

呃,答案很简单,但我花了几个小时才算出来。我正在使用 collectionView: cellForItemAtIndexPath:collectionView: didSelectItemAtIndexPath: 中检索对目标 UICollectionViewCell 的引用。这意味着被引用的单元格并不总是我认为的单元格,因为出列。最后,我从单元格创建了一个自定义委托回调,这样我就可以拥有自己的 didSelectItem... 方法,该方法也传递了单元格本身。这样我就知道我正在处理的单元格永远是我认为的单元格,然后动画开始工作。

当我开始遇到属性问题时我才意识到这一点,我知道当从其他方法访问时我已经设置为 nil。所以我意识到一个新的单元格正在出队。

感谢大家的帮助,很抱歉这有点徒劳。