-
Notifications
You must be signed in to change notification settings - Fork 0
polyline custom
Hyebin (Helia) edited this page Oct 11, 2023
·
1 revision
1. 'locations' ๋ณ์ ์์ฑ 2. Path๋ก ์ด๋๊ฒฝ๋ก ๊ทธ๋ฆฌ๊ธฐ 3. Gesture ๊ตฌํํ๊ธฐ
-
CLLocationCoordinate2Dํ์ ์ ์ขํ ๋ฐฐ์ด์ ์์ฑํฉ๋๋ค.
let locations = [
CLLocationCoordinate2D(latitude: 36.01719, longitude: 129.31855),
CLLocationCoordinate2D(latitude: 36.01691, longitude: 129.3181),
CLLocationCoordinate2D(latitude: 36.01663, longitude: 129.31851),
CLLocationCoordinate2D(latitude: 36.0169, longitude: 129.31887),
CLLocationCoordinate2D(latitude: 36.01719, longitude: 129.31855),
]- locations ๋ฐฐ์ด์ ๋ด๊ธด ๋ฐ์ดํฐ๋ค์ Path๋ก ๊ทธ๋ฆฝ๋๋ค.
- ์๋, ๊ฒฝ๋๋ฅผ ๊ธฐ์ค์ผ๋ก x,y์ขํ๋ฅผ ๊ตฌํฉ๋๋ค.
- ํ์ฌ๋ scale์ 100000 ์ผ๋ก ์ค์ ํ์๋๋ฐ, ์ด ๊ฐ์ด ์ปค์ง ์๋ก ๊ทธ๋ ค์ง๋ line์ด ์ปค์ง๋๋ค.
private var drawLine: some View {
Path { path in
for (index, location) in locations.enumerated() {
let point = CGPoint(
x: UIScreen.main.bounds.width / 2 + CGFloat(location.longitude - locations[0].longitude) * 100000,
y: UIScreen.main.bounds.width / 2 - CGFloat(location.latitude - locations[0].latitude) * 100000
)
if index == 0 {
path.move(to: point)
} else {
path.addLine(to: point)
}
}
}
.stroke(Color.orange, lineWidth: 5)
.aspectRatio(contentMode: .fit)
}- path์ ์์น๋ฅผ ๋ณ๊ฒฝ์ํค๊ธฐ ์ํ DragGesture๋ฅผ ์์ฑํฉ๋๋ค.
- path์ ๊ฐ๋๋ฅผ ๋ณ๊ฒฝ์ํค๊ธฐ ์ํ RotationGesture๋ฅผ ์์ฑํฉ๋๋ค.
- path์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝ์ํค๊ธฐ ์ํ magnificationGesture๋ฅผ ์์ฑํฉ๋๋ค.
@State private var scale: CGFloat = 1
@State private var lastScale: CGFloat = 0
@State private var offset: CGSize = .zero
@State private var lastStoredOffset: CGSize = .zero
@State private var angle: Angle = .degrees(0)
@State private var lastAngle: Angle = .degrees(0)
private var dragGesture: some Gesture {
return DragGesture()
.onChanged { value in
let translation = value.translation
offset = CGSize(
width: translation.width+lastStoredOffset.width,
height: translation.height+lastStoredOffset.height
)
}
.onEnded { value in
lastStoredOffset = offset
}
}
private var rotationGesture: some Gesture {
return RotationGesture()
.onChanged { value in
angle = lastAngle+value
}
.onEnded { value in
withAnimation(.spring) {
lastAngle = angle
}
}
}
private var magnificationGesture: some Gesture {
return MagnificationGesture()
.onChanged { value in
let updatedScale = value + lastScale
scale = (updatedScale < 1 ? 1 : updatedScale)
}
.onEnded { value in
withAnimation(.easeInOut(duration: 0.2)) {
if scale < 1 {
scale = 1
lastScale = 0
} else {
lastScale = scale-1
}
}
}
}- Path์ gesture๋ฅผ ์ถ๊ฐํ๊ณ , ํฌ๊ธฐ, ์์น, ๊ฐ๋๊ฐ gesture์ ๋ฐ๋ผ ๋ณ๊ฒฝ๋๋๋ก ์ค์ ํฉ๋๋ค.
- rotationGesture์ magnificationGesture๋ฅผ ๋์์ ์ธ์ํ ์ ์๋๋ก magnificationGesture๋ 'simultaneousGesture'๋ก ์ ์ธํฉ๋๋ค.
drawLine
.ignoresSafeArea()
.scaleEffect(scale)
.offset(offset)
.rotationEffect(angle)
.gesture(dragGesture)
.gesture(rotationGesture)
.simultaneousGesture(magnificationGesture)