You Can Quickly Read a Hidden Comment by Pointing to a Cellã¢â‚¬â„¢s Comment Indicator
In this cookbook-fashion tutorial you will learn how to move a tabular array view row past using a long printing gesture, like in Apple tree's Weather App.
You can either add this code directly to your project, add together information technology to a starter project I've made for you, or simply download the completed example.
What practise you need?
- UILongGestureRecognizer
- UITableView (it can be substituted with UICollectionView)
- UITableViewController (it can be substituted with UIViewController or UICollectionViewController)
- 5 minutes.
How to do it?
Outset by adding a UILongGestureRecognizer
to the table view. You can practice this in viewDidLoad
for your table view controller.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cocky action:@selector(longPressGestureRecognized:)]; [self.tableView addGestureRecognizer:longPress];
Adjacent add the action method for gesture recognizer. It should start by getting the location of the long press in the table view, and discover the corresponding index path for the prison cell at that location. Remember the index path might be naught (for example, if the user taps and holds on a section header of the table view).
- (IBAction)longPressGestureRecognized:(id)sender { UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender; UIGestureRecognizerState land = longPress.state; CGPoint location = [longPress locationInView:cocky.tableView]; NSIndexPath *indexPath = [cocky.tableView indexPathForRowAtPoint:location]; // More than coming soon... }
Next you'll handle the UIGestureRecognizerStateBegan
example. If at that place is a valid (non-nil) index path, get the corresponding UITableViewCell
and take a snapshot view of the table view cell using a helper method. And so add together the new snapshot view to the table view and center it on the respective cell.
For a better user feel and a more natural effect fade out the original cell, fade in the snapshot view, make the snapshot view slightly bigger, and showtime its Y coordinate to align with gesture's location Y. This way information technology appears every bit if the cell is popped out of the tabular array view, floating over it and snapped to user'southward finger.
static UIView *snapshot = null; ///< A snapshot of the row user is moving. static NSIndexPath *sourceIndexPath = nil; ///< Initial index path, where gesture begins. switch (land) { case UIGestureRecognizerStateBegan: { if (indexPath) { sourceIndexPath = indexPath; UITableViewCell *cell = [cocky.tableView cellForRowAtIndexPath:indexPath]; // Accept a snapshot of the selected row using helper method. snapshot = [cocky customSnapshotFromView:cell]; // Add the snapshot as subview, centered at cell's center... __block CGPoint center = cell.heart; snapshot.center = heart; snapshot.alpha = 0.0; [self.tableView addSubview:snapshot]; [UIView animateWithDuration:0.25 animations:^{ // Offset for gesture location. center.y = location.y; snapshot.center = center; snapshot.transform = CGAffineTransformMakeScale(one.05, 1.05); snapshot.blastoff = 0.98; // Fade out. prison cell.alpha = 0.0; } completion:^(BOOL finished) { cell.hidden = Aye; }]; } break; } // More coming before long... }
// Add together this at the end of your .m file. It returns a customized snapshot of a given view. - (UIView *)customSnapshotFromView:(UIView *)inputView { // Brand an paradigm from the input view. UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0); [inputView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *paradigm = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Create an image view. UIView *snapshot = [[UIImageView alloc] initWithImage:epitome]; snapshot.layer.masksToBounds = NO; snapshot.layer.cornerRadius = 0.0; snapshot.layer.shadowOffset = CGSizeMake(-v.0, 0.0); snapshot.layer.shadowRadius = 5.0; snapshot.layer.shadowOpacity = 0.4; return snapshot; }
Equally gesture moves (the UIGestureRecognizerStateChanged
example), move the snapshot view by offsetting its Y coordinate merely. If the gesture moves enough that its location corresponds to a different index path, tell the tabular array view to move the rows. At the same time, you should update your data source too:
example UIGestureRecognizerStateChanged: { CGPoint middle = snapshot.center; centre.y = location.y; snapshot.center = heart; // Is destination valid and is information technology different from source? if (indexPath && ![indexPath isEqual:sourceIndexPath]) { // ... update data source. [cocky.objects exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row]; // ... move the rows. [cocky.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath]; // ... and update source so it is in sync with UI changes. sourceIndexPath = indexPath; } break; } // More coming soon...
Finally, when the gesture either ends or cancels, both the table view and data source are upward to engagement. All you lot have to do is removing the snapshot view from table view and reverting the fadeout.
For a better user experience, fade out the snapshot view and brand it smaller to match the size of the prison cell. It appears every bit if the cell drops back into its place.
default: { // Make clean up. UITableViewCell *jail cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath]; cell.hidden = NO; cell.blastoff = 0.0; [UIView animateWithDuration:0.25 animations:^{ snapshot.center = prison cell.center; snapshot.transform = CGAffineTransformIdentity; snapshot.alpha = 0.0; // Undo fade out. cell.alpha = 1.0 } completion:^(BOOL finished) { sourceIndexPath = nil; [snapshot removeFromSuperview]; snapshot = cypher; }]; break; }
That'south it; build and run your project, and you tin now utilize a long press gesture to reorder your table view cells!
You lot can checkout the completed project here at GitHub.
How to apply this with UICollectionView?
Assuming you accept already a sample project that uses UICollectionView
, you can easily prefer the above code for that project. All you have to practise is replacing self.tableView
with cocky.collectionView
and updating calls to become and movement UICollectionViewCell
.
To do an practice, checkout the starter project of UICollectionView from GitHub and add tap-and-hold gesture to it for re-ordering cells. Yous can checkout the UICollectionView answer here from GitHub.
Where to become from here?
We hope you enjoyed this cookbook article! If you would like to see more cookbook-style articles like this in the future, delight let the states know.
Also, if you'd prefer to learn about this in video format, nosotros have a video tutorial for that.
Too, I'd honey to hear your comments or questions in the forums below!
Source: https://www.raywenderlich.com/2474-cookbook-moving-table-view-cells-with-a-long-press-gesture
Post a Comment for "You Can Quickly Read a Hidden Comment by Pointing to a Cellã¢â‚¬â„¢s Comment Indicator"