//
// 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)//这个被包裹的 CanvasBoard 就有了手势操作
}
}
}
苹果应用商店|搜|王勋才|有我全部作品
让一个swiftUI View 拥有多种手势操作
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 很多人都有不顺遂的童年,甚至于已经成年为人父母了,幼年时的有些伤痛依然根深蒂固地烙印在我们身上。 我的闺蜜就是这样...
- 01 人要有一颗愿意挑战的心,有一个喜欢探索的心态,学着做一些没做过的事,尝试着见一些没见过的人,试着在生活中埋一...
- 我家儿子今年上五年级了,算是一算已经是十一岁的小伙子了,可还是改不掉丢三落四的臭毛病。 因为丢三落四,孩子都闹出好...