Chúng tôi đang cố gắng thiết lập UICollectionView với bố cục tùy chỉnh. Nội dung của mỗi CollectionViewCell sẽ là một hình ảnh. Trên tất cả sẽ có hàng nghìn hình ảnh và khoảng 140-150 hiển thị tại một thời điểm nhất định. Trên một sự kiện hành động có khả năng tất cả các ô sẽ được tổ chức lại ở vị trí và kích thước. Mục tiêu là tạo hiệu ứng cho tất cả các sự kiện đang chuyển động hiện đang sử dụng phương thức performBatchUpdates. Điều này gây ra một thời gian trễ rất lớn trước khi tất cả mọi thứ được hoạt hình.Các vấn đề về hiệu suất UICollectionView trên performBatchUpdates
Điều này đến nay chúng tôi phát hiện ra rằng nội bộ phương pháp bố tríAttributesForItemAtIndexPath được gọi cho mỗi tế bào đơn lẻ (vài nghìn trong tổng số). Ngoài ra, phương thức cellForItemAtIndexPath được gọi cho nhiều ô hơn là thực tế có thể được hiển thị trên màn hình.
Có khả năng nào để nâng cao hiệu suất hoạt ảnh không?
UICollectionViewFlowLayout mặc định không thể cung cấp loại thiết kế mà chúng tôi muốn nhận ra trong ứng dụng. Dưới đây là một số mã của chúng tôi:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RPDataModel *dm = [RPDataModel sharedInstance]; //Singleton holding some global information
NSArray *plistArray = dm.plistArray; //Array containing the contents of the cells
NSDictionary *dic = plistArray[[indexPath item]];
RPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
cell.layer.borderColor = nil;
cell.layer.borderWidth = 0.0f;
[cell loadAndSetImageInBackgroundWithLocalFilePath:dic[@"path"]]; //custom method realizing asynchronous loading of the image inside of each cell
return cell;
}
Các lặp layoutAttributesForElementsInRect khắp các yếu tố thiết layoutAttributes cho allthe yếu tố bên trong rect. Cho-tuyên bố phá vỡ vào ô đầu tiên là qua biên giới được xác định bởi góc dưới bên phải của rect:
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray* attributes = [NSMutableArray array];
RPDataModel *dm = [RPDataModel sharedInstance];
for (int i = 0; i < dm.cellCount; i++) {
CGRect cellRect = [self.rp getCell:i]; //self.rp = custom object offering methods to get information about cells; the getCell method returns the rect of a single cell
if (CGRectIntersectsRect(rect, cellRect)) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[dm.relevanceArray[i][@"product"] intValue] inSection:0];
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attribute.size = cellRect.size;
attribute.center = CGPointMake(cellRect.origin.x + attribute.size.width/2, cellRect.origin.y + attribute.size.height/2);
[attributes addObject:attribute];
} else if (cellRect.origin.x > rect.origin.x + rect.size.width && cellRect.origin.y > rect.origin.y + rect.size.height) {
break;
}
}
return attributes;
}
Mở Layout thay đổi kết quả là khá nhiều như nhau bất kể nếu số lượng tế bào được xác định trong layoutAttributesForElementsInRect bị giới hạn hoặc không .. Hoặc hệ thống nhận các thuộc tính layout cho tất cả các ô trong đó nếu nó không bị giới hạn hoặc nó gọi phương thức layoutAttributesForElementAtIndexPath cho tất cả các ô bị thiếu nếu nó bị giới hạn. Nhìn chung, các thuộc tính cho mỗi ô đơn lẻ đang được sử dụng bằng cách nào đó.
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
RPDataModel *dm = [RPDataModel sharedInstance];
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect cellRect = [self.rp getCell:[dm.indexDictionary[@(indexPath.item)] intValue]];
attribute.size = cellRect.size;
attribute.center = CGPointMake(cellRect.origin.x + attribute.size.width/2, cellRect.origin.y + attribute.size.height/2);
return attribute;
}
Vui lòng đăng mã của bạn từ UICollectionViewLayout của bạn. –