滚动不起作用时,CollectionView FlowLayout重叠 [英] CollectionView FlowLayout overlapping when scrolling not working

查看:89
本文介绍了滚动不起作用时,CollectionView FlowLayout重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你们一切都很好

在这里,我创建了一个集合视图流布局,其中某些场景不起作用,请分享您的参考.这是集合视图

Here I have created a collection view flow layouts some scenarios not working, please share your references. here is the collection view

    import UIKit
    class ViewController: UIViewController {
        
        @IBOutlet weak var collection: UICollectionView!
        private let kCellHeight: CGFloat = UIScreen.main.bounds.width/2+38
        private let kItemSpace: CGFloat = UIScreen.main.bounds.width/2.08
        lazy var expand_click = false
        lazy var index_value = [String]()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            collection.delegate = self
            collection.dataSource = self
            let layout = StickyCollectionViewFlowLayout2()
            layout.minimumLineSpacing = -kItemSpace
            collection.collectionViewLayout = layout
            collection.reloadData()
        }
    }
    
    extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 15
        }    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
            print("Index",indexPath.row)
            return cell
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: view.frame.width-40, height: kCellHeight)
        }
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if (15)-1 == indexPath.row {
                self.showAlert(alertText: "", alertMessage: "last Cell")
            }else{
                if index_value.contains("\(indexPath.row)") {
                    self.showAlert(alertText: "", alertMessage: "Redirect")
                    expand_click = true
                }else{
                    index_value.removeAll()
                    index_value.append("\(indexPath.row)")
                    expand_click = true
                    cellData = indexPath.row
                    collectionView.performBatchUpdates({
                    }, completion: nil)
                }
            }
        }
    }

集合视图单元格

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var container: UIView!
   
    override func layoutSubviews() {
        container.backgroundColor = .white
        container.layer.cornerRadius = 13
        container.layer.shadowRadius = 2
        container.layer.shadowOpacity = 0.7
        container.layer.shadowOffset = CGSize(width: 0, height: 1)
        container.layer.shadowColor = UIColor.black.withAlphaComponent(0.45).cgColor
    }    
}

  • 单击单元格时,它将展开该单元格并移动到其他单元格的下方>*再次单击展开的单元格,它将被重定向到Ex的另一页(我显示了警告)>*最后一次点击它的单元格也应该重定向如果我们有15张卡片,那么我所做的就是我自己做的所有事情,超过50张卡片可以正常工作>滚动不流畅,我们在争夺尺寸方面遇到问题,也许我>无法解决问题

**Collection view FlowLayout**


class StickyCollectionViewFlowLayout2: UICollectionViewFlowLayout {
    
    var firstItemTransform: CGFloat?
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let items = NSArray (array: super.layoutAttributesForElements(in: rect)!, copyItems: true)
        
        print("------------------------------------------------------------------------------------------------------------------------")
        print("Count-------->",items.count,"<--------Count")
        print("------------------------------------------------------------------------------------------------------------------------")
        
        
        var headerAttributes: UICollectionViewLayoutAttributes?
        
        self.firstItemTransform = nil
        if cellData != nil {
            let min = cellData!
            print(min)
            var b = [UICollectionViewLayoutAttributes?]()
            var a = false
            items.forEach({ (object) in
                let object  = object as! UICollectionViewLayoutAttributes
                if min == object.indexPath.row {
                    a = true
                }else{
                    if a == true {
                        b.append(object)
                    }
                }
            })
            for attributes in b {
                if attributes?.representedElementKind == UICollectionView.elementKindSectionHeader {
                    headerAttributes = attributes
                }
                else {
                    self.atributeLayout(attributes!, headerAttributes: headerAttributes)
                }
            }
            cellData = nil
        }else{
            items.enumerateObjects(using: { (object, idex, stop) -> Void in
                let attributes = object as! UICollectionViewLayoutAttributes
                
                if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
                    headerAttributes = attributes
                }
                else {
                    self.atributeLayoutReset(attributes, headerAttributes: headerAttributes)
                }
            })
        }
        return items as? [UICollectionViewLayoutAttributes]
    }
    
    func atributeLayout(_ attributes: UICollectionViewLayoutAttributes, headerAttributes: UICollectionViewLayoutAttributes?){
        let minY = self.collectionView!.bounds.minY + self.collectionView!.contentInset.top
        var maxY = attributes.frame.origin.y+attributes.frame.height/2+80
        if let headerAttributes = headerAttributes {
            maxY -= headerAttributes.bounds.height
        }
        let finalY = max(minY, maxY)
        var origin = attributes.frame.origin
        let deltaY = (finalY - origin.y) / attributes.frame.height + 100
        
        if let itemTransform = self.firstItemTransform {
            let scale = 1 - deltaY * itemTransform
            attributes.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
        origin.y = finalY
        attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
        attributes.zIndex = attributes.indexPath.row
    }
    func atributeLayoutReset(_ attributes: UICollectionViewLayoutAttributes, headerAttributes: UICollectionViewLayoutAttributes?){
        cellIndex = nil
        if isExpandCell != true  {
            let minY = self.collectionView!.bounds.minY + self.collectionView!.contentInset.top
            var maxY = attributes.frame.origin.y
            if let headerAttributes = headerAttributes {
                maxY -= headerAttributes.bounds.height
            }
            let finalY = max(minY, maxY)
            var origin = attributes.frame.origin
            let deltaY = (finalY - origin.y) / attributes.frame.height + 100
            if let itemTransform = self.firstItemTransform {
                let scale = 1 - deltaY * itemTransform
                attributes.transform = CGAffineTransform(scaleX: scale, y: scale)
            }
            origin.y = finalY
            attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
            attributes.zIndex = attributes.indexPath.row
            //            })
        }else{
            print(collectionView!.contentInset.top,collectionView!.bounds.minY)
            let minY = 0.0 + collectionView!.contentInset.top
            var maxY = attributes.frame.origin.y
            if let headerAttributes = headerAttributes {
                maxY -= headerAttributes.bounds.height
            }
            let finalY = max(minY, maxY)
            var origin = attributes.frame.origin
            let deltaY = (finalY - origin.y) / attributes.frame.height + 100
            if let itemTransform = firstItemTransform {
                let scale = 1 - deltaY * itemTransform
                attributes.transform = CGAffineTransform(scaleX: scale, y: scale)
            }
            origin.y = finalY
            attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
            attributes.zIndex = attributes.indexPath.row
        }
    }
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return false
    }
}

如果有人有过收藏的经历,请分享给我谢谢

if anyone experienced collection view please share me your references Thank you

推荐答案

您好,请尝试我的代码.我已经在iPhone模拟器上进行了测试,效果很好.

Hello please try my code. I have tested on an iPhone simulator and it works great.

import SwiftUI

struct CardView: View {
    
    static let CARD_HEIGHT: CGFloat = 200
    static let CARD_WIDTH: CGFloat = 300
    static let TAB_HEIGHT: CGFloat = 30
    
    @State var selected: Int? = nil
    let cards: Int
    
    var body: some View {
        ScrollView {
            ZStack(alignment: .top) {
                ForEach(0 ..< cards) { card_i in
                    Button {
                        withAnimation {
                            self.selected = self.selected == card_i ? nil : card_i
                        }
                        print("A card was tapped. Do something with card_i here")
                    } label: { self.card }
                    .buttonStyle(PlainButtonStyle())
                    .frame(width: Self.CARD_WIDTH, height: Self.CARD_HEIGHT, alignment: .top)
                    .offset(x: .zero, y: CGFloat(card_i) * Self.TAB_HEIGHT + ((selected ?? cards + 1) < card_i ? Self.CARD_HEIGHT - Self.TAB_HEIGHT / 2 : 0))
                }
            }
            .frame(height: Self.CARD_HEIGHT + Self.TAB_HEIGHT * CGFloat(cards), alignment: .top)
            .padding()
        }
            
    }
    
    var card: some View {
        RoundedRectangle(cornerRadius: 17, style: .continuous)
            .fill(Color.gray)
            .overlay(RoundedRectangle(cornerRadius: 17, style: .continuous).stroke(Color.black, lineWidth: 5))
    }
}

编辑

像这样使用它

let swiftUIview = CardView(cards: 10, onCardTapped: { card in 
    print("card number \(card) tapped")
})

let uiViewController = UIHostingController(rootView: swiftUIview)

由于过时而删除了Gif.

Gif removed because outdated.

这篇关于滚动不起作用时,CollectionView FlowLayout重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆