Swift:在调整自定义单元格大小后,UICollectionView锁定到一个单元格

时间:2016-11-10 13:54:26

标签: ios swift scroll uicollectionview

我通过使用带有两个自定义单元格的UICollectionView来制作自定义出席按钮:Initial button

您可以点击或拖动来选择您的状态。单击单元格时,单元格将扩展为屏幕宽度,但只有您单击的单元格可见。如果你改变主意,你可以滑到我身边。这个功能有效。

我想实现按钮允许滑动如下:

Swiping process

当按下滑动按钮超过阈值时,该按钮会捕捉到您的状态。然而,在拍完之后,我再也无法刷到另一个单元格......当我滑动时,我只能看到它的一小部分:

enter image description here

快照功能只需调整单元格的大小即可完成。我试图使用reloadData()后跟一个scrollTo你刷到的项目。但是改变细胞是不可能的。

任何人都知道如何做到这一点?感谢。

我的代码 - scrollViewDidScroll中的有趣部分:

class testcontroller: UIViewController, UIGestureRecognizerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

var attendingCollectionView: UICollectionView!
var imInCell: imInCustomCollectionCell = imInCustomCollectionCell()
var imOutCell: imOutCustomCollectionCell = imOutCustomCollectionCell()
var hasNotRespondedYet = true
var attendingStatus = false
var attendingButtonWidths = UIScreen.main.bounds.width
var attendingButtonHeight = 100

override func viewDidLoad() {
    super.viewDidLoad()
    setUpCollectionView()
}

func setUpCollectionView(){
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .horizontal
    flowLayout.minimumLineSpacing = 0
    self.attendingCollectionView = UICollectionView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height-self.attendingButtonHeight, width: UIScreen.main.bounds.width, height: self.attendingButtonHeight), collectionViewLayout: flowLayout)
    self.attendingCollectionView.delegate = self
    self.attendingCollectionView.dataSource = self
    self.attendingCollectionView.isPagingEnabled = true
    self.attendingCollectionView.register(imInCustomCollectionCell.self, forCellWithReuseIdentifier: "imInCell")
    self.attendingCollectionView.register(imOutCustomCollectionCell.self, forCellWithReuseIdentifier: "imOutCell")
    self.attendingCollectionView.isUserInteractionEnabled = true
    self.attendingCollectionView.showsHorizontalScrollIndicator = false
    view.addSubview(self.attendingCollectionView)
    //never responeded before
    if self.hasNotRespondedYet {
        self.attendingCollectionView.isScrollEnabled = true
        self.attendingButtonWidths = self.attendingButtonWidths/2
        self.attendingCollectionView.alwaysBounceHorizontal = true
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.row == 0 {
        let custom = attendingCollectionView.dequeueReusableCell(withReuseIdentifier: "imInCell", for: indexPath) as! imInCustomCollectionCell
        imInCell = custom
        return custom
    } else {
        let custom = attendingCollectionView.dequeueReusableCell(withReuseIdentifier: "imOutCell", for: indexPath) as! imOutCustomCollectionCell
        imOutCell = custom
        return custom
    }
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let screenWidth = UIScreen.main.bounds.width
    let delta = scrollView.contentOffset.x/screenWidth
    if delta <= 0{
        if delta < -0.15 {
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
                let cell1 = self.imInCell
                let cell2 = self.imOutCell
                cell1.frame = CGRect(x: 0, y: 0, width: self.attendingButtonWidths*2, height: self.attendingButtonHeight)
                cell1.imInLabel.frame = CGRect(x: screenWidth/2-cell1.imInLabel.frame.width/2, y: cell1.imInLabel.frame.minY, width: cell1.imInLabel.frame.width, height: cell1.imInLabel.frame.height)
                cell2.frame = CGRect(x: UIScreen.main.bounds.width, y:0,width: self.attendingButtonWidths*2,height: self.attendingButtonHeight)
                cell2.imOutLabel.frame = CGRect(x: 10, y: cell2.imOutLabel.frame.minY, width: cell2.imOutLabel.frame.width, height: cell2.imOutLabel.frame.height)
                cell1.imInLabel.textColor = UIColor.white
                }, completion: { (f) in
                    self.attendingCollectionView.isScrollEnabled = true
                    self.attendingCollectionView.isPagingEnabled = true
                    self.hasNotRespondedYet = false
            })
        }
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
    if self.hasNotRespondedYet {
        return CGSize(width: self.attendingButtonWidths, height: attendingCollectionView.frame.size.height)
    } else {
        return CGSize(width: self.attendingButtonWidths, height: attendingCollectionView.frame.size.height)
    }
}

}

class imInCustomCollectionCell: UICollectionViewCell {

var imInLabel: UILabel!
override init(frame: CGRect){
    super.init(frame: frame)
    self.imInLabel = UILabel(frame: self.frame)
    self.imInLabel.text = "I'm in"
    self.imInLabel.sizeToFit()
    self.imInLabel.frame = CGRect(x: self.frame.width/2-self.imInLabel.frame.width/2, y: self.frame.height/2-self.imInLabel.frame.height/2, width: self.imInLabel.frame.width, height: self.imInLabel.frame.height)
    addSubview(self.imInLabel)
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class imOutCustomCollectionCell: UICollectionViewCell {}//similar logic as above

0 个答案:

没有答案