objective-c - Swift 应用迁移问题

标签 objective-c uicollectionview swift uicollectionviewlayout

我正在用 Swift 重写我的应用程序,到目前为止一切正常。但是最近尝试翻译一个UICollectionViewFlowLayout的子类,遇到了一些问题。我无法理解如何翻译此类的 layoutAttributesForElementsInRect 方法......我只是遇到了太多错误。这个类的问题是它是我没有写过的唯一一个。我是从网上下载的类(class),所以我不明白其背后的逻辑。谁能帮我?这是完整的类,但我只需要 layoutAttributesForElementsInRect 方法,因为我已经成功翻译了第二个 (layoutAttributesForItemAtIndexPath) 方法。哦,是的!布局基本上左对齐相应 Collection View 中的每个单元格!谢谢指教!

#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

最佳答案

试试这段代码。

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

关于objective-c - Swift 应用迁移问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24365842/

相关文章:

iphone - 创建地址簿副本

ios - 防止过于频繁地调用委托(delegate)方法

ios - UIPageViewController 中是否可以有多个 UIView

ios - 如何调用嵌入了UICollectionView的UITableViewCell的indexPath.row

Swift loadItem 关闭未运行

ios - 动态改变TableView Cell高度

ios - 在呈现 VC 时保持动画 View 的最后位置

ios - 如何在 UICollectionView 上方添加搜索栏?

ios - 消息不显示 UIActivityViewController 操作项

ios - 上传符号文件时 Firebase iOS 多个错误