Swipe gestures in a UITableView

When we have a UITableView, sometimes we want to add some actions when the user swipes over a row of the table. We can capture swipes to the left, right, down or up directions.
To capture both left and right swipe gestures, add the following code to your UITableViewController in the viewDidLoad method.

UISwipeGestureRecognizer *gestureLeft = [[UISwipeGestureRecognizer alloc] 
     initWithTarget:self 
     action:@selector(handleSwipeLeft:)];
[gestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.tableView addGestureRecognizer:gestureLeft];
    
UISwipeGestureRecognizer *gestureRight = [[UISwipeGestureRecognizer alloc] 
     initWithTarget:self 
     action:@selector(handleSwipeRight:)];
[gestureRight setDelegate:self];
[gestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableView addGestureRecognizer:gestureRight];

The action parameter indicates the method that will be executed when the gesture is recognized. We have to include these methods in our controller too.


- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    
    // Action...
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    
    // Action...
}

The method locationInView returns the location of the gesture, represented as a point in the local coordinate system of the view. Using the indexPathForRowAtPoint method of the UITableView object, we can obtain the row of the gesture.

We could use also the same action method to handle all the swipe gestures. Change the action methods from handleSwipeLeft and handleSwipeRight to handleSwipe, and add the following method instead.

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];

    switch ([gestureRecognizer direction]) {
        case UISwipeGestureRecognizerDirectionLeft:
            // Action
            break;
        case UISwipeGestureRecognizerDirectionRight:
            // Action
            break;
        default:
            break;
    }
}

Read More

Location Manager in iOS 8

The Core Location service in iOS 8 includes now two different modes of authorization: While Using the App and Always.

Location permissions

In our code application we must request the user to accept one of these modes.

  • When In Use. This mode allows your app to use the location services when the app is in the foreground.
    if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
       [self.locationManager requestWhenInUseAuthorization];
    
  • Always. This mode allows your app to use the location services when the app is in the foreground and in the background.
    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
       [self.locationManager requestAlwaysAuthorization];
    

Finally, we have to add in the Info.plist file of our app the values for the keys NSLocationWhenInUseUsageDescription or/and NSLocationAlwaysUsageDescription. These values contain the messages that will be displayed to the user when requesting the authorization.

info.plist properties

Read More