停止收藏视图以在滚动时创建新按钮



我有一个问题。我在 CollectionView 单元格中有一个按钮,当它第一次被触摸时会移动。这是"移动"代码:

[UIView animateWithDuration:0.5 animations:^{
    _button.frame = CGRectMake(80,112,30,20);
}];

但是当我向下滚动并再次向上滚动时,在其旧位置再次创建了相同的按钮,我该如何修复它?移动的按钮仍然存在 ->按钮在单元格中两次。以下是按钮的创建方式:

-(UIButton *)button{
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(111,112,30,20);
    [_button setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    _button.backgroundColor = [UIColor clearColor];
    [_button addTarget:self
               action:@selector(decMethod)
         forControlEvents:UIControlEventTouchUpInside];
    return _button;
}

这里是集合视图单元格:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView           cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CHTCollectionViewWaterfallCell *cell =
    (CHTCollectionViewWaterfallCell *)[collectionView     dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER
                                                                           forIndexPath:indexPath];
[cell addSubview:cell.buttonInc];

return cell;
}

谢谢!

当性能需要时,UICollection 将杀死不在屏幕外的单元格。在您遇到这种情况时,您可以创建一个 NSMutableArray 来记录所有单元格的移动历史记录。如果一个单元格已经移动,则将 YES 设置为数组的相应索引。在对按钮进行动画处理之前,只需检查数组以查找它是否已移动。

相关内容

最新更新