バックグラウンド スレッドで for ループを実行するようにこのコードを変換しようとしています。
-(void)prepareLayout {
[super prepareLayout];
if (!_animator) {
_animator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
CGSize contentSize = [self collectionViewContentSize];
NSArray *items = [super layoutAttributesForElementsInRect:CGRectMake(0, 0, contentSize.width, contentSize.height)];
for (UICollectionViewLayoutAttributes *item in items) {
UIAttachmentBehavior *spring = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:item.center];
spring.length = 0;
spring.damping = self.springDamping;
spring.frequency = self.springFrequency;
[_animator addBehavior:spring];
}
}
}
私はこれを試しましたが、正しく動作しません...ラグは解消されますが、一部の行が欠落しているか、コレクションビューの奇妙な位置にあります.forループがディスパッチで正しく動作していません... - (void)prepareLayout { [super prepareLayout];
if (!_animator) {
_animator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
CGSize contentSize = [self collectionViewContentSize];
NSArray *items = [super layoutAttributesForElementsInRect:CGRectMake(0, 0, contentSize.width, contentSize.height)];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
for (UICollectionViewLayoutAttributes *item in items) {
UIAttachmentBehavior *spring = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:item.center];
spring.length = 0;
spring.damping = self.springDamping;
spring.frequency = self.springFrequency;
[_animator addBehavior:spring];
}
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
}
}
他のバリエーションを試しましたが、うまくいきませんでした...誰かがこのコードをバックグラウンドスレッドで適切に実行するように変換するのを手伝ってくれると助かります...ありがとう