iOS UISwipeGestureRecognizer: Two Ways in Swift

I’ve been building a small sample iOS app and in learning how to implement UISwipeGestureRecognizer, found two different approaches. None of the resources I found online were overly explicit in both approaches, so documenting here…mostly for my own future reference.

Option 1: Storyboard

Xcode makes it easy to add a gesture recognizer to your app interface. Simply drag the appropriate gesture recognizer from the palette onto Storyboard, being sure to select the UI element that you would like to listen for the gesture. In my case, the entire view of my ViewController:

Screen Shot 2015-11-18 at 2.34.27 PM

Next, select the gesture recognizer and Ctrl+Drag to your ViewController class. Select the settings as shown below in the pop-up. You can now implement the response to your gesture.

Screen Shot 2015-11-18 at 2.36.38 PM

The Storyboard inspector also allows you to select swipe direction, etc. with the gesture recognizer selected.

Option 2: Code

Adding a gesture recognizer (in this case, a swipe gesture recognizer) requires just a few lines of code in your ViewController, typically in your viewDidLoad method.


let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "doSomething:")
swipeRight.direction = .Right
self.view.addGestureRecognizer(swipeRight)

The only item left is to implement the handler for gesture, which is mostly identical to Option 1:


func doSomething(gestureRecognizer:UIGestureRecognizer) {
print("doSomething called")
}

My preference is Option 1. Although the implementation is spread across two files, keeping all view-related bits in the Storyboard feels cleaner, but either approach will work.

Advertisements

Yahoo News Digest

This post is something of a departure from my normal ramblings, but I didn’t want the release of Yahoo’s News Digest app to pass without note.  In a world of never-ending news feeds and infinite scrolling, returning to a fully consumable, information experience has been a joy over the past 24 hours.

News Digest delivers two daily digest, each with eight articles on the topics of the day — this morning focused on Chris Christie and Dennis Rodman.  The articles are short (more on that momentarily), reasonably informational, and contain links to other sources.  It’s a nice approach to mobile news in our increasingly attention-strained world.

As much as I’ve enjoyed News Digest in its first day of release, I do have a few quibbles:

  1. The articles are almost to short.  Most of the stories thus far leave me wanting for more.
  2. Proofread, please.  I’ve already noted a couple of simple grammatical errors that should be caught in such a short format.

http://itunes.apple.com/us/app/yahoo-news-atom/id784982356?ls=1&mt=8


iOS Developer Tools

iOS Developer Tools

Great list of various tools for iOS Development.