0

질문이 있습니다. 나는 CollectionView Cell 안에 버튼을 가지고 있는데, 처음 터치했을 때 움직인다. 여기에 "이동"코드 :스크롤하면서 새로운 버튼을 만들려면 CollectionView를 중지하십시오.

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

하지만 아래로 스크롤 다시 스크롤 할 때, 옛 자리에 다시 만든 같은 버튼은 어떻게 고칠 수 있나요? 이동 한 버튼은 여전히 ​​있습니다 -> 버튼이 셀에 두 번 있습니다. 여기에 CollectionView 셀

-(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; 
} 

당신을 감사 여기 버튼 만드는 방법입니다!

답변

0

UICollection은 성능 향상을 위해 필요할 때 오프 스크린이 아닌 셀을 죽입니다. 상황에 따라 NSMutableArray를 작성하여 모든 셀 이동 내역을 기록 할 수 있습니다. 하나의 셀이 이미 이동 한 경우 YES를 해당 배열 색인으로 설정하십시오. 버튼을 애니메이션으로 만들기 전에 배열이 이동되었는지 확인하십시오.

+0

작동하지 않습니다. CollectionView는 첫 번째 위치에 새 단추를 무작위로 추가합니다. – seniorbenelli

+0

때문에 collectionView는 성능 문제에 대해 자동으로 셀을 다시 사용합니다. 처음 버튼을 원하지 않으면 버튼이있는 경우 셀의 contentView 하위 뷰를 확인한 다음 제거하십시오. – Jing

+0

괜찮습니다. 고맙습니다 – seniorbenelli

관련 문제