相关疑难解决方法(0)

SwiftUI - 获取孩子的大小?

有没有办法在 SwiftUI 中获取子视图的大小?

我基本上希望做 UIKit 相当于:

self.child.frame.origin.x -= self.child.intrinsicContentSize.width/2.0
Run Code Online (Sandbox Code Playgroud)

我认为 GeometryReader 不会工作,因为它返回父级中的可用大小。

[编辑] 我发现可以使用.alignmentGuide(_, computeValue:)虽然这绝对是一个黑客来获取和保存尺寸。

LessonSliderText(text: self.textForProgress(self.progress), color: self.completedColor)
    .alignmentGuide(HorizontalAlignment.leading) { (dimensions) -> Length in
        self.textSize = CGSize(width: dimensions.width, height: dimensions.height)
        return 0
    }
    .offset(x: self.width*self.currentPercentage - self.textSize.width / 2.0)
    .offset(y: -self.textSize.height/2.0)
    .animation(nil)
    .opacity(self.isDragging ? 1.0 : 0.0)
    .animation(.basic())
Run Code Online (Sandbox Code Playgroud)

我想要完成的事情

swiftui

17
推荐指数
2
解决办法
8806
查看次数

SwiftUI - 在列表中的视图内触发的动画也不会为列表设置动画

我有一个List显示两个Views相同类型的。当您点击其中一个视图时,它们会通过动画改变高度。

但是,List嵌入的这些视图不会动画,这会导致丑陋的故障,因为List行的高度会立即更改,而该行内的实际视图是动画的:

在此处输入图片说明

我怎样才能制作List动画?我尝试向其添加.animation修饰符,但这没有任何作用。

我也不想将tapGesture视图移出。视图应该是自包含的,而不是依赖其他视图来控制它(我认为这就是 MVVM 的意义所在)

谢谢!

import SwiftUI

struct SubView: View {
    @State var change: Bool = false

    var body: some View {
        Rectangle()
            .frame(width: 200, height: change ? 300 : 200)
            .foregroundColor(Color.red)
            .onTapGesture {
                withAnimation {
                    self.change.toggle()
                }
        }
    }
}

struct Test: View {

    var body: some View {
        List {
            SubView()
            SubView()
        }
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: …
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

10
推荐指数
1
解决办法
1103
查看次数

Swift UI 列表动画,用于扩展具有动态高度的单元格

我试图在用户点击单元格时为 swiftUI 列表中的单元格展开动画。然而动画有缺陷。

我遇到了这个答案(/sf/answers/4261171841/)来帮助制作动画,但是它需要已经知道单元格展开后的预期高度。就我而言,这取决于以下数据note.text

List{
    ForEach(notes){ note in
        VStack{
            HStack{
                Text(note.title)
                    .fontWeight(.bold)
                    .foregroundColor(.gray)
                Spacer()
                Image(systemName: "chevron.right.circle")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.gray)
                    .frame(width: 20, height: 20)
            }
            .padding(.vertical)
            .contentShape(Rectangle())
            .onTapGesture{
                selectedNote = note
            }
            if isSelected(note) {
                Text(note.text)
            }
        }
        .padding([.bottom, .leading, .trailing], 20)
        .background(
            RoundedRectangle(cornerRadius: 25, style: .continuous)
                .foregroundColor(isSelected(noteSection) ? .red : .clear)
        )
        .animation(.default)
    }
    .onMove(perform: move)
}
Run Code Online (Sandbox Code Playgroud)

正如您从上面的代码中看到的,我需要使用一个列表,以便我可以使用 .onMove 功能。

有没有办法在链接的答案中获得相同的平滑列表动画,但对于动态扩展尺寸?

谢谢。

animation swiftui

7
推荐指数
1
解决办法
984
查看次数

标签 统计

swiftui ×3

animation ×1

ios ×1

swift ×1