var body: someView { // LongPressGesture creation // Gesture will be handled only if if takes at least 2 seconds let longPressGesture =LongPressGesture(minimumDuration: 2, maximumDistance: 10) .onEnded { _in ifself.rectangleColor == .red { self.rectangleColor = .green } else { self.rectangleColor = .red } }
returnRectangle() // Change color .foregroundColor(rectangleColor) .cornerRadius(40) .frame(width: 200, height: 100, alignment: .center) // Add the longPressGesture to this view .gesture(longPressGesture) } }
var body: someView { // DragGesture creation let dragGesture =DragGesture() // When drag location is changed we recalculate offset for the rectangle .onChanged { value in self.rectangleOffset = value.translation } // When gesture ended we return the rectangle to the initial position .onEnded { _in self.rectangleOffset = .zero }
returnRectangle() .foregroundColor(.green) .cornerRadius(40) // Change position for the rectangle .offset(rectangleOffset) .frame(width: 200, height: 100, alignment: .center) // Add the dragGesture to this view .gesture(dragGesture) } }
var body: someView { // MagnificationGesture creation let magnificationGesture =MagnificationGesture() // Scale effect recalculation for the rectangle .onChanged { value in self.rectangleScaleEffect = value }
returnRectangle() .foregroundColor(.green) .cornerRadius(40) // Change scale effect .scaleEffect(rectangleScaleEffect) .frame(width: 200, height: 100, alignment: .center) // Add the magnificationGesture to this view .gesture(magnificationGesture) } }
var body: someView { // DragGesture creation let rotationGesture =RotationGesture() // Rotation angle recalculation for the rectangle .onChanged { value in self.rectangleRotationAngle = value }
returnRectangle() .foregroundColor(.green) .cornerRadius(40) // Rotate the rectangle .rotationEffect(rectangleRotationAngle) .frame(width: 200, height: 100, alignment: .center) // Add the rotationGesture to this view .gesture(rotationGesture) } }