//
// ContentView.swift
// PencilDraw
//
// Created by wangxuncai on 2021/12/5.
//
import SwiftUI
struct GestureView<Content: View>: View {
let content: Content
@State private var currentAmount: CGFloat = 0
@State private var finalAmount: CGFloat = 1
@State private var currentRotationAmount: Angle = .degrees(0)
@State private var finalRotationAmount: Angle = .degrees(0)
@State private var offset:CGSize = CGSize.zero
@State private var finalOffset:CGSize = CGSize.zero
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
let rotation = RotationGesture()
.onChanged { angle in
currentRotationAmount = angle
}
.onEnded { angle in
finalRotationAmount += self.currentRotationAmount
currentRotationAmount = .degrees(0)
}
let magnification = MagnificationGesture()
.onChanged { amount in
currentAmount = amount - 1
}
.onEnded { amount in
finalAmount += self.currentAmount
currentAmount = 0
}
let moveGesture = DragGesture().onChanged { value in
offset = value.translation
}.onEnded { value in
finalOffset.width += self.offset.width
finalOffset.height += self.offset.height
offset = CGSize.zero
}
let combineGesture = rotation.simultaneously(with: magnification)
let finalGesture = combineGesture.simultaneously(with: moveGesture)
content
.scaleEffect(finalAmount + currentAmount)
.rotationEffect(currentRotationAmount + finalRotationAmount)
.offset(x: offset.width + finalOffset.width, y: offset.height + finalOffset.height)
.gesture(
finalGesture
)
}
}
//使用方法
struct ContentView: View {
@State var image:UIImage = UIImage(named: "1")!
var body: some View {
GestureView{
CanvasBoard(image: $image)//這個(gè)被包裹的 CanvasBoard 就有了手勢(shì)操作
}
}
}
蘋(píng)果應(yīng)用商店|搜|王勛才|有我全部作品
讓一個(gè)swiftUI View 擁有多種手勢(shì)操作
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 很多人都有不順?biāo)斓耐?,甚至于已?jīng)成年為人父母了,幼年時(shí)的有些傷痛依然根深蒂固地烙印在我們身上。 我的閨蜜就是這樣...
- 今年玩微博無(wú)意中關(guān)注了行動(dòng)派琦琦的微博,看她提到了李欣頻,突然腦中閃現(xiàn)了這個(gè)名字,李欣頻——文案天后,搜了下她,才...
- 我家兒子今年上五年級(jí)了,算是一算已經(jīng)是十一歲的小伙子了,可還是改不掉丟三落四的臭毛病。 因?yàn)閬G三落四,孩子都鬧出好...
- 可能成長(zhǎng)的過(guò)程就像胚胎干細(xì)胞分化的過(guò)程,逐漸分化成其專門的作用。活著活著,我們?cè)交钤秸囵B(yǎng)人才--培養(yǎng)行業(yè)專業(yè)人...