Gestures Mobile Application Development in iOS School of EECS - - PowerPoint PPT Presentation

gestures
SMART_READER_LITE
LIVE PREVIEW

Gestures Mobile Application Development in iOS School of EECS - - PowerPoint PPT Presentation

Gestures Mobile Application Development in iOS School of EECS Washington State University Instructor: Larry Holder Mobile Application Development in iOS 1 Outline Gestures Gesture recognizers Gesture states Custom gestures


slide-1
SLIDE 1

Gestures

Mobile Application Development in iOS School of EECS Washington State University Instructor: Larry Holder

Mobile Application Development in iOS 1

slide-2
SLIDE 2

Outline

  • Gestures
  • Gesture recognizers
  • Gesture states
  • Custom gestures

Mobile Application Development in iOS 2

slide-3
SLIDE 3

Add Gesture in Storyboard

  • Step 1: Drag gesture into view

Mobile Application Development in iOS 3

slide-4
SLIDE 4

Add Gesture in Storyboard

  • Step 2: Connect gesture to @IBAction

Mobile Application Development in iOS 4

// In ViewController class... @IBAction func tapDetected (_ sender: UIGestureRecognizer) { let point = sender.location(in: self.view) let x = Int(point.x) let y = Int(point.y) print("tap detected at (\(x),\(y))") }

slide-5
SLIDE 5

Add Gesture Programmatically

  • View controller class conforms to

UIGestureRecognizerDelegate

  • Implement method to handle gesture
  • Create gesture recognizer and add to view

Mobile Application Development in iOS 5

slide-6
SLIDE 6

Add Gesture Programmatically

Mobile Application Development in iOS 6

class ViewController: UIViewController, UIGestureRecognizerDelegate {

  • verride func viewDidLoad() {

super.viewDidLoad() // Do any additional setup after loading the view let twoTouchTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTwoTouchTap)) twoTouchTapGestureRecognizer.delegate = self twoTouchTapGestureRecognizer.numberOfTouchesRequired = 2 self.view.addGestureRecognizer(twoTouchTapGestureRecognizer) } @objc func handleTwoTouchTap (_ sender: UITapGestureRecognizer) { let point = sender.location(in: self.view) let x = Int(point.x) let y = Int(point.y) print("two-touch tap detected at (\(x),\(y))") } }

slide-7
SLIDE 7

Other Gestures: Subclasses of UIGestureRecognizer

  • UITapGestureRecognizer (multiple taps/touches)
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UIPanGestureRecognizer
  • UIScreenEdgeGestureRecognizer
  • UILongPressGestureRecognizer
  • class MyGesture: UIGestureRecognizer (custom)

Mobile Application Development in iOS 7

Note: Only one gesture detected per user interaction.

slide-8
SLIDE 8

Gesture States

  • UIGestureRecognizer.State

– .possible (default) – .began (calls action) – .changed (calls action) – .ended (calls action, reset to .possible) – .cancelled (calls action, reset to .possible) – .failed (no call to action, reset to .possible) – .recognized (multi-touch, calls action, reset to .possible)

  • developer.apple.com/documentation/uikit/uigesturerecognizer/state

Mobile Application Development in iOS 8

slide-9
SLIDE 9

Gesture States (e.g., Pan)

Mobile Application Development in iOS 9

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan)) panGestureRecognizer.delegate = self self.view.addGestureRecognizer(panGestureRecognizer) @objc func handlePan (_ sender: UIPanGestureRecognizer) { let point = sender.location(in: self.view) let x = Int(point.x) let y = Int(point.y) if (sender.state == .began) { print("pan began at (\(x),\(y))") } if (sender.state == .changed) { print ("pan moved to (\(x),\(y))") } if (sender.state == .ended) { print ("pan ended at (\(x),\(y))") } }

slide-10
SLIDE 10

Custom Gestures

  • Create subclass of UIGestureRecognizer
  • Import UIKit.UIGestureRecognizerSubclass

– Defines methods and properties to override

  • Override main gesture functions

– touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) – touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) – touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) – touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) – reset()

Mobile Application Development in iOS 10

slide-11
SLIDE 11

Custom Gesture Example: Backslash

Mobile Application Development in iOS 11

slide-12
SLIDE 12

Backslash Custom Gesture (1)

Mobile Application Development in iOS 12

import UIKit import UIKit.UIGestureRecognizerSubclass class BackslashGestureRecognizer: UIGestureRecognizer { var initialPoint: CGPoint! var previousPoint: CGPoint!

  • verride func touchesBegan(_ touches: Set<UITouch>,

with event: UIEvent) { print("backslash: touchesBegan") let touch = touches.first if let point = touch?.location(in: self.view) { initialPoint = point previousPoint = point state = .began } }

slide-13
SLIDE 13

Backslash Custom Gesture (2)

Mobile Application Development in iOS 13

  • verride func touchesMoved(_ touches: Set<UITouch>,

with event: UIEvent) { print("backslash: touchesMoved") let touch = touches.first if let point = touch?.location(in: self.view) { if ((point.x previousPoint.x) && (point.y previousPoint.y)) { previousPoint = point state = .changed } else { state = .failed } } }

slide-14
SLIDE 14

Backslash Custom Gesture (3)

Mobile Application Development in iOS 14

  • verride func touchesEnded(_ touches: Set<UITouch>,

with event: UIEvent) { print("backslash: touchesEnded") let touch = touches.first if let point = touch?.location(in: self.view) { if (point != initialPoint) { state = .ended } else { state = .failed } } }

slide-15
SLIDE 15

Backslash Custom Gesture (4)

Mobile Application Development in iOS 15

  • verride func touchesCancelled(_ touches: Set<UITouch>,

with event: UIEvent) { print("backslash: touchesCancelled") state = .cancelled }

  • verride func reset() {

print("backslash: reset") } }

slide-16
SLIDE 16

Backslash Custom Gesture (5)

Mobile Application Development in iOS 16

// In viewDidLoad... let backslashGestureRecognizer = BackslashGestureRecognizer(target: self, action: #selector(handleBackslash)) backslashGestureRecognizer.delegate = self self.view.addGestureRecognizer(backslashGestureRecognizer) // In ViewController... @objc func handleBackslash(_ sender: BackslashGestureRecognizer) { if sender.state == .ended { print("backslash detected") } }

Remember to conform ViewController to UIGestureRecognizerDelegate

slide-17
SLIDE 17

Drawing Boxes

Mobile Application Development in iOS 17

var boxViews: [UIView] = [] func drawBox(_ point: CGPoint) { let boxRect = CGRect(x: point.x, y: point.y, width: 5.0, height: 5.0) let boxView = UIView(frame: boxRect) boxView.backgroundColor = UIColor.red self.view?.addSubview(boxView) boxViews.append(boxView) } func clearBoxes() { for boxView in boxViews { boxView.removeFromSuperview() } boxViews.removeAll() }

slide-18
SLIDE 18

Multiple Gestures

  • Simultaneous gestures

– func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool

  • Gesture preference

– func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool

Mobile Application Development in iOS 18

slide-19
SLIDE 19

Multiple Gestures

Mobile Application Development in iOS 19

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { if gestureRecognizer is BackslashGestureRecognizer { if otherGestureRecognizer is UITapGestureRecognizer { return true } } return false } func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)

  • > Bool {

if gestureRecognizer is BackslashGestureRecognizer { if otherGestureRecognizer is UITapGestureRecognizer { return true } } return false }

slide-20
SLIDE 20

Multiple Gestures

  • Preserving interactions with view elements

– E.g., button taps will go to gesture, not UIButton – Use gestureRecognizer: shouldReceive

Mobile Application Development in iOS 20

// In ViewController func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { if touch.view is UIButton { return false } // Add more checks for other types of view elements, as needed return true }

slide-21
SLIDE 21

Resources

  • Human Interface Guidelines: Gestures

– developer.apple.com/design/human-interface- guidelines/ios/user-interaction/gestures

  • UIGestureRecognizer API Reference

– developer.apple.com/documentation/uikit/uigesturerecognizer

  • Implementing a custom gesture recognizer

– developer.apple.com/documentation/uikit/touches_presses_an d_gestures/implementing_a_custom_gesture_recognizer

Mobile Application Development in iOS 21