Created
January 28, 2020 09:29
-
-
Save e-sung/15034df3d1365de8318c94b9e781fbc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SwiftUI | |
import Foundation | |
import PlaygroundSupport | |
/// 터치 포인트와 원의 중심과의 거리만큼 원이 늘어나도록 하는 뷰 | |
struct ResizableCircle: View { | |
@State var circleRadius:CGFloat = 100 | |
/// 하드코딩된 값을 쓰지 않고, 그냥, View자체의 center 의 CGPoint값을 알아 낼 수 있는 방법이 없을까요? | |
var center: CGPoint = CGPoint(x: 200, y: 200) | |
var drag: some Gesture { | |
return DragGesture() | |
.onChanged({ value in | |
let distanceFromCenter = value.location.distance(to: self.center) | |
self.circleRadius = distanceFromCenter | |
}) | |
} | |
var circle: some View { | |
Circle() | |
.fill(Color.blue) | |
.frame(width: circleRadius) | |
.gesture(drag) | |
.position(self.center) | |
} | |
var body: some View { | |
self.circle | |
} | |
} | |
public extension CGPoint { | |
func distance(to point: CGPoint) -> CGFloat { | |
return sqrt(pow((point.x - x), 2) + pow((point.y - y), 2)) | |
} | |
} | |
PlaygroundPage.current.setLiveView(ResizableCircle()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment