Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Custom Gesture Recognizer on iOS
Tachibana Kaoru
April 27, 2020
3
860
Custom Gesture Recognizer on iOS
Tachibana Kaoru
April 27, 2020
Tweet
Share
More Decks by Tachibana Kaoru
See All by Tachibana Kaoru
GeoLocationAnchor and MKTileOverlay
toyship
0
110
Custom Group Activities
toyship
3
690
Synchronized iPhones, Again!
toyship
2
950
ARKit4.pdf
toyship
1
1.4k
HEVC Video with Alpha Channel
toyship
1
660
RealityKit & Reality Composer
toyship
3
300
ARKit3
toyship
5
6.3k
UIViewPropertyAnimator and Easing
toyship
2
630
Synchronized iPhones!
toyship
3
3.5k
Featured
See All Featured
Three Pipe Problems
jasonvnalue
89
8.7k
Product Roadmaps are Hard
iamctodd
34
6.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
237
19k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
315
19k
How to train your dragon (web standard)
notwaldorf
58
3.9k
No one is an island. Learnings from fostering a developers community.
thoeni
9
1.3k
Robots, Beer and Maslow
schacon
152
7.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
19
1.4k
A Modern Web Designer's Workflow
chriscoyier
689
180k
How GitHub (no longer) Works
holman
296
140k
Stop Working from a Prison Cell
hatefulcrawdad
261
17k
Fontdeck: Realign not Redesign
paulrobertlloyd
73
4.1k
Transcript
Custom Gesture Recognizer @TachibanaKaoru 2020/4/27 potatotips #69
About Me @TachibanaKaoru Freelance iOS Engineer
Gesture Recognizer
Default Gesture Recognizers UITapGestureRecognizer UISwipeGestureRecognizer UIRotationGestureRecognizer ....
Limitation of Default Gesture δΣενϟʔ͕ೝࣝ͞Ε͍ͯΔ్தͰͳʹ͔ॲཧΛ͍ͨ͠ɻ ಠࣗͷετϩʔΫͰೖྗ͍ͨ͠ɻ
ͦ͜Ͱ Custom Gesture Recognizer !
GestureRecognizerͷ͘͠Έ શମͷྲྀΕ view ͷλονΠϕϯτͷํΛղੳ͢Δɻ ҙਤͨ͠ɾํͳΒδΣενϟʔೝࣝΛଓ͚ɺͦ͏Ͱͳ͚Εऴ ྃ͢Δɻ δΣενϟʔ͕ೝࣝͰ͖ͨΒ delegate ʹ recognizedΠϕϯτΛૹ৴͢
Δɻ
Tracking of UITouch
UITouch open class UITouch : NSObject { open var timestamp:
TimeInterval { get } open var tapCount: Int { get } open var type: UITouch.TouchType { get } open func location(in view: UIView?) -> CGPoint open func previousLocation(in view: UIView?) -> CGPoint }
velocity vector with UITouch location previousLocation
velocity vector with UITouch location previousLocation (x1 , y1 )
velocity vector with UITouch location previousLocation (x1 , y1 )
(x2 , y2 )
velocity vector with UITouch location previousLocation (x1 , y1 )
(x2 , y2 ) ( x2 − x1 t2 − t1 , y2 − y1 t2 − t1 ) 2ؒͷϕΫτϧ
velocity vector with UITouch location previousLocation (x1 , y1 )
(x2 , y2 ) ( x2 − x1 t2 − t1 , y2 − y1 t2 − t1 ) 2ؒͷϕΫτϧ (x2 − x1 , y2 − y1 ) 2ؒͷ୯Ґ࣌ؒ͋ͨΓͷϕΫτϧ
struct Velocity { var vx: CGFloat var vy: CGFloat }
extension UITouch{ func velocity(in view: UIView?) -> Velocity{ let location = location(in: view) let previousLocation = previousLocation(in: view) let distanceX = location.x - previousLocation.x let distanceY = location.y - previousLocation.y let velocity = Velocity(vx: distanceX, vy: distanceY) return velocity }
gradient of UITouch location previousLocation (x1 , y1 ) (x2
, y2 ) θ
gradient of UITouch location previousLocation (x1 , y1 ) (x2
, y2 ) θ vx vy
gradient of UITouch location previousLocation (x1 , y1 ) (x2
, y2 ) θ vx vy tan θ = vy vx
gradient of UITouch location previousLocation (x1 , y1 ) (x2
, y2 ) θ vx vy tan θ = vy vx θ = tan−1 vy vx
struct Velocity { var vx: CGFloat var vy: CGFloat var
gradient: CGFloat{ // ࣮ࡍʹɺvyɺvx͕0ͷ࣌ͷॲཧ͓Αͼɺgradient͕ 0-2πʹͳΔΑ͏ͳ // ਖ਼نԽΛߦ͍·͢ var calcGradient = atan(vy / vx) return calcGradient }
Radian ހ๏ 360° = 2 π radian
π 2 0 π 3π 2
π 2 0 π 3π 2 π 4
π 2 0 π 3π 2 3π 4
Tracking of UITouch ͜ΕͰɺUITouch͔ΒɺϕΫτϧͱ͖ (gradient) ͕Θ͔ΔΑ͏ʹͳ Γ·ͨ͠ɻ
How to handle gesture state Custom Gesture RecognizerͰɺԼهͷ2छྨͷঢ়ଶΛཧ͠·͢ɻ δΣενϟʔͷશମঢ়ଶ (UIGestureRecognizer.State)
ܧଓ or ࣦഊ or ޭ ετϩʔΫͷஅ શମͷͲͷ෦Λೝࣝதͳͷ͔
UIGestureRecognizer.State open class UIGestureRecognizer : NSObject { // the current
state of the gesture recognizer open var state: UIGestureRecognizer.State { get } }
class MyGestureRecognizer: UIGestureRecognizer{ override func touchesBegan(_ touches: Set<UITouch>, with event:
UIEvent) { } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) { } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) { } override func reset() { } }
Handling touch event t
Handling touch event touchesBegan t touchesMoved touchesEnd
touchesBegan t touchesMoved touchesEnd
Possible Recognized Failed Possible gestureॲཧܧଓ gestureॲཧऴྃ gestureॲཧऴྃ
P F P P P P R F F touchesBegan
touchesMoved touchesEnd
ετϩʔΫͷஅ ετϩʔΫͷ͖͔Βɺೝ͍ࣝͨ͠δΣενϟʔͲͷετϩʔΫʹͳͷ ͔Λஅ͢Δɻ ෳͷετϩʔΫ͔Βߏ͞ΕΔδΣενϟʔ͋Δ
ೋͭͷ࿈ଓͨ͠ετϩʔΫ͔Βߏ͞ΕΔ߹
δΣενϟʔ։࢝ ӈԼͷετϩʔΫ ӈ্ͷετϩʔΫ δΣενϟʔྃ
P F P P P P R F F touchesBegan
touchesMoved touchesEnd
ೋͭͷ࿈ଓ͠ͳ͍ετϩʔΫ͔Β ߏ͞ΕΔ߹
ӈԼͷετϩʔΫ͔Β࢝·Δύλʔϯ ࠨԼͷετϩʔΫ͔Β࢝·Δύλʔϯ
P F P P P F touchesBegan touchesMoved touchesEnd touchesEnd
touchesMoved touchesMoved touchesBegan R F P P
·ͱΊ Custom Gesture Recognizerͷ࣮ҙ֎ͱ؆୯Ͱ͢ɻ ͨͩ͠ɺετϩʔΫʹΑͬͯɺϢʔβʔͷೖྗΛҙਤ௨Γʹஅ͢Δ ͷ͕͍͠߹͋ΔͨΊɺঢ়گʹԠͨ͡࠷దԽ͕ඞཁɻ αϯϓϧ͕ͪ͜Βʹ͋ΔͷͰɺΑ͔ͬͨΒݟͯΈ͍ͯͩ͘͞ɻ https://github.com/TachibanaKaoru/CustomGestureRecognizer