Ios 如何确定;阴影的z指数;

Ios 如何确定;阴影的z指数;,ios,swift,uicollectionview,z-index,shadow,Ios,Swift,Uicollectionview,Z Index,Shadow,我为标题感到抱歉,无法综合该问题。问题是: 在我的UICollectionView中,有一些单元格想要放置阴影,它们彼此排列得非常近,这使得每个单元格的阴影都到达相邻单元格(第一个图像),而我想要的是,它只到达背景(第二个图像) 我的想法或尝试: 我无法将视图放在单元格后面,向其添加帧,并在此视图中应用阴影,因为单元格具有动态移动(UIDynamics CollectionView布局) 我尝试在UICollectionViewLayout的子类中,将所有这些单元格放在同一个z索引中。不起作用

我为标题感到抱歉,无法综合该问题。问题是:

在我的UICollectionView中,有一些单元格想要放置阴影,它们彼此排列得非常近,这使得每个单元格的阴影都到达相邻单元格(第一个图像),而我想要的是,它只到达背景(第二个图像)

我的想法或尝试:

  • 我无法将视图放在单元格后面,向其添加帧,并在此视图中应用阴影,因为单元格具有动态移动(UIDynamics CollectionView布局)
  • 我尝试在UICollectionViewLayout的子类中,将所有这些单元格放在同一个z索引中。不起作用。找出原因:
  • (…)具有相同值的项具有未确定的 秩序


    请帮我解决我的问题。谢谢

    您可以尝试将阴影定义为补充视图,与各自的单元格对齐,并为其提供比单元格更低的z顺序。

    您可以尝试将阴影定义为补充视图,与各自的单元格对齐,并为其提供比单元格更低的z顺序。

    从TheEye响应中,我决定实现ui装饰视图。现在一切都很好

    // MARK: At UICollectionViewCustomLayout:
    
    public override init() {
        super.init()
        // Register the NIB of the view that will hold the shadows:
        let nib = UINib(nibName: "Shadow", bundle: nil)
        self.registerNib(nib, forDecorationViewOfKind: "shadow")
    }
    
    public override func layoutAttributesForDecorationViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let layoutAtt: UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forDecorationViewOfKind: "shadow", withIndexPath: indexPath)
        layoutAtt.frame = (layoutAttributesForItemAtIndexPath(indexPath)?.frame)!
        layoutAtt.zIndex = -1
        return layoutAtt
        }
    
    public override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var atts = [UICollectionViewLayoutAttributes]()
        let numberOfItems:Int = (self.collectionView?.numberOfItemsInSection(0))!
        for index in 0..<numberOfItems {
            let layoutItem = layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0))!
            let frameItem = layoutItem.frame
            if CGRectIntersectsRect(frameItem, rect) {
                atts.append(layoutAttributesForDecorationViewOfKind("shadow", atIndexPath: NSIndexPath(forItem: index, inSection: 0))!)
                atts.append(layoutItem)
            }
        }
        return atts
    }
    
    // MARK: At the Nib Class File:
    
    // Here I created a additional view to append the shadow. 
    // Thats because awakeFromNib() is called before the UICollectionViewCustomLayout 
    // have a chance to layout it, but I want to make 
    // use of shadowPath to gain performance, thats why 
    // I make use of the additional UIView with autolayout stuffs.
    
    @IBOutlet weak var shadowView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        shadowView.layer.shadowOffset = CGSizeMake(0, 0)
        shadowView.layer.shadowColor = UIColor.yellowColor().CGColor
        shadowView.layer.shadowRadius = 3
        shadowView.layer.shadowOpacity = 1
        let shadowFrame: CGRect = shadowView.layer.bounds
        let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
        shadowView.layer.shadowPath = shadowPath
        shadowView.clipsToBounds = false
        self.clipsToBounds = false
    }
    
    //标记:在UICollectionViewCustomLayout:
    公共重写init(){
    super.init()
    //注册将容纳阴影的视图笔尖:
    设nib=UINib(nibName:“Shadow”,bundle:nil)
    self.registerNib(nib,用于类的生态视图:“影子”)
    }
    public override func LayoutAttribute ForDecorationViewOfKind(elementKind:String,atIndexPath indexPath:NSIndexPath)->UICollectionViewLayoutAttribute?{
    让layoutAtt:UICollectionViewLayoutAttributes=UICollectionViewLayoutAttributes(对于类别为“shadow”的ecorationViewOfKind,带indexPath:indexPath)
    layoutAtt.frame=(layoutatributesforItemAtIndexPath(indexPath)?.frame)!
    layoutAtt.zIndex=-1
    返回layoutAtt
    }
    公共覆盖函数layoutAttributesForElementsInRect(rect:CGRect)->[UICollectionViewLayoutAttributes]?{
    var atts=[UICollectionViewLayoutAttribute]()
    让numberOfItems:Int=(self.collectionView?.numberOfItemsInSection(0))!
    
    对于0中的索引,从TheEye响应中,我决定实现
    UIDecorationViews
    。现在一切都很好

    // MARK: At UICollectionViewCustomLayout:
    
    public override init() {
        super.init()
        // Register the NIB of the view that will hold the shadows:
        let nib = UINib(nibName: "Shadow", bundle: nil)
        self.registerNib(nib, forDecorationViewOfKind: "shadow")
    }
    
    public override func layoutAttributesForDecorationViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let layoutAtt: UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forDecorationViewOfKind: "shadow", withIndexPath: indexPath)
        layoutAtt.frame = (layoutAttributesForItemAtIndexPath(indexPath)?.frame)!
        layoutAtt.zIndex = -1
        return layoutAtt
        }
    
    public override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var atts = [UICollectionViewLayoutAttributes]()
        let numberOfItems:Int = (self.collectionView?.numberOfItemsInSection(0))!
        for index in 0..<numberOfItems {
            let layoutItem = layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0))!
            let frameItem = layoutItem.frame
            if CGRectIntersectsRect(frameItem, rect) {
                atts.append(layoutAttributesForDecorationViewOfKind("shadow", atIndexPath: NSIndexPath(forItem: index, inSection: 0))!)
                atts.append(layoutItem)
            }
        }
        return atts
    }
    
    // MARK: At the Nib Class File:
    
    // Here I created a additional view to append the shadow. 
    // Thats because awakeFromNib() is called before the UICollectionViewCustomLayout 
    // have a chance to layout it, but I want to make 
    // use of shadowPath to gain performance, thats why 
    // I make use of the additional UIView with autolayout stuffs.
    
    @IBOutlet weak var shadowView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        shadowView.layer.shadowOffset = CGSizeMake(0, 0)
        shadowView.layer.shadowColor = UIColor.yellowColor().CGColor
        shadowView.layer.shadowRadius = 3
        shadowView.layer.shadowOpacity = 1
        let shadowFrame: CGRect = shadowView.layer.bounds
        let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
        shadowView.layer.shadowPath = shadowPath
        shadowView.clipsToBounds = false
        self.clipsToBounds = false
    }
    
    //标记:在UICollectionViewCustomLayout:
    公共重写init(){
    super.init()
    //注册将容纳阴影的视图笔尖:
    设nib=UINib(nibName:“Shadow”,bundle:nil)
    self.registerNib(nib,用于类的生态视图:“影子”)
    }
    public override func LayoutAttribute ForDecorationViewOfKind(elementKind:String,atIndexPath indexPath:NSIndexPath)->UICollectionViewLayoutAttribute{
    让layoutAtt:UICollectionViewLayoutAttributes=UICollectionViewLayoutAttributes(对于类别为“shadow”的ecorationViewOfKind,带indexPath:indexPath)
    layoutAtt.frame=(layoutatributesforItemAtIndexPath(indexPath)?.frame)!
    layoutAtt.zIndex=-1
    返回layoutAtt
    }
    公共覆盖函数layoutAttributesForElementsInRect(rect:CGRect)->[UICollectionViewLayoutAttributes]{
    var atts=[UICollectionViewLayoutAttribute]()
    让numberOfItems:Int=(self.collectionView?.numberOfItemsInSection(0))!
    
    对于0中的索引,做得很好-但我会更改
    layouttributesforementsinrect
    函数,以检查装饰视图的类型,并返回“阴影”的阴影类型-它更通用,并且在添加更多装饰视图时也能工作。做得好-但是我会更改
    layouttributesforementsinrect
    函数以检查装饰视图的类型并返回“shadow”类型的阴影-它更通用,并且在添加更多装饰视图时也能工作。