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
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
560
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
220
Paper Plane
katiecoart
PRO
1
51k
Ethics towards AI in product and experience design
skipperchong
2
310
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Documentation Writing (for coders)
carmenintech
77
5.4k
How STYLIGHT went responsive
nonsquared
100
6.2k
The agentic SEO stack - context over prompts
schlessera
0
820
Why Our Code Smells
bkeepers
PRO
340
58k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
310
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
970
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