Ios 自定义UICollectionViewCell内容视图动画
如果用户触摸Ios 自定义UICollectionViewCell内容视图动画,ios,objective-c,animation,uicollectionview,uicollectionviewcell,Ios,Objective C,Animation,Uicollectionview,Uicollectionviewcell,如果用户触摸UICollectionViewCell,我会更改其高度。为了实现这一点,我使用性能更新:更改基础数据并使用标准单元格动画。这非常有效,并且使用标准的“增长和收缩”动画对更改设置动画 此外,我还使用reloadSection更新该部分中单元格的所有子视图。这是我的密码: [collectionView performBatchUpdates:^{ dataHelper.isCellExpanded = !dataHelper.isCellExpanded; } comple
UICollectionViewCell
,我会更改其高度。为了实现这一点,我使用性能更新:
更改基础数据并使用标准单元格动画。这非常有效,并且使用标准的“增长和收缩”动画对更改设置动画
此外,我还使用reloadSection
更新该部分中单元格的所有子视图。这是我的密码:
[collectionView performBatchUpdates:^{
dataHelper.isCellExpanded = !dataHelper.isCellExpanded;
} completion:^(BOOL finished) {
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}];
我需要为扩展单元状态实现子视图的灵活重新排序。所以我不使用自动布局。我只更改子视图帧,并希望将帧更改与UICollectionViewCell
动画一起设置动画
问题是单元格的contentView
内的子视图在没有动画的情况下更新,并且在单元格的高度更改动画完成后。但是我想设置单元格子视图的动画,同时设置高度变化的动画。如何实现这一点
更新
myUICollectionViewCell
中的自定义动画方法如下所示:
- (void)animate {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^
{
CGRect labelFrame = self.testLabel.frame;
labelFrame.origin.y += 70;
self.testLabel.frame = labelFrame;
}
completion:nil];
}
我在我的UICollectionViewCell
子类的initWithFrame:
中创建testLabel
,如下所示:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGRect cellFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
// content
_testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 20, frame.size.width - 20, 20)];
_testLabel.backgroundColor = [UIColor redColor];
_testLabel.textColor = [UIColor blackColor];
_testLabel.text = @"test";
[self.contentView addSubview:_testLabel];
....
您必须在“集合视图”单元中实现动画方法,并且需要从PerformBatChUpdate方法调用该方法
[collectionView performBatchUpdates:^{
dataHelper.isCellExpanded = !dataHelper.isCellExpanded;
// Access the cells you want to animate
yourCustomCell *cell = (yourCustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell animate];
} completion:^(BOOL finished) {
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}];
在cell animate方法中,您可以实现您可能需要的所有动画。我可以通过将弹簧和支柱添加到
contentView
和UICollectionViewCell
子类中的autoresizingMask
来解决这个问题
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGRect cellFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// content
_testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, frame.size.height - 20, frame.size.width - 20, 20)];
_testLabel.backgroundColor = [UIColor redColor];
_testLabel.textColor = [UIColor blackColor];
_testLabel.text = @"You rock";
_testLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.contentView addSubview:_testLabel];
旁注:
我曾尝试使用AutoLayout来解决此问题,但始终无法使其正常工作。这没有任何作用。我在单元格中添加了一个
animate
方法。在这个方法中,我调用一个标准的animateWithDuration
。代码被调用,但没有动画。动画实现看起来如何?