【问题标题】:Scale Shapes From An Actual Size Instead Of 0 SwiftUI从实际大小而不是 0 SwiftUI 缩放形状
【发布时间】:2021-01-25 16:07:54
【问题描述】:

我正在尝试为我正在使用 swiftui 制作的应用制作脉冲动画。我想要一个中心灰色圆圈来脉冲。目前,外部圆圈从 0 开始缩放,但我希望它们从中间圆圈大小开始。 这是我当前的代码:

import SwiftUI

struct Pulse: View {
    
    @State var animate = false
    @State private var scale: CGFloat = 2
    var body: some View {
        ZStack
        {
            Circle()
                .fill(Color.black)
                .frame(width: 130, height: 130)
            Circle()
                .stroke(Color.red.opacity(0.1), lineWidth: 5)
                .frame(width: 300, height: 300)
                .scaleEffect(self.animate ? 1 : 0)
            Circle()
                .stroke(Color.red.opacity(0.15), lineWidth: 5)
                .frame(width: 266, height: 266)
                .scaleEffect(self.animate ? 1 : 0)
            
            Circle()
                .stroke(Color.red.opacity(0.2), lineWidth: 5)
                .frame(width: 232, height: 232)
                .scaleEffect(self.animate ? 1 : 0)
            
            Circle()
                .stroke(Color.red.opacity(0.3), lineWidth: 5)
                .frame(width: 198, height: 198)
                .scaleEffect(self.animate ? 1 : 0)
            
            Circle()
                .stroke(Color.red.opacity(0.35), lineWidth: 5)
                .frame(width: 164, height: 164)
                .scaleEffect(self.animate ? 1 : 0)
            
            Circle()
                .stroke(Color.red.opacity(0.4), lineWidth: 5)
                .frame(width: 130, height: 130)
                .scaleEffect(self.animate ? 1 : 0)

            
            Circle()
                .fill(Color.gray)
                .frame(width: 130, height: 130)
            
        }
        .frame(width: 290, height: 290, alignment: .center)
        .onAppear(perform: {
            self.animate.toggle()
        })
        .animation(Animation.linear(duration: 6).repeatForever(autoreverses: true))
        .ignoresSafeArea()
    }
}

【问题讨论】:

    标签: animation swiftui swift5 shapes pulse


    【解决方案1】:

    内圈与外圈的大小之比为130 / 300。在最小的情况下,您希望宽度和高度为 300 的圆为 130

    改变所有这些:

    .scaleEffect(self.animate ? 1 : 0)
    

    到:

    .scaleEffect(self.animate ? 1 : 130 / 300)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多