Phi*_*ler 1 objective-c uicollectionview uicollectionviewlayout swift
我正在将我的应用程序重写为Swift,到目前为止一切正常.但最近我试图翻译UICollectionViewFlowLayout子类,我遇到了一些问题.我无法理解如何翻译此类的layoutAttributesForElementsInRect方法...我只是得到了太多错误.这个课程的问题在于它是我没写过的单一课程.我从互联网上下载了这个类,所以我不明白它背后的逻辑.谁能帮我?这是完整的类,但我只需要layoutAttributesForElementsInRect方法,因为我已经成功翻译了第二个(layoutAttributesForItemAtIndexPath).哦,是的!布局基本上左对齐相应集合视图中的每个单元格!谢谢你的建议!
#import "IYAlignLeftLayout.h"
const NSInteger kMaxCellSpacing = 30;
@implementation IYAlignLeftLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
if (nil == attributes.representedElementKind) {
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* currentItemAttributes =
[super layoutAttributesForItemAtIndexPath:indexPath];
UIEdgeInsets sectionInset = UIEdgeInsetsMake(50, 20, 50, 20);
if (indexPath.item == 0) { // first item of section
CGRect frame = currentItemAttributes.frame;
frame.origin.x = sectionInset.left; // first item of the section should always be left aligned
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width + kMaxCellSpacing;
CGRect currentFrame = currentItemAttributes.frame;
CGRect strecthedCurrentFrame = CGRectMake(0,
currentFrame.origin.y,
self.collectionView.frame.size.width,
currentFrame.size.height);
if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) { // if current item is the first item on the line
// the approach here is to take the current frame, left align it to the edge of the view
// then stretch it the width of the collection view, if it intersects with the previous frame then that means it
// is on the same line, otherwise it is on it's own new line
CGRect frame = currentItemAttributes.frame;
frame.origin.x = sectionInset.left; // first item on the line should always be left aligned
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
CGRect frame = currentItemAttributes.frame;
frame.origin.x = previousFrameRightPoint;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
@end
Run Code Online (Sandbox Code Playgroud)
试试这个代码.
override func layoutAttributesForElementsInRect(rect: CGRect) -> AnyObject[]!
{
var attributesToReturn:UICollectionViewLayoutAttributes[] = super.layoutAttributesForElementsInRect(rect) as UICollectionViewLayoutAttributes[]
for attributes in attributesToReturn {
if let elemedKind = attributes.representedElementKind {
var indexPath: NSIndexPath = attributes.indexPath;
attributes.frame = self.layoutAttributesForItemAtIndexPath(indexPath).frame
}
}
return attributesToReturn;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3072 次 |
| 最近记录: |