2014-07-11 60 views
0

我正在嘗試使用UIPanGestureRecognizer自定義可滑動UICollectionViewCell。這裏是我的代碼:UICollectionViewCell在更新時不會更改

在定製UICollectionViewCell

func setup() { 
    attributes = delegate.attributesForCell(self) 
    selectionView = UIView(frame: attributes.frame) 
    self.addSubview(selectionView) 
    let cellPan = UIPanGestureRecognizer(target: self, action: "cellDidPan:") 
    selectionView.addGestureRecognizer(cellPan) 
} 

func cellDidPan(gesture: UIPanGestureRecognizer) { 
    switch gesture.state { 
    case .Began: 
     originalFrame = attributes.frame 
    case .Changed: 
     let translation: CGPoint = gesture.translationInView(selectionView) 
     attributes.frame = CGRect(origin: CGPoint(x: originalFrame.origin.x + translation.x, y: originalFrame.origin.y), size: originalFrame.size) 
     println("t \(translation.x)") 
     println("c \(attributes.center)") 
     println("f \(attributes.frame)") 
    case .Ended: 
     println("nothing here yet") 
    default: 
     println("Unrecognized gesture state") 
    } 
} 

UICollectionViewController(細胞的代表):

func attributesForCell(cell: SwipeableCollectionViewCell) -> UICollectionViewLayoutAttributes { 
    let indexPath = collectionView.indexPathForCell(cell) 
    return collectionView.layoutAttributesForItemAtIndexPath(indexPath) 
} 

,當我遇到我的UICollectionViewCell刷卡,幀記錄正確的改變(除x座標外,所有東西都保持不變,只剩下較小的向左滑動和向右向右滑動),但單元格停留在相同的位置。

我正在使用UICollectionViewFlowLayout,並設置故事板和界面生成器中的所有內容。

+0

如果您已經找到問題的答案,請將其寫下來作爲答案,提供詳細的信息,以便在未來遇到相同問題時幫助其他人。 – Neeku

回答

0

我找到了答案! layoutAttributesForItemAtIndexPath()返回副本的屬性,而不是實際的屬性對象。這意味着修改這些屬性顯然不會做任何事情。而是使用UICollectionViewCell屬性。

相關問題