使用 Swift5.5、iOS15.0.1、
我不得不意識到我的圓形 ProgressBar 在更新到 iOS15 后不再有影片效果。
下面是我的代碼 - 誰能告訴我該怎么做才能使圓形 ProgressBar-View 再次影片?
誰能告訴我如何在這個例子中規避棄用警告
animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.?
import SwiftUI
struct ContentView: View {
@State var progressValue: Float = 0.28
var body: some View {
ProgressBar(progress: $progressValue)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ProgressBar: View {
@Binding var progress: Float
var body: some View {
ZStack {
Circle()
.stroke(lineWidth: 20.0)
.opacity(0.3)
.foregroundColor(Color.red)
Circle()
.trim(from: 0.0, to: CGFloat(min(self.progress, 1.0)))
.stroke(style: StrokeStyle(lineWidth: 20.0, lineCap: .round, lineJoin: .round))
.foregroundColor(Color.red)
.rotationEffect(Angle(degrees: 270.0))
.animation(.linear)
Text(String(format: "%.0f %%", min(self.progress, 1.0)*100.0))
.font(.largeTitle)
.bold()
}
}
}
我的原始版本是這樣的:
Circle()
.trim(from: 0, to: showFreespaceRing ? CGFloat(Double(freeDiskspace) / Double(totalDiskspace)) : 0)
.stroke(Color.green.opacity(0.7), style: StrokeStyle(lineWidth: 10, lineCap: .round))
.frame(width: circleDiam, height: circleDiam)
.animation(.easeIn(duration: 1))
.onAppear() {
showFreespaceRing = true
}
uj5u.com熱心網友回復:
該value引數采用一個Equatable代表影片的值。在這種情況下:progress。
我也移到了.animation外面ZStack——否則,我看到影片上有一個有趣的抖動。
struct ContentView: View {
@State var progressValue: Float = 0.28
let timer = Timer.publish(every: 1.0, on: .main, in: .default).autoconnect()
var body: some View {
ProgressBar(progress: $progressValue)
.onReceive(timer) { _ in
progressValue = 0.1
}
}
}
struct ProgressBar: View {
@Binding var progress: Float
var body: some View {
ZStack {
Circle()
.stroke(lineWidth: 20.0)
.opacity(0.3)
.foregroundColor(Color.red)
Circle()
.trim(from: 0.0, to: CGFloat(min(self.progress, 1.0)))
.stroke(style: StrokeStyle(lineWidth: 20.0, lineCap: .round, lineJoin: .round))
.foregroundColor(Color.red)
.rotationEffect(Angle(degrees: 270.0))
Text(String(format: "%.0f %%", min(self.progress, 1.0)*100.0))
.font(.largeTitle)
.bold()
}
.animation(.linear, value: progress)
}
}
uj5u.com熱心網友回復:
我終于發現出錯了:
在我的原始版本中,我有
Circle()
.trim(from: 0, to: showFreespaceRing ? CGFloat(Double(freeDiskspace) / Double(totalDiskspace)) : 0)
.stroke(Color.green.opacity(0.7), style: StrokeStyle(lineWidth: 10, lineCap: .round))
.frame(width: circleDiam, height: circleDiam)
.animation(.easeIn(duration: 1))
.onAppear() {
showFreespaceRing = true
}
現在我有:
Circle()
.trim(from: 0, to: showFreespaceRing ? 0 : CGFloat(Double(freeDiskspace) / Double(totalDiskspace)))
.stroke(Color.green.opacity(0.7), style: StrokeStyle(lineWidth: 10, lineCap: .round))
.frame(width: circleDiam, height: circleDiam)
.animation(.easeIn(duration: 1), value: showFreespaceRing)
.onAppear() {
showFreespaceRing.toggle()
}
有了這個,影片又開始作業了(有點 - 看下面的視頻,還有一個問題)......
訣竅是.toggle()在onAppear方法內部使用。
絕對不起作用的是showFreespaceRing = true在onAppear()方法內部(而是showFreespaceRing.toggle()改為!!!!!!!!!
而且,當然,實作 iOS15 的新value內部animation:
.animation(.easeIn(duration: 1), value: showFreespaceRing)
但是,目前的解決方案仍然存在一個煩惱:
影片不流暢!!
看這個視頻:
如果仔細觀察,您會發現影片一點也不流暢,而是閃爍得很厲害。(即環形影片看起來仍然是災難性的)。我怎樣才能獲得流暢的影片?

==================================================
經過1天的調查,這是我發現的:
影片中抖動的根本原因似乎是.sheet!!!!
這是一個抖動的完整示例!
import SwiftUI
enum THSheetSelection: Hashable, Identifiable {
case trialSheet
var id: THSheetSelection { self }
}
struct ContentView: View {
@State private var sheetState: THSheetSelection?
var body: some View {
Text("Hello")
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() .milliseconds(2000)) {
sheetState = .trialSheet
}
}
.sheet(item: $sheetState) { state in
switch state {
case .trialSheet:
SessionDiskspaceStatisticsView()
}
}
}
}
struct SessionDiskspaceStatisticsView: View {
@State private var neededDiskSpace: Int64 = 0
@State private var freeDiskspace: Int64 = 0
@State private var totalDiskspace: Int64 = 0
@State private var showFreespaceRing = false
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom
var body: some View {
ZStack {
VStack(alignment: .center) {
Spacer().frame(height: 30)
ZStack {
HStack {
Spacer().frame(width: 50)
ZStack{
Circle()
.trim(from: 0, to: 1)
.stroke(Color.gray.opacity(0.2), lineWidth: 10)
.frame(width: 137.5, height: 137.5)
if neededDiskSpace < freeDiskspace {
Circle()
.trim(from: 0, to: showFreespaceRing ? 0 : CGFloat(Double(freeDiskspace) / Double(totalDiskspace)))
.stroke(Color.green.opacity(0.7), style: StrokeStyle(lineWidth: 10, lineCap: .round))
.frame(width: 137.5, height: 137.5)
.animation(.easeIn(duration: 1), value: showFreespaceRing)
.onAppear() {
showFreespaceRing.toggle()
}
.rotationEffect(.init(degrees: 90))
} else {
Text(LocalizedStringKey("NotEnoughSpaceKey"))
.font(.system(size: 16))
.fontWeight(.regular)
.foregroundColor(Color.red)
.frame(width: 137.5 - 10)
.multilineTextAlignment(.center)
.rotationEffect(.init(degrees: 90))
}
}
.rotationEffect(.init(degrees: -90))
Spacer()
}
}
Spacer().frame(height: 30)
}
}
.onAppear {
neededDiskSpace = 20000
freeDiskspace = 130000
totalDiskspace = 150000
}
}
}
Any idea on how to animate the circular-progressBar inside a .sheet without Jitter ??????
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315329.html
上一篇:Python:影片Matplotlib錯誤'缺少1個必需的位置引數:'curr'
下一篇:在R中創建GIF影片
