0
我無法在選擇didSelectItemAt時將UICollectionViewCell展開爲全屏幕的寬度和高度。第一個單元格在完成選擇後放大屏幕後完美展開,但其他單元格展開。在滾動CollectionView中的單元格時,好像X和Y座標不爲0。試圖讓這個工作在斯威夫特。任何幫助是極大的讚賞。當在UICollectionView中選擇時將單元格展開爲全屏幕
struct itemStruct
{
// Set your values here...
internal let itemWidth: CGFloat = 200
internal let itemHeight: CGFloat = 300
internal let minimumInteritemSpacing: CGFloat = 30
internal let animationDuration: Double = 0.2
internal let transformScale = CGAffineTransform(scaleX: 0.8, y: 0.8)
}
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
private let options = itemStruct()
private var cv: UICollectionView!
private var isfirstTimeTransform: Bool = true
override func viewDidLoad()
{
super.viewDidLoad()
self.initViews()
}
// MARK: - UI
private func initViews()
{
// Collection View Layout
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: options.itemWidth, height: options.itemHeight)
layout.minimumLineSpacing = options.minimumInteritemSpacing
let horizontalInset: CGFloat = (self.view.frame.size.width - options.itemWidth)/2
let verticalInset: CGFloat = (self.view.frame.size.height - options.itemHeight)/2
layout.sectionInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset)
// Collection View
cv = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), collectionViewLayout: layout)
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
cv.backgroundColor = UIColor.darkGray
cv.alwaysBounceVertical = false
cv.isPagingEnabled = true
cv.delegate = self
cv.dataSource = self
self.view.addSubview(cv)
}
// MARK: UICollectionView Data Source
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.white
if indexPath.row == 0 && isfirstTimeTransform
{
isfirstTimeTransform = false
}
else
{
cell.transform = options.transformScale
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = cv.cellForItem(at: indexPath) // or whatever you collection view cell class name is.
UIView.animate(withDuration: 1.0, animations: {
self.view.bringSubview(toFront: self.cv)
collectionView.bringSubview(toFront: item!)
item?.frame.origin = self.view.frame.origin /// this view origin will be at the top of the scroll content, you'll have to figure this out
item?.frame.size.width = self.view.frame.width
item?.frame.size.height = self.view.frame.height
})
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
let pageWidth: CGFloat = options.itemWidth + options.minimumInteritemSpacing; // width + space
let currentOffset: CGFloat = scrollView.contentOffset.x;
let targetOffset: CGFloat = targetContentOffset.pointee.x;
var newTargetOffset: CGFloat = 0
if targetOffset > currentOffset
{
newTargetOffset = ceil(currentOffset/pageWidth) * pageWidth
}
else
{
newTargetOffset = floor(currentOffset/pageWidth) * pageWidth
}
if newTargetOffset < 0
{
newTargetOffset = 0
}
else if newTargetOffset > scrollView.contentSize.width
{
newTargetOffset = scrollView.contentSize.width
}
targetContentOffset.pointee.x = currentOffset
scrollView.setContentOffset(CGPoint(x: newTargetOffset, y: 0), animated: true)
var index = Int(newTargetOffset/pageWidth)
if index == 0
{
// If first index
var cell = self.cv.cellForItem(at: IndexPath(row: index, section: 0))
UIView.animate(withDuration: options.animationDuration, animations: {
cell?.transform = .identity
})
cell = self.cv.cellForItem(at: IndexPath(row: index + 1, section: 0))
UIView.animate(withDuration: options.animationDuration, animations: {
cell?.transform = self.options.transformScale
})
}
else
{
var cell = self.cv.cellForItem(at: IndexPath(row: index, section: 0))
UIView.animate(withDuration: options.animationDuration, animations: {
cell?.transform = .identity
})
index -= 1 // Left
cell = self.cv.cellForItem(at: IndexPath(row: index, section: 0))
UIView.animate(withDuration: options.animationDuration, animations: {
cell?.transform = self.options.transformScale
})
index += 2 // Right
cell = self.cv.cellForItem(at: IndexPath(row: index, section: 0))
UIView.animate(withDuration: options.animationDuration, animations: {
cell?.transform = self.options.transformScale
})
}
}
}