Search code examples
iosiphoneswift2uicollectionviewlayout

Type of expression is ambiguous | SpringyCollectionViewFlowLayout


As described in this UICollectionViewFlowlayout, Step 1:

// Need to overflow our actual visible rect slightly to avoid flickering.
var visibleRect = CGRectInset(self.collectionView!.bounds, -100, -100)
var itemsInVisibleRectArray: NSArray = super.layoutAttributesForElementsInRect(visibleRect)!
var itemsIndexPathsInVisibleRectSet: NSSet = NSSet(array: itemsInVisibleRectArray.valueForKey("indexPath") as [AnyObject])

// Step 1: Remove any behaviours that are no longer visible.
var noLongerVisibleBehaviours = (self.dynamicAnimator.behaviors as NSArray).filteredArrayUsingPredicate(NSPredicate(block: {behaviour, bindings in
    var currentlyVisible: Bool = itemsIndexPathsInVisibleRectSet.member((behaviour as UIAttachmentBehavior).items.first!.indexPath) != nil
    return !currentlyVisible
}))

"Type of expression is ambiguous without more context" .. what should I do? I am really stuck :/

Thank you for any help I can get on this...


Solution

  • If anyone might just want to solve this problem, look here:

        let visibleRect = CGRectInset(self.collectionView!.bounds, -100, -100)
        let itemsInVisibleRect = super.layoutAttributesForElementsInRect(visibleRect)!
        let indexPathsInVisibleRect = Set(itemsInVisibleRect.map({ (attribute) -> NSIndexPath in
            return attribute.indexPath
        }))
    
        // Step 1: Remove any behaviours that are no longer visible.
    
        let noLongerVisisbleBehaviors = self.dynamicAnimator.behaviors.filter { (eachBehavior) -> Bool in
            var currentlyVisible = false
            if let behavior = eachBehavior as? UIAttachmentBehavior, first = behavior.items.first as? UICollectionViewLayoutAttributes {
                currentlyVisible = indexPathsInVisibleRect.contains(first.indexPath)
            }
            return !currentlyVisible
        }
    

    Thankfully, this has proven to be a truly a huge save for the day :}