我想編程一個可展開的UICollectionViewCell。 如果用戶按下頂部按鈕,單元格應展開到不同的高度並顯示底層內容。展開式UICollectionViewCell
我的第一個實現具有自定義UICollectionViewCell,可以通過單擊頂部按鈕來擴展它,但collectionview不會擴展。自動取款機。該單元格是collectionview中的最後一個,只需向下滑動即可,您可以看到擴展的內容。如果您停止觸摸,則滾動視圖會跳起來,並且看不到擴展的單元格。
這裏是我的代碼:
import Foundation
import UIKit
class ExpandableCell: UICollectionViewCell {
@IBOutlet weak var topButton: IconButton!
public var expanded : Bool = false
private var expandedHeight : CGFloat = 200
private var notExpandedHeight : CGFloat = 50
@IBAction func topButtonTouched(_ sender: AnyObject) {
if (expanded == true) {
self.deExpand()
} else {
self.expand()
}
}
public func expand() {
UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: self.expandedHeight)
}, completion: { success in
self.expanded = true
})
}
public func deExpand() {
UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: self.notExpandedHeight)
}, completion: { success in
self.expanded = false
})
}
}
我也有正確執行的問題下面的方法,因爲我沒有直接引用單元格:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
Basicly一切都應該是動畫。
該屏幕截圖顯示了一個展開和未展開的單元格。希望有所幫助!
謝謝!
不知何故,[APLExpandableCollectionView](https://github.com/apploft/APLExpandableCollectionView)可能是有用的... –