if the user stops touching the view before minimumDuration seconds have elapsed or moves their finger more than maximumDistance points SwiftUI does not invoke the onEnded(_:) callback.
โญ๏ธ .onChanged()
ๅช่ฆๆธๅฐ๏ผๅฐฑๆ่งธ็ผ๏ผไฝใๅช่งธ็ผไธๆฌกใ๏ผๅฐฑ็ฎๆธๅไน ๏ผไนๅช่งธ็ผไธๆฌก๏ผไธๆ้ฃ็บ่งธ็ผ๏ผ้้็ value ็ธฝๆฏ trueใไฝใๆฏๆธไธๆฌก๏ผๅฐฑ่งธ็ผไธๆฌกใใ
importSwiftUIstructContentView:View {// star image size state (true: small, false: big)@Stateprivatevar isSmallStar =false// โญ๏ธ `@GestureState` tracks the state change of a gesture// `isStarPressed` indicates whether a tap event is detected@GestureStateprivatevar isStarPressed =falsevar body: some View {Image(systemName:"star.circle.fill") .font(.system(size:200))// โญ๏ธ dimmer when star is pressed .opacity(isStarPressed ?0.4:1) .scaleEffect(isSmallStar ?0.5:1.0) .animation(.easeInOut) .foregroundColor(.yellow) .overlay( Text(verbatim:"\(isStarPressed)") .foregroundColor(isStarPressed ? .pink : .secondary)) .gesture( LongPressGesture(minimumDuration:1.0)// on pressed: (tap and hold)// โญ๏ธ update `@GestureState` var as the gesture's value changes.// โข value: LongPressGesture.Value == Bool (current value)// gesture's current value (`true` indicates a tap is detected)// โข state: inout State == Bool (previous gesture's value)// `state` == `isStarPressed` in this case// โข transaction:// stores the "context"(?) of the gesture. .updating($isStarPressed) { (value, state, _) in// โญ๏ธ save current value (is pressed or not?) state = value }// on long pressed:// โข value: LongPressGesture.Value == Bool (gesture's final value)// ๐ธ ่จป๏ผ// ๅชๆๆ้่ถ ้ `minimumDuration` ๆๆๅผ็ผ onEndedโ๏ธ // ่ไธ `isStarPressed` ๆ้ฆฌไธ่ฎ็บ falseโ๏ธ .onEnded { _in// โญ๏ธ toggle image size when long pressed isSmallStar.toggle() }) }}