Guest User

Untitled

a guest
Nov 17th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.61 KB | Source Code | 0 0
  1. import SwiftUI
  2.  
  3. struct Arrow: Shape {
  4.    
  5.     var headHeight = 0.5
  6.     var shaftWidth = 0.5
  7.    
  8.     var animatableData: AnimatablePair<Double, Double> {
  9.         get { AnimatablePair (headHeight, shaftWidth) }
  10.         set {
  11.             headHeight = newValue.first
  12.             shaftWidth = newValue.second }
  13.     }
  14.     func path(in rect: CGRect) -> Path {
  15.        
  16.         let height = rect.height * headHeight
  17.         let thickness = rect.width * shaftWidth / 2
  18.        
  19.         return Path { path in
  20.             path.move(to: CGPoint(x: rect.midX, y: 0))
  21.             path.addLine(to: CGPoint(x: rect.maxX, y: height))
  22.             path.addLine(to: CGPoint(x: rect.midX + thickness, y: height))
  23.             path.addLine(to: CGPoint(x: rect.midX + thickness, y: rect.maxY))
  24.             path.addLine(to: CGPoint(x: rect.midX - thickness, y: rect.maxY))
  25.             path.addLine(to: CGPoint(x: rect.midX - thickness, y: height))
  26.             path.addLine(to: CGPoint(x: 0, y: height))
  27.             path.closeSubpath()
  28.         }
  29.     }
  30. }
  31.  
  32. struct ContentView: View {
  33.     @State private var headHeight = 0.5
  34.     @State private var shaftWidth = 0.25
  35.        
  36.     var body: some View {
  37.        
  38.      Arrow(headHeight: headHeight, shaftWidth: shaftWidth)
  39.             .fill(.blue)
  40.             .onTapGesture {
  41.                 withAnimation {
  42.                     headHeight = Double.random(in: 0.2...0.8)
  43.                     shaftWidth = Double.random(in: 0.2...0.8)
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. struct ContentView_Previews: PreviewProvider {
  50.     static var previews: some View {
  51.         ContentView()
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment