I have a RoundedRectangle and I want to make the trim rotate forever and make a nice animation from it. I added you this image here, so you can see what kind of trim I am talking about

try 制作旋转效果和3D旋转效果,但不起作用

@State private var show = false

var body: some View {
    ZStack {
        RoundedRectangle(cornerRadius: 20, style: .continuous)
            .foregroundColor(.gray.opacity(0.1))
            .frame(width: 195, height: 50)
        
        
        RoundedRectangle(cornerRadius: 20, style: .continuous)
            .trim(from: 0.25, to: 0.5)
            .stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
            .frame(width: show ? 50 : 200, height: show ? 200 : 50)
            .rotationEffect(Angle(degrees: show ? 90 : 0))
            .animation(.linear.delay(1).repeatForever(autoreverses: true),
                       value: show)
        
        RoundedRectangle(cornerRadius: 20, style: .continuous)
            .trim(from: 0.75, to: 1)
            .stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
            .frame(width: show ? 50 : 200, height: show ? 200 : 50)
            .rotationEffect(Angle(degrees: show ? 90 : 0))
            .animation(.linear.delay(1).repeatForever(autoreverses: true),
                       value: show)
        
        Text("title")
            .foregroundStyle(LinearGradient(gradient: Gradient(colors: colors),
                                            startPoint: .topLeading,
                                            endPoint: .bottomTrailing))
    }
    .onAppear {
        show.toggle()
    }

这是我的密码.使它移动,但不是想要的方式.可能会更顺畅,就像两条蛇相继而行

推荐答案

enter image description here


为修剪偏移的更改设置动画.由于很难设置超过01的边界的动画,因此使用.rotation(Angle(degrees: 180))可始终保持在01的边界内:

struct ContentView: View {
    let colors = [Color.green, Color.blue]
    @State private var offset = 0.0
    
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 20, style: .continuous)
                .foregroundColor(.gray.opacity(0.1))
                .frame(width: 195, height: 50)
            
            RoundedRectangle(cornerRadius: 20, style: .continuous)
                .trim(from: 0.25 + offset, to: 0.5 + offset)
                .stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
                .frame(width: 195, height: 50)
 
            RoundedRectangle(cornerRadius: 20, style: .continuous)
                .trim(from: 0.25 + offset, to: 0.5 + offset)
                .stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
                .rotation(Angle(degrees: 180))
                .frame(width: 195, height: 50)
            
            Text("title")
                .foregroundStyle(LinearGradient(gradient: Gradient(colors: colors),
                                                startPoint: .topLeading,
                                                endPoint: .bottomTrailing))
        }
        .onAppear {
            withAnimation(.linear(duration: 0.75).repeatForever(autoreverses: false)) {
                offset = 0.5
            }
        }
    }
}

优化代码

因为这两条"蛇"除了180度旋转之外都是一样的,所以我们可以用ForEach来编码:

ForEach([0.0, 180.0], id: \.self) { rotation in
    RoundedRectangle(cornerRadius: 20, style: .continuous)
        .trim(from: 0.25 + offset, to: 0.5 + offset)
        .stroke(style: StrokeStyle(lineWidth: 3, lineCap: .round))
        .rotation(Angle(degrees: rotation))
        .frame(width: 195, height: 50)
}

Swift相关问答推荐

在运行时检测蒸汽工人的类型

如何分解阿拉伯字母?

如何在SWIFT中使用DiscardingTaskGroup获得任务结果

当计数大于索引时,索引超出范围崩溃

如何观察UIViewRepresentable中的多个变化?

我需要一些关于 SwiftUI 动画的帮助

Swift计时器未触发

数组中某些元素的总和

有没有一种方法可以访问封闭实例ObservableObject以从属性包装器中的任何位置调用objectWillChange.send()

我怎样才能缩短(提高效率)这段代码?

如何让动画变慢 SwiftUI

任何使 ScrollView 像 List 一样执行更灵活的修饰符?

为什么swiftui中的导航视图栏那么大?

当我必须在 Swift 中的类、 struct 和枚举之间进行 Select 时,我应该如何推理?

在 SwiftUI 中,如何在 UIView 内或作为 UIView 使用 UIHostingController?

如何快速制作虚线?

如何更改弹出框的大小

如何发送静默推送通知有效负载

URLComponents.url 为零

TabView 在切换选项卡时重置导航堆栈