SwiftUI - how to exclude inner `DragGesture` from outer `simultaneousGesture`? - ios

I have a yellow button and a blue circle inside a gray container. I added DragGestures to both the circle and the container — however, the circle never moves.
This is the behavior that I want:
Dragging the gray area
Dragging the button
Dragging the circle
The container moves
The container moves
The circle moves
This is what I get:
Dragging the gray area
Dragging the button
Dragging the circle
The container moves ✅
The container moves ✅
The container moves ❌
Here's my code:
struct ContentView: View {
#GestureState var containerOffset = CGSize.zero
#GestureState var circleOffset = CGSize.zero
var body: some View {
VStack { /// gray container
/// should move the gray container when dragged
Button {
print("Button Pressed")
} label: {
Text("Button")
.padding(50)
.background(.yellow)
}
/// should **NOT** move the gray container when dragged
/// How do I make it move the circle instead?
Circle()
.fill(.blue)
.frame(width: 100, height: 100)
.offset(circleOffset)
.gesture(
DragGesture() /// this never gets called!
.updating($circleOffset) { value, circleOffset, transaction in
circleOffset = value.translation
}
)
.padding(50)
}
.background(Color.gray)
.offset(containerOffset)
.simultaneousGesture( /// `simultaneous` is needed to allow dragging from the yellow button
DragGesture() /// move the gray container
.updating($containerOffset) { value, containerOffset, transaction in
containerOffset = value.translation
}
)
}
}
How do I stop the outer DragGesture from swallowing the inner circle's DragGesture?

One way to do it is to add a state variable in ContentView that tracks whether the circle's gesture is active. When the circle's gesture is active, use a GestureMask of .subviews on the outer gesture to prevent it from activating. You can do it with an #State:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
#GestureState var containerOffset = CGSize.zero
#GestureState var circleOffset = CGSize.zero
#State var subviewDragIsActive = false
var body: some View {
VStack { /// gray container
/// should move the gray container when dragged
Button {
print("Button Pressed")
} label: {
Text("Button")
.padding(50)
.background(Color.yellow)
}
/// should **NOT** move the gray container when dragged
/// How do I make it move the circle instead?
Circle()
.fill(Color.blue)
.frame(width: 100, height: 100)
.offset(circleOffset)
.gesture(
DragGesture() /// this never gets called!
.updating($circleOffset) { value, circleOffset, transaction in
circleOffset = value.translation
}
.onChanged { _ in
subviewDragIsActive = true
}
.onEnded { _ in
subviewDragIsActive = false
}
)
.padding(50)
}
.background(Color.gray)
.offset(containerOffset)
.simultaneousGesture( /// `simultaneous` is needed to allow dragging from the yellow button
DragGesture() /// move the gray container
.updating($containerOffset) { value, containerOffset, transaction in
containerOffset = value.translation
},
including: subviewDragIsActive ? .subviews : .all
)
}
}
PlaygroundPage.current.setLiveView(ContentView())
or you can do it with an #GestureState:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
#GestureState var containerOffset = CGSize.zero
#GestureState var circleOffset = CGSize.zero
#GestureState var subviewDragIsActive = false
var body: some View {
VStack { /// gray container
/// should move the gray container when dragged
Button {
print("Button Pressed")
} label: {
Text("Button")
.padding(50)
.background(Color.yellow)
}
/// should **NOT** move the gray container when dragged
/// How do I make it move the circle instead?
Circle()
.fill(Color.blue)
.frame(width: 100, height: 100)
.offset(circleOffset)
.gesture(
DragGesture() /// this never gets called!
.updating($circleOffset) { value, circleOffset, transaction in
circleOffset = value.translation
}
.updating($subviewDragIsActive) {
_, flag, _ in
flag = true
}
)
.padding(50)
}
.background(Color.gray)
.offset(containerOffset)
.simultaneousGesture( /// `simultaneous` is needed to allow dragging from the yellow button
DragGesture() /// move the gray container
.updating($containerOffset) { value, containerOffset, transaction in
containerOffset = value.translation
},
including: subviewDragIsActive ? .subviews : .all
)
}
}
PlaygroundPage.current.setLiveView(ContentView())
If the circle is in a separately-defined View type, you should use an #State so you can pass down a Binding, like this:
import SwiftUI
import PlaygroundSupport
struct CircleView: View {
#GestureState var circleOffset = CGSize.zero
#Binding var dragIsActive: Bool
var body: some View {
Circle()
.fill(Color.blue)
.frame(width: 100, height: 100)
.offset(circleOffset)
.gesture(
DragGesture() /// this never gets called!
.updating($circleOffset) { value, circleOffset, transaction in
circleOffset = value.translation
}
.onChanged { _ in dragIsActive = true }
.onEnded { _ in dragIsActive = false }
)
}
}
struct ContentView: View {
#GestureState var containerOffset = CGSize.zero
#State var subviewDragIsActive = false
var body: some View {
VStack { /// gray container
/// should move the gray container when dragged
Button {
print("Button Pressed")
} label: {
Text("Button")
.padding(50)
.background(Color.yellow)
}
/// should **NOT** move the gray container when dragged
/// How do I make it move the circle instead?
CircleView(dragIsActive: $subviewDragIsActive)
.padding(50)
}
.background(Color.gray)
.offset(containerOffset)
.simultaneousGesture( /// `simultaneous` is needed to allow dragging from the yellow button
DragGesture() /// move the gray container
.updating($containerOffset) { value, containerOffset, transaction in
containerOffset = value.translation
},
including: subviewDragIsActive ? .subviews : .all
)
}
}
PlaygroundPage.current.setLiveView(ContentView())
If any of several subviews might have a drag going on simultaneously (due to multitouch), you should probably use the preference system to pass their isActive states up to ContentView, but that's a significantly more complex implementation.

Related

Part of scrollview should have scrolling disabled SwiftUI

I have a ScrollView layout as shown in photo.
Is it possible and how could you achieve the following in SwiftUI:
Green square area does not react to scroll gestures but is part of the ScrollView and will scroll with red squares
The rest of the screen (red square area) react to scroll gestures as usual.
The reason is ask this it that in green square area I am trying to add a SpriteView that will handle its own touch events and I don't want ScrollView to prevent this.
The only way I found is to create your own ScrollView, as shown below.
It might be easier to tackle with UIKit.
struct ContentView: View {
#State private var dragAmount = CGFloat.zero
#State private var dragPosition = CGFloat.zero
#State private var dragging = false
var body: some View {
GeometryReader { proxy in
VStack {
RoundedRectangle(cornerRadius: 20)
.fill(.red)
.frame(height: 300)
.overlay(
Text(dragging ? "dragging red ..." : "")
)
.offset(y: dragPosition + dragAmount)
.gesture(DragGesture()
.onChanged { _ in
dragging = true
}
.onEnded { _ in
dragging = false
}
)
ForEach(0..<20) { _ in
RoundedRectangle(cornerRadius: 20)
.fill(.green)
.frame(height: 100)
}
.offset(y: dragPosition + dragAmount)
.gesture(DragGesture()
.onChanged { value in
dragAmount = value.translation.height
}
.onEnded { value in
withAnimation(.easeOut) {
dragPosition += value.predictedEndTranslation.height
dragAmount = 0
dragPosition = min(0, dragPosition)
dragPosition = max(-1750, dragPosition)
}
}
)
}
}
}
}

Scaled view unexpectedly moving up and down when pushed from navigation view

I have a view that should scale in and out, starting immediately when the view is shown and repeating forever. However, I find that it's actually animating up and down as well as scaling much like in this post when it's pushed from a navigation view:
struct PlaceholderView: View {
#State private var isAnimating = false
var body: some View {
Circle()
.frame(width: 30, height: 30)
.scaleEffect(self.isAnimating ? 0.8 : 1)
.animation(Animation.easeInOut(duration: 1).repeatForever())
.onAppear {
self.isAnimating = true
}
.frame(width: 50, height: 50)
.contentShape(
Rectangle()
)
}
}
struct SettingsView: View {
#State private var showPlaceholder = false
var body: some View {
NavigationView {
ZStack {
Button(
action: {
showPlaceholder = true
}, label: {
Text("Go to placeholder")
}
)
NavigationLink(
destination: PlaceholderView(),
isActive: $showPlaceholder
) {
EmptyView()
}
.hidden()
}
}
.navigationViewStyle(.stack)
}
}
Why is this and how can I stop this from happening?
UPDATE:
Wrapping self.isAnimating = true in DispatchQueue.main.async {} fixes the issue, but I don't understand why...
I can reproduce the problem, but I wasn't able to reproduce your DispatchQueue.main.async {} fix.
Here is how I fixed it:
Animation is made by comparing a before state and and after state and animating between them. With an implicit animation, the before state is the position of the circle before it is added to the navigation view. By switching to an explicit animation and setting your before state after the view appears, the move animation is avoided and you only get the scaling:
Replace isAnimating with scale. In onAppear {}, first establish the before scale of 0.8, then animate the change to scale 1:
struct PlaceholderView: View {
// To work correctly, this initial value should be different
// than the before value set in .onAppear {}.
#State private var scale = 1.0
var body: some View {
Circle()
.frame(width: 30, height: 30)
.scaleEffect(self.scale)
.onAppear {
self.scale = 0.8 // before
withAnimation(.easeInOut(duration: 1).repeatForever()) {
self.scale = 1 // after
}
}
.frame(width: 50, height: 50)
.contentShape(
Rectangle()
)
}
}

Animation conflict issue with another animation

I have this LoaderView which grow with given value, I want add some highlight effect to it as well, So basically there is 2 deferent and separate animation happening, one for activeValue and one for the highlight. i did not combine them together because they are 2 deferent things, but my output of codes combine these 2 values together and destroys highlight effect. I am looking a solid answer for my issue to having this 2 animation works side by side.
The issue happens when i add value.
I want this view be usable in macOS as well, so there is a possibility user changes the Window size, then my view would be changed as well.
struct ContentView: View {
#State private var value: CGFloat = 50
var body: some View {
LoaderView(activeValue: value, totalValue: 100)
.padding()
Button("add") { value += 10 }
.padding()
}
}
struct LoaderView: View {
let activeValue: CGFloat
let totalValue: CGFloat
let activeColor: Color
let totalColor: Color
init(activeValue: CGFloat, totalValue: CGFloat, activeColor: Color = Color.green, totalColor: Color = Color.secondary) {
if activeValue >= totalValue { self.activeValue = totalValue }
else if activeValue < 0 { self.activeValue = 0 }
else { self.activeValue = activeValue }
self.totalValue = totalValue
self.activeColor = activeColor
self.totalColor = totalColor
}
#State private var startAnimation: Bool = .init()
private let heightOfCapsule: CGFloat = 5.0
var body: some View {
let linearGradient = LinearGradient(colors: [Color.yellow.opacity(0.1), Color.yellow], startPoint: .leading, endPoint: .trailing)
return GeometryReader { proxy in
totalColor
.overlay(
Capsule()
.fill(activeColor)
.frame(width: (activeValue / totalValue) * proxy.size.width)
.overlay(
Capsule()
.fill(linearGradient)
.frame(width: 100)
.offset(x: startAnimation ? 100 : -100),
alignment: startAnimation ? Alignment.trailing : Alignment.leading)
.clipShape(Capsule()),
alignment: Alignment.leading)
.clipShape(Capsule())
}
.frame(height: heightOfCapsule)
.onAppear(perform: { startAnimation.toggle() })
.animation(Animation.default, value: activeValue)
.animation(Animation.linear(duration: 3.0).repeatForever(autoreverses: false), value: startAnimation)
}
}
The issue isn't the .animations it is that .onAppear is only called once, so your animation never restarts. startAnimation needs to be toggled when activeValue changes. The way your animation works, toggling startAnimation reverses the animation direction. Therefore, I set autoreverses: true. It is a compromise either way.
struct LoaderView: View {
//Nothing changed in this portion
.onAppear{
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
startAnimation.toggle()
}
}
// The issue is that .onAppear() is only called once.
.onChange(of: activeValue, perform: { _ in
startAnimation.toggle()
})
.animation(Animation.default, value: activeValue)
// As a side effect, changing startAnimation to opposite values reverses the animation
// you have made, so setting autoreverses: true hides this.
.animation(Animation.linear(duration: 3.0).repeatForever(autoreverses: true), value: startAnimation)
}
}
Edit:
I apologize for not getting back to this sooner. The issue with the rubber band animation is because of the transition when the app firsts launches. To prevent this, you need to wrap your variable change in .onAppear() in a DispatchQueue.main.asyncAfter(deadline:) with enough time for the view to load. Then when you start the animation, it works correctly.

SwiftUI handling button/view with onTapGesture and onLongPressGesture with a release action

I have a view that has both onTapGesture and onLongPressGesture simultaneously. The issue is that the implementation of my onLongPressGesture prevents onTapGesture from ever being called.
Here is some code
View()
.onTapGesture {
action_1
}
.onLongPressGesture(minimumDuration: 0.5, maximumDistance: 10, pressing: {
pressing in
self.isPressing = pressing
if (pressing) {action_2}
if !pressing {action_3}
}, perform: {})
The pressing argument in .onLongPressGesture detects if a user is pressing the view/button, and will always perform the .onLongPressGesture, regardless of what the minimumDuration is.
Edit
Snapchat shutter where you can tap to take a picture, hold the button to start recording a video, then release the button to stop recording the video. That is why there are three actions that need to be performed.
This is tricky. Here's what I did:
onTapGesture, for the tap gesture
LongPressGesture, for a 0.5 second delay. Once the 0.5 seconds is over (.onEnded), start the recording.
DragGesture, for observing when the finger lift off the screen. When this happens, stop the recording.
struct ContentView: View {
/// for visual indicators
#State var recording = false
#State var tapped = false
var body: some View {
let longPressDrag = LongPressGesture(minimumDuration: 0.5) /// 2.
.onEnded { _ in /// 0.5 seconds is over, start recording
print("Long press start")
recording = true
}
.sequenced(before: DragGesture(minimumDistance: 0)) /// 3.
.onEnded { _ in /// finger lifted, stop recording
print("Long press release")
recording = false
}
Circle()
.fill(recording ? Color.red : Color.blue)
.opacity(tapped ? 0.5 : 1)
.frame(width: 100, height: 100)
.onTapGesture { /// 1.
print("Tapped")
tapped = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { tapped = false }
}
.gesture(longPressDrag)
}
}
Result:
Old answer: The pressing parameter isn't for performing actions like action_2. You can, but it's more commonly used for changing #State, so for example highlighting the view in green when it's pressed. You can find more information in the community documentation.
Instead, what you probably want is to call your action (action_2/print("long")) in the perform closure.
struct ContentView: View {
var body: some View {
Text("Hi")
.font(.title)
.onTapGesture {
print("tap")
}
.onLongPressGesture(minimumDuration: 0.5, maximumDistance: 10) {
print("long")
}
}
}
Result:
XCode 14. Swift 5 I have added both gesture to Image View. On tap gesture image will be scale and unscale. On LongPressGesture image bg color will change. Provide count: 2 to make it doubleTapGesture.
struct ContentView: View {
// MARK: - Property
#State private var isAnimating: Bool = false
#State private var imageScale: CGFloat = 1
#State private var shadowColor: Color = .black
// MARK: - Content
var body: some View {
NavigationView {
ZStack {
Image("magazine-front-cover")
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(8)
.padding()
.shadow(color: shadowColor, radius: 22, x: 2, y: 2)
.opacity(isAnimating ? 1 : 0)
.animation(.linear(duration: 1), value: isAnimating)
.scaleEffect(imageScale)
// MARK: - 1. Tap Gesture
.onTapGesture(count: 1, perform: {
if imageScale == 1 {
withAnimation(.spring()) {
imageScale = 5
}
} else {
withAnimation(.spring()) {
imageScale = 1
}
}
})
// MARK: - 2. LongPress Gesture
.onLongPressGesture(perform: {
if shadowColor == .black {
shadowColor = .green
} else {
shadowColor = .black
}
})
}
.ignoresSafeArea(edges: .all)
.navigationTitle("Pinch & Zoom")
.navigationBarTitleDisplayMode(.inline)
.onAppear {
isAnimating = true
}
} // END of Navigation View
}
}

Why does my SwiftUI animation revert to the previous frame on rotation?

I have a SwiftUI View that is meant to be sort of a "warning" background that flashes on and off. It appears inside another View with a size that changes depending on device orientation. Everything works properly upon the first appearance, but if the device is rotated while the warning view is active it incorporates the frame of the previous orientation into the animation, making it not only flash on and off, but also move in and out of the view its positioned in, as well as change to and from the size it was on the previous orientation. Why is this happening and how can I fix it? Generic code:
struct PulsatingWarningBackground: View {
let color : Color
let speed : Double
#State private var opacity : Double = 1
var body: some View {
GeometryReader {geometry in
self.color
.opacity(self.opacity)
.animation(
Animation
.linear(duration: self.speed)
.repeatForever(autoreverses: true)
)
.onAppear(perform: {self.opacity = 0})
}
}
}
struct PulsatingWarningViewHolder: View {
var body: some View {
GeometryReader {geometry in
ZStack {
Color.white
ZStack {
Color.black
PulsatingWarningBackground(color: .red, speed: 1/4)
}.frame(width: geometry.frame(in: .local).width/5, height: geometry.frame(in: .local).height/10, alignment: .center)
}
}
}
}
You can apply animation only to opacity by using withAnimation(_:_:) inside onAppear(perform:). It works properly as you want.
struct PulsatingWarningBackground: View {
let color : Color
let speed : Double
#State private var opacity : Double = 1
var body: some View {
self.color
.opacity(self.opacity)
.onAppear(perform: {
withAnimation(Animation.linear(duration: self.speed).repeatForever()) {
self.opacity = 0
}
})
}
}
struct PulsatingWarningViewHolder: View {
var body: some View {
GeometryReader {geometry in
ZStack {
Color.white
ZStack {
Color.black
PulsatingWarningBackground(color: .red, speed: 1/4)
}
.frame(width: geometry.frame(in: .local).width/5, height: geometry.frame(in: .local).height/10, alignment: .center)
}
}
}
}

Resources