iOS collecitonVIewCell 滑动到屏幕中央

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){
            
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容