0

我跟着@swiftPunk 回答它工作正常。但在最后一点,我坚持我的第一张图片没有正确显示。请检查随附的屏幕截图。

更新代码:-

struct ImageStackRoundUpr: View {
var friendRiders = ["DTR-CentrImg.jpeg", "1frame_0_delay-0.07s", "frame_7_delay-0.07s"]
var nonFriendRiders = ["DTR-CentrImg.jpeg", "1frame_0_delay-0.07s", "frame_7_delay-0.07s"]
var body: some View {
    HStack(spacing:-10) {
        Group {
            ZStack {
                Image("DTR-CentrImg.jpeg")
                     .renderingMode(.original)
                    .resizable()
                    .frame(width: 40, height: 40)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.gray, lineWidth: 1))
                ZStack {
                    Image("Crown")
                        .renderingMode(.original)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 23, height: 23)
                        .shadow(radius: 1)
                }
                .offset(x: 0, y: -25)
                .rotationEffect(.degrees(12))
            }
            
            ForEach(self.friendRiders.indices, id: \.self) { rider in
                Image(friendRiders[rider])
                    .renderingMode(.original)
                    .resizable()
                    .frame(width: 34, height: 34)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.gray, lineWidth: 1))
                    .zIndex(Double(friendRiders.count + nonFriendRiders.count - rider) )
            }
            
            ForEach(self.nonFriendRiders.indices, id: \.self) { rider in
                Image(nonFriendRiders[rider])
                    .renderingMode(.original)
                    .resizable()
                    .frame(width: 34, height: 34)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.gray, lineWidth: 1))
                    .zIndex(Double(nonFriendRiders.count - rider))
            }
        }
        Spacer()
    }.padding(.vertical)
    }
 }

输出:-

截屏

我的目标:-

截屏

有人可以向我解释如何在 HStack 下显示图像,我已经尝试通过上面的方式实现,但还没有结果。

任何帮助将不胜感激。

提前致谢。

4

3 回答 3

3

使用 zIndex 你可以做到


在此处输入图像描述

版本 1.0.0

struct ImageStackRoundUpr: View {
    
    var friendImages = ["1", "2", "3", "4", "5"]
    
    var body: some View {
        
        HStack(spacing: -10.0) {

            ForEach(friendImages.indices, id: \.self) { index in
                Image(friendImages[index])
                    .resizable()
                    .scaledToFit()
                    .frame(width: 80, height: 80)
                    .background(Color.gray)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.black, lineWidth: 2))
                    .zIndex(Double(friendImages.count - index))
            }

            Spacer()

        }
        .background(Color.green)
  
    }
}

版本 2.0.0

最终你会寻找这个

在此处输入图像描述


import SwiftUI

struct ContentView: View {
    var body: some View {
        
        zIndexHierarchyView()
        
    }
}

struct zIndexHierarchyView: View {

    let imageArray: [String] = ["1", "2", "3", "4", "5"]
    
    var body: some View {
        
        ScrollView(.horizontal, showsIndicators: false) {
            
            HStack(spacing: -30.0) {
                
                ForEach(imageArray.indices, id: \.self) { index in
                    
                    Image(imageArray[index])
                        .resizable()
                        .scaledToFit()
                        .frame(width: 150, height: 150)
                        .background(Color.gray)
                        .clipShape(Circle())
                        .overlay(Circle().strokeBorder(Color.black, lineWidth: 3))
                        .zIndex(Double(imageArray.count + imageArray.count - index))  // <<: Here!
                        .shadow(radius: 10)
                    
                }
                
                ForEach(imageArray.indices, id: \.self) { index in
                    
                    Image(imageArray[index])
                        .resizable()
                        .scaledToFit()
                        .frame(width: 150, height: 150)
                        .background(Color.gray)
                        .clipShape(Circle())
                        .overlay(Circle().stroke(Color.black, lineWidth: 3))
                        .zIndex(Double(imageArray.count - index))                    // <<: Here!
                        .shadow(radius: 10)
                    
                }
                
            }
            .padding(10)
            .background(Color.green)
            .cornerRadius(10)
            
        }
        .shadow(radius: 10)
        .statusBar(hidden: true)
        
    }
}

2.0.1 版

关于更新:此更新仅根据 OP 的要求进行,以解决新问题!


在此处输入图像描述

struct ImageStackRoundUpr: View {
    
    var friendRiders = ["1", "2", "3", "4", "5"]
    var nonFriendRiders = ["1", "2", "3", "4", "5"]
    
    var body: some View {
        
        HStack(spacing: -10) {
            
            Image("1")
                .renderingMode(.original)
                .resizable()
                .frame(width: 40, height: 40)
                .background(Color.gray)
                .clipShape(Circle())
                .overlay(Circle().stroke(Color.black, lineWidth: 1))
                .zIndex(Double(friendRiders.count + nonFriendRiders.count + 1))
                .overlay(Image("Crown")
                            .renderingMode(.original)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 23, height: 23)
                            .shadow(radius: 1)
                            .offset(x: 0, y: -25)
                            .rotationEffect(.degrees(12)))
            
            ForEach(self.friendRiders.indices, id: \.self) { rider in
                
                Image(friendRiders[rider])
                    .renderingMode(.original)
                    .resizable()
                    .frame(width: 34, height: 34)
                    .background(Color.gray)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.black, lineWidth: 1))
                    .zIndex(Double(friendRiders.count + nonFriendRiders.count - rider))
                
            }
            
            ForEach(self.nonFriendRiders.indices, id: \.self) { rider in
                
                Image(nonFriendRiders[rider])
                    .renderingMode(.original)
                    .resizable()
                    .frame(width: 34, height: 34)
                    .background(Color.gray)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.black, lineWidth: 1))
                    .zIndex(Double(nonFriendRiders.count - rider))
            }
            
            
            Spacer()
            
        }
        .padding(.vertical)

    }
}
于 2021-04-05T14:02:30.143 回答
1

尝试以下操作:

struct ContentView: View {
    var friendImages: [Color] = [Color.red, Color.green, Color.blue]
    var body: some View {
        HStack(spacing:-6) {
            Group {
                ForEach(friendImages, id: \.self) { imgeName in
                    imgeName
                        .frame(width: 40, height: 40)
                        .clipShape(Circle())
                        .overlay(Circle().stroke(Color.gray, lineWidth: 1))
                        .zIndex(Double(friendImages.count) - Double(friendImages.firstIndex(of: imgeName) ?? 0))
                }
            }
            Spacer()
        }.padding(.vertical)
    }
}

所以这里的关键是.zIndex(...)修饰符。您可以在文档中阅读更多相关信息:https://developer.apple.com/documentation/swiftui/view/zindex(_:)

编辑:
回答更新的问题:我不会使用三个不同的 ForEach 循环。相反,我会简单地调整我的数组:

struct ContentView: View {
    var friendImages: [Color] = [Color.red, Color.green, Color.blue].repeated(count: 3)
    var body: some View {
        HStack(spacing: -6) {
            Group {
                ForEach(friendImages.indices, id: \.self) { i in
                    friendImages[i]
                        .frame(width: 40, height: 40)
                        .clipShape(Circle())
                        .overlay(Circle().stroke(Color.gray, lineWidth: 1))
                        .zIndex(Double(friendImages.count - i))
                }
            }
            Spacer()
        }.padding(.vertical)
    }
}

extension Array {
  init(repeating: [Element], count: Int) {
    self.init([[Element]](repeating: repeating, count: count).flatMap{$0})
  }

  func repeated(count: Int) -> [Element] {
    return [Element](repeating: self, count: count)
  }
}

请参阅此问题以供参考: 在 Swift 中重复数组

于 2021-04-05T13:57:57.613 回答
1

可能的解决方案(因为它们都在一个容器中)是用来zIndex使它们以相反的顺序绘制,例如

    ForEach(friendImages.indices, id: \.self) { i in
        Image(friendImages[i])
            .resizable()
            .frame(width: 40, height: 40)
            .clipShape(Circle())
            .overlay(Circle().stroke(Color.gray, lineWidth: 1))
            .zIndex(Double(friendImages.count - i))
    }
于 2021-04-05T13:58:18.717 回答