在树形层次结构中对齐节点

huangapple go评论85阅读模式
英文:

Align Nodes in tree hierarchy

问题

我正在寻找替代解决方案,因为我已经尝试更改了许多Sprite Kit属性值,并得出结论这是不可能的。所以我不会发布任何代码。您可以尝试使用提到的库以获取更多信息。

原问题:

我正在使用BLBubbleFilters库来创建气泡。
该库使用磁性属性将项目对齐到屏幕中心。每次打开屏幕时,每个节点的位置都不同。

我到目前为止尝试了什么?

  • 我已经尝试了与库和其物理属性(例如磁性属性)的各种玩耍,以实现我的目标。

在树形层次结构中对齐节点

目标是:

  • 将“Family”节点放在所有节点的上方,其余节点位于其下方。

我的想法是,每个节点都将具有一个属性,例如“priority(整数)”。也就是说,优先级为0的节点将位于所有节点的上方,优先级为1的节点位于优先级为0的节点下方,依此类推。

由于SpriteKit处理物理属性,是否有任何属性或方法可以实现我的目标?

在树形层次结构中对齐节点

请看,家庭气泡位于顶部。

英文:

Note:

I am looking for alternate solution, Since I have tried changing lots of Sprite Kit properties values, and came upto conclusion that it is not possible with those changes. So I ain't posting any code. You can try the mentioned library for more information.

Original Question:

I am using BLBubbleFilters library to create Bubbles.
This library used magnetic property to pull align items to the centre of the screen. And each time the screen opens the positioning of each node is different.

What have I tried so far?

  • I have tried playing around with the library and its physics attributes(e.g. Magnetic property) to achieve my goal.

在树形层次结构中对齐节点

The goal is:

  • Place 'Family' node over and above all nodes, and rest of the nodes below it.

What I thought is to that, each node will have a property for example "priority(Integer)". That is, the Priority 0 nodes will be placed above all nodes, priority 1 nodes below 0 priority nodes and so on.

Since SpriteKit deals with Physics properties, is there is any such property or way to achieve my goal?

在树形层次结构中对齐节点

See, how the family bubble is at the Top.

答案1

得分: 1

我不会在这里提供完整的代码翻译,但我可以为你解释代码的主要功能和思路。

你的代码主要是关于自定义UICollectionViewFlowLayout,用于在UICollectionViewCells中创建气泡并排列它们。以下是代码的主要功能:

  1. InterestBubbleType 枚举定义了五种不同的气泡类型(A、B、C、D、E),并提供了它们在Y轴上的位置。

  2. SelectInterestsCollectionViewFlowLayOut 类是自定义的集合视图布局,用于管理气泡的排列和位置。

  3. prepare 方法用于准备布局,设置一些常量和计算布局。

  4. configureCasheForTwentyInterests 方法根据给定的 itemPositions 数组配置气泡的布局属性,并将它们存储在缓存中。

  5. layoutAttributesForElements 方法返回可见区域内的所有布局属性。

  6. layoutAttributesForItem 方法返回指定索引路径的布局属性。

  7. collectionViewContentSize 属性定义了集合视图的内容大小,以便适应所有气泡。

总体来说,这个自定义布局的主要思想是根据不同的气泡类型和位置来排列它们,并考虑到在X轴上需要移动的位置。你可以根据需要配置代码以达到你想要的效果。如果气泡数量发生变化,你只需修改positionsToIncreaseXPosition数组中的位置,布局就会相应地调整。

图像说明中显示了布局的外观,其中每个单元格都是一个白色正方形,内部包含一个具有半高度的圆角的图像视图,不同类型的气泡按照预定义的位置排列。

希望这个解释有助于你理解代码的功能和目的。

英文:

I had to do something like that and I did By creating a custom SelectInterestsCollectionViewFlowLayOut and I added the bubbles in the UICollectionViewCells.

The result was this.
在树形层次结构中对齐节点

Every cell is a white square and inside I have an image view with corner radius 1/2 * height.

The difference is that in my case I had the same bubble size for all my bubbles.

My code is:

    enum InterestBubbleType {
    
    case A
    case B
    case C
    case D
    case E
    
    func getYPosition(cellSideSize: CGFloat) -> CGFloat {
        let spaceFromTopForTwoCellsOnTop: CGFloat = cellSideSize / 2
        switch self {
        case .A:
            return cellSideSize
        case .B:
            return cellSideSize * 2
        case .C:
            return spaceFromTopForTwoCellsOnTop
        case .D:
            return cellSideSize + spaceFromTopForTwoCellsOnTop
        case .E:
            return 0
        }
    }
}

class SelectInterestsCollectionViewFlowLayOut: UICollectionViewFlowLayout {
    var cache: [UICollectionViewLayoutAttributes] = []
    var layoutAttributes: [UICollectionViewLayoutAttributes] = []
    var cellSideSize: CGFloat!
    var xPosition: CGFloat = 0
    var itemPositions: [InterestBubbleType] = [.A,.C,.D,.E,.A,.B,.C,.D,.E,.A,.B,.C,.D,.A,.B,.C,.D,.E,.A,.B]
    var positionsToIncreaseXPosition = [0,2,5,7,10,12,14,16]
    
    override func prepare() {
        cache = []
        super.prepare()
        //Prepare constants
        self.xPosition = 0
        cellSideSize = self.collectionView!.height() / 3
        self.configureCasheForTwentyInterests()
    }
    
    private func configureCasheForTwentyInterests() {
        for i in 0...(self.itemPositions.count - 1) {
            let indexPath = IndexPath(item: i, section: 0)
            let xPosition = self.xPosition
            let frame = CGRect(x: xPosition, y: self.itemPositions[i].getYPosition(cellSideSize: self.cellSideSize), width: self.cellSideSize, height: self.cellSideSize)
            let atribute = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            atribute.frame = frame
            cache.append(atribute)
            if self.positionsToIncreaseXPosition.contains(i) {
                self.xPosition += self.cellSideSize
            }
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        self.layoutAttributes = []
        for uiCollectionViewLayoutAttribute in self.cache {
            self.layoutAttributes.append(uiCollectionViewLayoutAttribute)
        }
        return layoutAttributes
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return layoutAttributes[indexPath.row]
    }
    
    override var collectionViewContentSize: CGSize {
        let totalWidth = cellSideSize * 9
        return CGSize(width: totalWidth, height: self.collectionView!.height())
    }
}

Now how it works is that in my collection view there are 5 different y positions that a bubble can be inside the collection view and they are A,B,C,D,E and I also keep in mind when you need to move 1 more x position which is 1 * collectionViewWidth size.

You can configure the code to achive your own result.
In my case I had a fixed number of bubbles and a fixed view to show them. The reason I chose to do it with collection view was because if the number change you can just add positions to change x position in positionsToIncreaseXPosition and it will work.

huangapple
  • 本文由 发表于 2020年1月6日 19:26:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定