Подкласс UICollectionViewFlowLayout
и переопределить метод layoutAttributesForElementsInRect:
, чтобы выровнять UICollectionViewCells
:
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* array = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
for(UICollectionViewLayoutAttributes* attributes in array) {
if(CGRectIntersectsRect(attributes.frame, rect)){
CGFloat d = UIInterfaceOrientationIsPortrait(_orientation)?
CGRectGetMidY(visibleRect)-attributes.center.y :
CGRectGetMidX(visibleRect)-attributes.center.x;
CGFloat w = visibleRect.size.width;
CGFloat h = visibleRect.size.height;
CGFloat dRatio = UIInterfaceOrientationIsPortrait(_orientation)? d/(h/2) : d/(w/2);
CGFloat angle = MAX_ANGLE*dRatio; // an angle between 0 and MAX_ANGLE based on proximity to center
CGFloat radians = DEGREES_TO_RADIANS(angle);
debug = 0;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = PERSPECTIVE;
rotationAndPerspectiveTransform = UIInterfaceOrientationIsPortrait(_orientation)?
CATransform3DRotate(rotationAndPerspectiveTransform, radians, 1.0f, 0.0f, 0.0f) :
CATransform3DRotate(rotationAndPerspectiveTransform, radians, 0.0f, 1.0f, 0.0f);
attributes.transform3D = rotationAndPerspectiveTransform;
}
}
return array;
}
Я не редактировал этот код для вас и есть ссылки на макросы и переменные экземпляра так что вам нужно настроить его делай что хочешь. Это было использовано для создания макета стиля покрытия, но принципы будут одинаковыми для вас, но без 3D-бизнеса. Вам нужно установить вставку раздела, чтобы гарантировать, что в потоке будет одна строка или столбец.
Обращаясь к одной и той же проблеме, вы можете указать код? –
@kirtimali Я просто написал пользовательский UIView, так как мне нужна сетка с каждым элементом того же размера. Было всего несколько страниц кода. Не могу поделиться им, извините. – jjxtra