Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Custom Gesture Recognizer on iOS
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Tachibana Kaoru
April 27, 2020
1.5k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Custom Gesture Recognizer on iOS
Tachibana Kaoru
April 27, 2020
More Decks by Tachibana Kaoru
See All by Tachibana Kaoru
GeoLocationAnchor and MKTileOverlay
toyship
0
400
Custom Group Activities
toyship
3
1.5k
Synchronized iPhones, Again!
toyship
2
1.7k
ARKit4.pdf
toyship
1
2.3k
HEVC Video with Alpha Channel
toyship
1
1.3k
RealityKit & Reality Composer
toyship
3
680
ARKit3
toyship
4
7.6k
UIViewPropertyAnimator and Easing
toyship
2
1.2k
Synchronized iPhones!
toyship
3
5k
Featured
See All Featured
Site-Speed That Sticks
csswizardry
13
1.2k
Music & Morning Musume
bryan
47
7.2k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.4k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
210
Rails Girls Zürich Keynote
gr2m
96
14k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
340
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Utilizing Notion as your number one productivity tool
mfonobong
4
320
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
180
Building Adaptive Systems
keathley
44
3.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