Created
January 10, 2020 11:08
-
-
Save CodeSlicing/9992dbeb3f999e73bee9610e06cb9ea8 to your computer and use it in GitHub Desktop.
SwiftUI example showing a sticky state drag with spring animations
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
// | |
// StickyDrag.swift | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
// of the Software, and to permit persons to whom the Software is furnished to do so, | |
// subject to the following conditions: | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// | |
// Created by Adam Fordyce on 10/01/2020. | |
// Copyright © 2020 Adam Fordyce. All rights reserved. | |
// | |
import SwiftUI | |
import PureSwiftUI | |
struct StickyDrag: View { | |
var body: some View { | |
HStack { | |
StickyShape(color: .red) | |
StickyShape(color: .green) | |
StickyShape(color: .blue) | |
} | |
} | |
} | |
private let stickyThreshold: CGFloat = 175 | |
private let shapeSize: CGFloat = 100 | |
struct StickyShape: View { | |
let color: Color | |
@GestureState private var translation: CGSize = .zero | |
var scaleForTranslation: CGFloat { | |
if abs(translation.y) < stickyThreshold { | |
return 1 + abs(translation.y) / shapeSize | |
} else { | |
return 1 | |
} | |
} | |
var offsetForTranslation: CGFloat { | |
if abs(translation.y) < stickyThreshold { | |
return translation.y / 2 | |
} else { | |
return translation.y | |
} | |
} | |
var body: some View { | |
VStack { | |
Frame(shapeSize, self.color) | |
.shadow(5) | |
.yScale(self.scaleForTranslation) | |
.yOffset(self.offsetForTranslation) | |
.gesture(DragGesture(minimumDistance: 0) | |
.updating(self.$translation) { value, state, _ in | |
state = value.translation | |
}) | |
.animation(.spring(response: 0.2, dampingFraction: 0.4, blendDuration: 1)) | |
} | |
} | |
} | |
struct StickyDrag_Previews: PreviewProvider { | |
struct StickyDrag_Harness: View { | |
var body: some View { | |
StickyDrag().previewDevice(.iPhone_11_Pro) | |
} | |
} | |
static var previews: some View { | |
StickyDrag_Harness() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment