스탠포드 iOS 강의노트 L5

Lecture 5

Lecture 5: Gestures

Course Description Updated for iOS 10 and Swift. Tools and APIs required to build applications for the iPhone and iPad platforms using the iOS SDK. User interface design for mobile devices and unique user interactions using multi-touch technologies. Object-oriented design using model-view-controller paradigm, memory management, Swift programming language. Other topics include: object-oriented database API, animation, mobile device power management, multi-threading, networking and performance considerations.

Gestures

UIPinchGestureRecognizer

// Pinch - UIPinchGestureRecognizer
@objc func changeScale(byReactingTo pinchRecognizer: UIPinchGestureRecognizer)
{
    switch pinchRecognizer.state {
    case .changed, .ended:
        scale *= pinchRecognizer.scale
        pinchRecognizer.scale = 1
    default:
        break
    }
}

UIRotationGestureRecognizer

UISwipeGestureRecognizer

UITapGestureRecognizer

@objc func toggleEyes(byReactingTo tapRecognizer: UITapGestureRecognizer)
{
    if tapRecognizer.state == .ended {
        let eyes: FacialExpression.Eyes = (expression.eyes == .closed) ? .open : .closed
        expression = FacialExpression(eyes: eyes, mouth: expression.mouth)
    }
}

Sample Code

@IBOutlet weak var skullView: SkullView! {
    didSet {
        let handler = #selector(skullView.changeScale(byReactingTo:))
        
        let pinchRecognizer = UIPinchGestureRecognizer(target: skullView, action: handler)
        skullView.addGestureRecognizer(pinchRecognizer)
        
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.toggleEyes(byReactingTo:)))
        skullView.addGestureRecognizer(tapRecognizer)
        
        let swipeUpRecognizer = UISwipeGestureRecognizer(target:self, action: #selector(increaseHappiness))
        swipeUpRecognizer.direction = .up
        skullView.addGestureRecognizer(swipeUpRecognizer)
        
        let swipeDownRecognizer = UISwipeGestureRecognizer(target:self, action: #selector(decreaseHappiness))
        swipeDownRecognizer.direction = .down
        skullView.addGestureRecognizer(swipeDownRecognizer)
        
        updateUI()
    }
}