Swift应用程序迁移问题

时间:2014-06-23 12:22:39

标签: objective-c uicollectionview swift uicollectionviewlayout

我将我的应用程序重写为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

3 个答案:

答案 0 :(得分:5)

试试这段代码。

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

答案 1 :(得分:1)

这里是代码;)

override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {

    var attributesToReturn: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElementsInRect(rect) as [UICollectionViewLayoutAttributes]

    for (index, attributes) in enumerate(attributesToReturn) {

        if attributes.representedElementKind != nil {

            var indexPath: NSIndexPath  = attributes.indexPath
            attributes.frame = layoutAttributesForItemAtIndexPath(indexPath).frame
        }
    }
    return attributesToReturn
}

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {

//        let kMaxCellSpacing: CGFloat = 30.0

    let currentItemAttributes: UICollectionViewLayoutAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)

    let sectionInset: UIEdgeInsets = UIEdgeInsetsMake(50.0, 20.0, 50.0, 20.0)

    if indexPath.item == 0 { // first item of section
        var frame: CGRect = currentItemAttributes.frame
        frame.origin.x = sectionInset.left // first item of the section should always be left aligned
        currentItemAttributes.frame = frame
        return currentItemAttributes
    }

    let previousIndexPath: NSIndexPath = NSIndexPath(forItem: indexPath.item - 1, inSection: indexPath.section)
    let previousFrame: CGRect = layoutAttributesForItemAtIndexPath(previousIndexPath).frame
    let previousFrameRightPoint: CGFloat = previousFrame.origin.x + previousFrame.size.width/* + kMaxCellSpacing */

    let currentFrame: CGRect = currentItemAttributes.frame
    let strecthedCurrentFrame: CGRect = CGRectMake(
        0.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
        var frame: CGRect = currentItemAttributes.frame
        frame.origin.x = sectionInset.left // first item on the line should always be left aligned
        currentItemAttributes.frame = frame
        return currentItemAttributes
    }

    var frame: CGRect = currentItemAttributes.frame
    frame.origin.x = previousFrameRightPoint
    currentItemAttributes.frame = frame
    return currentItemAttributes
}

答案 2 :(得分:0)

新的Swift 3语法是:

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    <#code#>
}