美文网首页
iOS collecitonVIewCell 滑动到屏幕中央

iOS collecitonVIewCell 滑动到屏幕中央

作者: LPrison | 来源:发表于2021-12-17 20:01 被阅读0次

iOS collecitonVIewCell 滑动到屏幕中央

  // 获取当前cell的frame  
 let att = self.collectionView.layoutAttributesForItem(at: IndexPath(row: index, section: 0))!
 // 计算出偏移量,当前cell的中心点x
 let offset = CGPoint(x: att.frame.origin.x-LCXScreenWidth/2+att.bounds.width/2, y: 0)
 self.collectionView.setContentOffset(offset, animated: true)

iOS collecitonView 获取某一个cell经过某个位置

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
        /// 获取collecitonView头部的中点
        let point = self.collectionView.superview!.convert(CGPoint(x: scrollView.center.x, y: 0), to: self.collectionView)
        // 转换成经过此处的index
        if let index = self.collectionView.indexPathForItem(at: point){
            
        }
    }

相关文章

网友评论

      本文标题:iOS collecitonVIewCell 滑动到屏幕中央

      本文链接:https://www.haomeiwen.com/subject/dmaifrtx.html