0
다음 FLOWLayout을 Objective C에서 Swift 3으로 변환하려고합니다. 불행히도 Null 오류가 발생합니다. 누구나 이미 비슷한 코드를 구현했거나이 코드를 Swift 3으로 변환 했습니까? 여기 Springy Collection View LibrarySpringyCollectionView를 Swift로 변환
는 내가 지금까지 무엇을 가지고 있습니다 :
override func prepare() {
super.prepare()
// Need to overflow our actual visible rect slightly to avoid flickering.
let visibleRect = self.collectionView!.bounds.insetBy(dx: -100, dy: -100)
let itemsInVisibleRectArray: NSArray = super.layoutAttributesForElements(in: visibleRect)! as NSArray
let itemsIndexPathsInVisibleRectSet: NSSet = NSSet(array: itemsInVisibleRectArray.value(forKey: "indexPath") as! [AnyObject])
// Step 1: Remove any behaviours that are no longer visible.
let noLongerVisibleBehaviours = (self.dynamicAnimator.behaviors as NSArray).filtered(using: NSPredicate(block: {behaviour, bindings in
let currentlyVisible: Bool = itemsIndexPathsInVisibleRectSet.member((behaviour as! UIAttachmentBehavior).items.first!) != nil
return !currentlyVisible
}))
for (_, obj) in noLongerVisibleBehaviours.enumerated() {
self.dynamicAnimator.removeBehavior(obj as! UIDynamicBehavior)
self.visibleIndexPathsSet.remove((obj as! UIAttachmentBehavior).items.first!)
}
// Step 2: Add any newly visible behaviours.
// A "newly visible" item is one that is in the itemsInVisibleRect(Set|Array) but not in the visibleIndexPathsSet
let newlyVisibleItems = itemsInVisibleRectArray.filtered(using: NSPredicate(block: {item, bindings in
let currentlyVisible: Bool = self.visibleIndexPathsSet.member(item!) != nil
return !currentlyVisible
}))
let touchLocation: CGPoint = self.collectionView!.panGestureRecognizer.location(in: self.collectionView)
for (_, item) in newlyVisibleItems.enumerated() {
let springBehaviour: UIAttachmentBehavior = UIAttachmentBehavior(item: item as! UIDynamicItem, attachedToAnchor: (item as AnyObject).center)
springBehaviour.length = CGFloat(kLength)
springBehaviour.damping = CGFloat(kDamping)
springBehaviour.frequency = CGFloat(kFrequence)
let zeropoint = CGPoint(x: 0,y :0)
// If our touchLocation is not (0,0), we'll need to adjust our item's center "in flight"
if (!zeropoint.equalTo(touchLocation)) {
let yDistanceFromTouch = fabsf(Float(touchLocation.y - springBehaviour.anchorPoint.y))
let xDistanceFromTouch = fabsf(Float(touchLocation.x - springBehaviour.anchorPoint.x))
let scrollResistance = (yDistanceFromTouch + xDistanceFromTouch)/Float(kResistence)
let item = springBehaviour.items.first as! UICollectionViewLayoutAttributes
var center = item.center
if self.latestDelta < 0 {
center.x += max(self.latestDelta, self.latestDelta * CGFloat(scrollResistance))
} else {
center.x += min(self.latestDelta, self.latestDelta * CGFloat(scrollResistance))
}
item.center = center
}
self.dynamicAnimator.addBehavior(springBehaviour)
self.visibleIndexPathsSet.add(item)
}
}
나는 여기에 첫 번째 오류를 얻을 :
좋아요, 더 이상 오류가 없습니다. 하지만 내 Collectionview 내에서 애니메이션이 발생하지 않는, 일반적인 흐름 레이아웃처럼 보인다. –
그것은 나를 위해 작동합니다. 내가 사용했던 모든 코드에 대한 링크로 내 대답을 업데이트했습니다. – beyowulf
고맙습니다. 코드를 복사하면 매력처럼 작동합니다! –