Screen Edge Pan Gesture Tutorial in iOS8 with Swift

A Screen Edge Pan Gesture is a panning gesture that start near an edge of the screen. When you drag from top to bottom in iOS8 the Notification Screen is displayed. In this tutorial a soccer bal will rotate with a Screen Edge Pan Gesture. This tutorial is built with iOS 8.1 with Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftScreenEdgePanGesturesTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

We need an image of the soccer ball, download it and add it to the project. Go to the Storyboard and drag an Image View to the main view. Select the Image View, go to the Size Inspector and fill in the following values.

Select the Image View again and go to the Property Inspector. In the Image View section select the soccer ball image in the Image field.

Select the main View and change the background color to green, The Storyboard should look like this.

Next, we need to add the Auto Layout values. Ctrl and Drag from inside the image to a little bit to the left, while staying inside the image. Hold down the Ctrl key and select both the Width and Height options.

Ctrl and Drag from the image to the top of the view. Select "Top Space to Top Layout Guide" and "Center Horizontally in Container"

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Image to the ViewController class and create the following Outlet.

Go to the ViewController.swift file and add the following properties

var screenEdgeRecognizer: UIScreenEdgePanGestureRecognizer!
var currentRadius:CGFloat = 0.0

Change the viewDidLoad method to

override func viewDidLoad() {
    screenEdgeRecognizer = UIScreenEdgePanGestureRecognizer(target: self,
    action: "rotateBall:")
    screenEdgeRecognizer.edges = .Left

a UIScreenEdgePanGestureRecognizer object is initialized. When this gesture is detected from the left side to inside of the view, the rotateBall method is called.

func rotateBall(sender: UIScreenEdgePanGestureRecognizer) {
  // 1  
  if sender.state == .Ended {
    // 2  
    if self.currentRadius==360.0 {  
    UIView.animateWithDuration(1.0, animations: {
       self.currentRadius += 90.0
       self.imageView.transform = CGAffineTransformMakeRotation((self.currentRadius * CGFloat(M_PI)) / 180.0)
  1. if the gesture is ended the code inside the if-block is executed
  2. if the ball has rotated a whole circle the radius is reset to 0.
  3. An animation is performed where the ball rotates 90 degrees in 1 second.

Build and Run the project, Make a Screen Edge Pan Gesture from the left side to rotate the ball.

You can download the source code of the IOS8SwiftScreenEdgePanGesturesTutorial at the ioscreator repository on Github.