UIPickerView, detect "rolling wheel" start and stop? - ios

I just discovered that if I do the following:
Click the button that animates a UIPickerView into my view
Quickly start the wheel rolling towards, then past, the last item
Dismiss the view with a button
Then it has not yet selected the last item yet.
I tried this by simply outputting to the console whenever the didSelectRow method was fired, and it fires when the wheel stabilizes on the last item.
Can I detect that the wheel is still rolling, so that I can delay checking it for a selected value until it stabilizes?
If it matters, I'm programming in MonoTouch, but I can read Objective-C code well enough to reimplement it, if you have a code example that is.

As animation keys don't work, I wrote this simple function that works for detecting if a UIPickerView is currently moving.
-(bool) anySubViewScrolling:(UIView*)view
{
if( [ view isKindOfClass:[ UIScrollView class ] ] )
{
UIScrollView* scroll_view = (UIScrollView*) view;
if( scroll_view.dragging || scroll_view.decelerating )
{
return true;
}
}
for( UIView *sub_view in [ view subviews ] )
{
if( [ self anySubViewScrolling:sub_view ] )
{
return true;
}
}
return false;
}
It ends up returning true five levels deep.

Swift 4 (updated) version with extension of #DrainBoy answers
extension UIView {
func isScrolling () -> Bool {
if let scrollView = self as? UIScrollView {
if (scrollView.isDragging || scrollView.isDecelerating) {
return true
}
}
for subview in self.subviews {
if ( subview.isScrolling() ) {
return true
}
}
return false
}
}

Since animationKeys seems to not work anymore, I have another solution. If you check the subviews of UIPickerView, you'll see that there is a UIPickerTableView for each component.
This UIPickerTableView is indeed a subclass of UITableView and of course of UIScrollView. Therefore, you can check its contentOffset value to detect a difference.
Besides, its scrollViewDelegate is nil by default, so I assume you can safely set an object of yours to detect scrollViewWillBeginDragging, scrollViewDidEndDecelerating, etc.
By keeping a reference to each UIPickerTableView, you should be able to implement an efficient isWheelRolling method.

Expanded #iluvatar_GR answer
extension UIView {
func isScrolling () -> Bool {
if let scrollView = self as? UIScrollView {
if (scrollView.isDragging || scrollView.isDecelerating) {
return true
}
}
for subview in self.subviews {
if ( subview.isScrolling() ) {
return true
}
}
return false
}
func waitTillDoneScrolling (completion: #escaping () -> Void) {
var isMoving = true
DispatchQueue.global(qos: .background).async {
while isMoving == true {
isMoving = self.isScrolling()
}
DispatchQueue.main.async {
completion()}
}
}
}

Expanded #iluvatar_GR, #Robert_at_Nextgensystems answer
Used Gesture, UIScrollView isDragging or isDecelerating.
// Call it every time when Guesture action.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
// Changes the button name to scrolling at the start of scrolling.
DispatchQueue.main.async {
self._button.setTitle("Scrolling...", for: .normal)
self._button.isEnabled = false
self._button.backgroundColor = Utils.hexStringToUIColor(hex: "FF8FAE")
}
// Indication according to scrolling status
_datePicker.waitTillDoneScrolling(completion: {
print("completion")
DispatchQueue.main.async {
self._button.setTitle("Completion", for: .normal)
self._button.isEnabled = true
self._button.backgroundColor = Utils.hexStringToUIColor(hex: "7CB0FF")
}
})
}
[SWIFT4] Share Example Source link!
enter Sample Source link
Reference : How to recognize swipe in all 4 directions

I think you can just check if the UIPickerView is in the middle of animating and wait for it to stop. This was answered here link

You can use a SwipeGestureRecognizer on the picker.
I assume this is not a perfect solution at all.
- (void)viewDidLoad
{
[super viewDidLoad];
_pickerSwipeGestureRecognizer.delegate = self;
[_pickerSwipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp)];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if([gestureRecognizer isEqual:_pickerSwipeGestureRecognizer]){
NSLog(#"start");
}
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"end");
}

Related

Hit Test rotated view

I need to write a function that will return true if given point is inside any of subviews hierarchy of view A. A can be transformed (rotated), and has clipToBounds disabled. It's subviews also can have clipToBounds disabled, and true must returned if any of subviews is tapped. I have something that almost works, but fails when view is rotated. Can someone suggest how to fix this?
extension UIView {
func hitTestIgnoringBounds(_ point: CGPoint) -> Bool {
guard !isHidden && alpha > 0 else { return false }
let subPoint = frame.origin - point
return frame.contains(point) || subviews.contains {
return $0.hitTestIgnoringBounds(subPoint)
}
}
}
I knew it should have been easy, but could not make it work. I made a stupid mistake - my code was adding sublayers instead of subviews. When I switched to subviews, everything worked fine:
func viewOrSubviewsContaintPoint(_ point: CGPoint) -> Bool {
let convertedPoint = convert(point, from: superview)
let containsPoint = bounds.contains(convertedPoint)
var subviewsContainsPoint: Bool {
return subviews.contains { $0.viewOrSubviewsContaintPoint(convertedPoint) }
}
return containsPoint || subviewsContainsPoint
}

iMessage App - Using a UITextField in compact presentation style

I'm simply trying to use a UITextField for my iMessage App.
The problem is with when it is in compact mode (MSMessagesAppPresentationStyleCompact) once you select the textfield, all the views disappear. It seems to work fine in expanded mode.
What is the proper way of using a textfield in compact mode? Thanks
It appears that you can only use textfields while in expanded mode, so you'll need to implement something like this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if([self presentationStyle] == MSMessagesAppPresentationStyleCompact) {
[self requestPresentationStyle:MSMessagesAppPresentationStyleExpanded];
self.didRequestKeyboard = YES;
return NO;
}
return YES;
}
-(void)didTransitionToPresentationStyle:(MSMessagesAppPresentationStyle)presentationStyle {
// Called after the extension transitions to a new presentation style.
// Use this method to finalize any behaviors associated with the change in presentation style.
if(presentationStyle == MSMessagesAppPresentationStyleExpanded){
if(self.didRequestKeyboard){
[self.textField becomeFirstResponder];
self.didRequestKeyboard = NO;
}
}
}
I struggled with this problem (still present as of iOS 10.2) and ended on this workaround:
fileprivate class TextField: UITextField {
override var canBecomeFirstResponder: Bool {
if let viewController = viewController as? MSMessagesAppViewController,
viewController.presentationStyle == .compact {
viewController.requestPresentationStyle(.expanded)
return false
}
return super.canBecomeFirstResponder
}
}
I say "workaround" because I feel this is a problem Apple should solve ergo this solution should be temporary. This implementation, as an alternative to the original answer's, is segregated and can be easily ripped out.
Same solution but using blocks
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if presentationStyle != .expanded {
didTransitionHandler = {
textField.becomeFirstResponder()
self.didTransitionHandler = nil
}
requestPresentationStyle(.expanded)
return false
}
return true
}

How to disable scrolling entirely in a WKWebView?

I know this looks like a simple question one can simply say:
webview.scrollView.scrollEnabled = NO;
webview.scrollView.panGestureRecognizer.enabled = NO;
webview.scrollView.bounces = NO;
or even
for (UIView* subview in webview.subviews) {
if ([subview respondsToSelector:#selector(setScrollEnabled:)]) {
[(id)subview setScrollEnabled:enabled];
}
if ([subview respondsToSelector:#selector(panGestureRecognizer)]) {
[[(id)subview panGestureRecognizer] setEnabled:enabled];
}
}
but while it does prevent scolling (in the contentOffset meaning) inside the WKWebviewit doesn't prevent it from receiving pan gesture events involving scrolling.
So articles like those of the Huffington Post, which have javascript included to automatically change articles when the user scrolls left or right still get that behavior.
How can I prevent this ?
Before Swift 3
You can simply disable scroll on its implicit scrollView
webView.scrollView.scrollEnabled = false
Swift 3
webView.scrollView.isScrollEnabled = false
Took me a while but I figured out a way of doing this.
I had to remove a private gesture recognizer within a private subview of the WKWebView.
I had a category on WKWebView to do so:
#implementation WKWebView (Scrolling)
- (void)setScrollEnabled:(BOOL)enabled {
self.scrollView.scrollEnabled = enabled;
self.scrollView.panGestureRecognizer.enabled = enabled;
self.scrollView.bounces = enabled;
// There is one subview as of iOS 8.1 of class WKScrollView
for (UIView* subview in self.subviews) {
if ([subview respondsToSelector:#selector(setScrollEnabled:)]) {
[(id)subview setScrollEnabled:enabled];
}
if ([subview respondsToSelector:#selector(setBounces:)]) {
[(id)subview setBounces:enabled];
}
if ([subview respondsToSelector:#selector(panGestureRecognizer)]) {
[[(id)subview panGestureRecognizer] setEnabled:enabled];
}
// here comes the tricky part, desabling
for (UIView* subScrollView in subview.subviews) {
if ([subScrollView isKindOfClass:NSClassFromString(#"WKContentView")]) {
for (id gesture in [subScrollView gestureRecognizers]) {
if ([gesture isKindOfClass:NSClassFromString(#"UIWebTouchEventsGestureRecognizer")])
[subScrollView removeGestureRecognizer:gesture];
}
}
}
}
}
#end
Hope this helps anyone some day.
Credit and many thanks to apouche for the Obj-C code. In case anybody else has the same problem, here is the working solution adapted for Swift 2
extension WKWebView {
func setScrollEnabled(enabled: Bool) {
self.scrollView.scrollEnabled = enabled
self.scrollView.panGestureRecognizer.enabled = enabled
self.scrollView.bounces = enabled
for subview in self.subviews {
if let subview = subview as? UIScrollView {
subview.scrollEnabled = enabled
subview.bounces = enabled
subview.panGestureRecognizer.enabled = enabled
}
for subScrollView in subview.subviews {
if subScrollView.dynamicType == NSClassFromString("WKContentView")! {
for gesture in subScrollView.gestureRecognizers! {
subScrollView.removeGestureRecognizer(gesture)
}
}
}
}
}
}
finally
self.webView.scrollView.userInteractionEnabled = NO
Here is a Swift 3 version:
extension WKWebView {
func setScrollEnabled(enabled: Bool) {
self.scrollView.isScrollEnabled = enabled
self.scrollView.panGestureRecognizer.isEnabled = enabled
self.scrollView.bounces = enabled
for subview in self.subviews {
if let subview = subview as? UIScrollView {
subview.isScrollEnabled = enabled
subview.bounces = enabled
subview.panGestureRecognizer.isEnabled = enabled
}
for subScrollView in subview.subviews {
if type(of: subScrollView) == NSClassFromString("WKContentView")! {
for gesture in subScrollView.gestureRecognizers! {
subScrollView.removeGestureRecognizer(gesture)
}
}
}
}
}
}
I found that I had to make my view controller a UIScrollViewDelegate then add this function to prevent scrolling.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
Here is a C# extension for WKWebView based on alain.s's swift solution (based on apouche's solution) for those of us using Xamarin. I am using this in my app.
Notable differences is that I check if subviews exist before looping and instead of dynamically looking for a "WKContentView" (something I'm not sure is even possible in Xamarin) I simply check if each subview has GestureRecognizers and remove them. This will obviously disable all types of gestures so consider this if you expect any user interaction with the web content.
public static class WKWebViewExtension
{
public static void DisableScroll(this WebKit.WKWebView webView)
{
webView.ScrollView.ScrollEnabled = false;
webView.ScrollView.PanGestureRecognizer.Enabled = false;
webView.ScrollView.Bounces = false;
if (webView.Subviews != null)
{
foreach (var subView in webView.Subviews)
{
if (subView is UIScrollView)
{
UIScrollView subScrollView = (UIScrollView)subView;
subScrollView.ScrollEnabled = false;
subScrollView.Bounces = false;
subScrollView.PanGestureRecognizer.Enabled = false;
}
if (subView.Subviews != null)
{
foreach (var subScrollView in subView.Subviews)
{
if (subScrollView.GestureRecognizers != null)
{
foreach (var gesture in subScrollView.GestureRecognizers)
{
subScrollView.RemoveGestureRecognizer(gesture);
}
}
}
}
}
}
}
}
Here's a swift version if anyone's still having trouble with this issue
let subviews = self.theWebView.scrollView.subviews
for subview in subviews{
if(subview.isKindOfClass(NSClassFromString("WKContentView"))){
if let recognizers = subview.gestureRecognizers {
for recognizer in recognizers! {
if recognizer.isKindOfClass(NSClassFromString("UIWebTouchEventsGestureRecognizer")){
subview.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}
}
Swift 5
disableScrollView(self.webView)
func disableScrollView(_ view: UIView) {
(view as? UIScrollView)?.isScrollEnabled = false
view.subviews.forEach { disableScrollView($0) }
}
Try to disable scrollView zoom in this way:
CGFloat zoomScale = webview.scrollView.zoomScale;
webview.scrollView.maximumZoomScale = zoomScale;
webview.scrollView.minimumZoomScale = zoomScale;

Trying to loop through UIView and all subviews to get the first responder, not working

I am trying to find the current focused First Responder by looping through the main UIView and all of it's SubViews, and all of it's SubViews through recursion, but I am coming up with nil.
extension UIView {
func getCurrentFirstResponder() -> AnyObject? {
if self.isFirstResponder() {
return self
}
for subView: UIView in self.subviews as [UIView] {
if subView.isFirstResponder() {
return subView
}
else {
subView.getCurrentFirstResponder()
}
}
return nil
}
}
let focusedView = self.view.getCurrentFirstResponder() as? UIView
Does this look correct? Why am I getting a nil view when I use this?
You code doesn't return anything in case the recursive call to subView.getCurrentFirstResponder() actually finds a first responder.
Try this:
for subView: UIView in self.subviews as [UIView] {
if subView.isFirstResponder() {
return subView
}
else {
if let sub = subView.getCurrentFirstResponder() {
return sub;
}
}
}
return nil

How to implement UITableView`s swipe to delete for UICollectionView

I just like to ask how can I implement the same behavior of UITableView`s swipe to delete in UICollectionView. I am trying to find a tutorial but I cannot find any.
Also, I am using PSTCollectionView wrapper to support iOS 5.
Thank you!
Edit:
The swipe recognizer is already good.
What I need now is the same functionality as UITableView's when cancelling the Delete mode, e.g. when user taps on a cell or on a blank space in the table view (that is, when user taps outside of the Delete button).
UITapGestureRecognizer won't work, since it only detects taps on release of a touch.
UITableView detects a touch on begin of the gesture (and not on release), and immediately cancels the Delete mode.
There is a simpler solution to your problem that avoids using gesture recognizers. The solution is based on UIScrollView in combination with UIStackView.
First, you need to create 2 container views - one for the visible part of the cell and one for the hidden part. You’ll add these views to a UIStackView. The stackView will act as a content view. Make sure that the views have equal widths with stackView.distribution = .fillEqually.
You’ll embed the stackView inside a UIScrollView that has paging enabled. The scrollView should be constrained to the edges of the cell. Then you’ll set the stackView’s width to be 2 times the scrollView’s width so each of the container views will have the width of the cell.
With this simple implementation, you have created the base cell with a visible and hidden view. Use the visible view to add content to the cell and in the hidden view you can add a delete button. This way you can achieve this:
I've set up an example project on GitHub. You can also read more about this solution here.
The biggest advantage of this solution is the simplicity and that you don't have to deal with constraints and gesture recognizers.
Its very simple..You need to add a customContentView and customBackgroundView behind the customContentView.
After that and you need to shift the customContentViewto the left as user swipes from right to left. Shifting the view makes visible to the customBackgroundView.
Lets Code:
First of all you need to add panGesture to your UICollectionView as
override func viewDidLoad() {
super.viewDidLoad()
self.panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panThisCell))
panGesture.delegate = self
self.collectionView.addGestureRecognizer(panGesture)
}
Now implement the selector as
func panThisCell(_ recognizer:UIPanGestureRecognizer){
if recognizer != panGesture{ return }
let point = recognizer.location(in: self.collectionView)
let indexpath = self.collectionView.indexPathForItem(at: point)
if indexpath == nil{ return }
guard let cell = self.collectionView.cellForItem(at: indexpath!) as? CustomCollectionViewCell else{
return
}
switch recognizer.state {
case .began:
cell.startPoint = self.collectionView.convert(point, to: cell)
cell.startingRightLayoutConstraintConstant = cell.contentViewRightConstraint.constant
if swipeActiveCell != cell && swipeActiveCell != nil{
self.resetConstraintToZero(swipeActiveCell!,animate: true, notifyDelegateDidClose: false)
}
swipeActiveCell = cell
case .changed:
let currentPoint = self.collectionView.convert(point, to: cell)
let deltaX = currentPoint.x - cell.startPoint.x
var panningleft = false
if currentPoint.x < cell.startPoint.x{
panningleft = true
}
if cell.startingRightLayoutConstraintConstant == 0{
if !panningleft{
let constant = max(-deltaX,0)
if constant == 0{
self.resetConstraintToZero(cell,animate: true, notifyDelegateDidClose: false)
}else{
cell.contentViewRightConstraint.constant = constant
}
}else{
let constant = min(-deltaX,self.getButtonTotalWidth(cell))
if constant == self.getButtonTotalWidth(cell){
self.setConstraintsToShowAllButtons(cell,animate: true, notifyDelegateDidOpen: false)
}else{
cell.contentViewRightConstraint.constant = constant
cell.contentViewLeftConstraint.constant = -constant
}
}
}else{
let adjustment = cell.startingRightLayoutConstraintConstant - deltaX;
if (!panningleft) {
let constant = max(adjustment, 0);
if (constant == 0) {
self.resetConstraintToZero(cell,animate: true, notifyDelegateDidClose: false)
} else {
cell.contentViewRightConstraint.constant = constant;
}
} else {
let constant = min(adjustment, self.getButtonTotalWidth(cell));
if (constant == self.getButtonTotalWidth(cell)) {
self.setConstraintsToShowAllButtons(cell,animate: true, notifyDelegateDidOpen: false)
} else {
cell.contentViewRightConstraint.constant = constant;
}
}
cell.contentViewLeftConstraint.constant = -cell.contentViewRightConstraint.constant;
}
cell.layoutIfNeeded()
case .cancelled:
if (cell.startingRightLayoutConstraintConstant == 0) {
self.resetConstraintToZero(cell,animate: true, notifyDelegateDidClose: true)
} else {
self.setConstraintsToShowAllButtons(cell,animate: true, notifyDelegateDidOpen: true)
}
case .ended:
if (cell.startingRightLayoutConstraintConstant == 0) {
//Cell was opening
let halfOfButtonOne = (cell.swipeView.frame).width / 2;
if (cell.contentViewRightConstraint.constant >= halfOfButtonOne) {
//Open all the way
self.setConstraintsToShowAllButtons(cell,animate: true, notifyDelegateDidOpen: true)
} else {
//Re-close
self.resetConstraintToZero(cell,animate: true, notifyDelegateDidClose: true)
}
} else {
//Cell was closing
let buttonOnePlusHalfOfButton2 = (cell.swipeView.frame).width
if (cell.contentViewRightConstraint.constant >= buttonOnePlusHalfOfButton2) {
//Re-open all the way
self.setConstraintsToShowAllButtons(cell,animate: true, notifyDelegateDidOpen: true)
} else {
//Close
self.resetConstraintToZero(cell,animate: true, notifyDelegateDidClose: true)
}
}
default:
print("default")
}
}
Helper methods to update constraints
func getButtonTotalWidth(_ cell:CustomCollectionViewCell)->CGFloat{
let width = cell.frame.width - cell.swipeView.frame.minX
return width
}
func resetConstraintToZero(_ cell:CustomCollectionViewCell, animate:Bool,notifyDelegateDidClose:Bool){
if (cell.startingRightLayoutConstraintConstant == 0 &&
cell.contentViewRightConstraint.constant == 0) {
//Already all the way closed, no bounce necessary
return;
}
cell.contentViewRightConstraint.constant = -kBounceValue;
cell.contentViewLeftConstraint.constant = kBounceValue;
self.updateConstraintsIfNeeded(cell,animated: animate) {
cell.contentViewRightConstraint.constant = 0;
cell.contentViewLeftConstraint.constant = 0;
self.updateConstraintsIfNeeded(cell,animated: animate, completionHandler: {
cell.startingRightLayoutConstraintConstant = cell.contentViewRightConstraint.constant;
})
}
cell.startPoint = CGPoint()
swipeActiveCell = nil
}
func setConstraintsToShowAllButtons(_ cell:CustomCollectionViewCell, animate:Bool,notifyDelegateDidOpen:Bool){
if (cell.startingRightLayoutConstraintConstant == self.getButtonTotalWidth(cell) &&
cell.contentViewRightConstraint.constant == self.getButtonTotalWidth(cell)) {
return;
}
cell.contentViewLeftConstraint.constant = -self.getButtonTotalWidth(cell) - kBounceValue;
cell.contentViewRightConstraint.constant = self.getButtonTotalWidth(cell) + kBounceValue;
self.updateConstraintsIfNeeded(cell,animated: animate) {
cell.contentViewLeftConstraint.constant = -(self.getButtonTotalWidth(cell))
cell.contentViewRightConstraint.constant = self.getButtonTotalWidth(cell)
self.updateConstraintsIfNeeded(cell,animated: animate, completionHandler: {(check) in
cell.startingRightLayoutConstraintConstant = cell.contentViewRightConstraint.constant;
})
}
}
func setConstraintsAsSwipe(_ cell:CustomCollectionViewCell, animate:Bool,notifyDelegateDidOpen:Bool){
if (cell.startingRightLayoutConstraintConstant == self.getButtonTotalWidth(cell) &&
cell.contentViewRightConstraint.constant == self.getButtonTotalWidth(cell)) {
return;
}
cell.contentViewLeftConstraint.constant = -self.getButtonTotalWidth(cell) - kBounceValue;
cell.contentViewRightConstraint.constant = self.getButtonTotalWidth(cell) + kBounceValue;
self.updateConstraintsIfNeeded(cell,animated: animate) {
cell.contentViewLeftConstraint.constant = -(self.getButtonTotalWidth(cell))
cell.contentViewRightConstraint.constant = self.getButtonTotalWidth(cell)
self.updateConstraintsIfNeeded(cell,animated: animate, completionHandler: {(check) in
cell.startingRightLayoutConstraintConstant = cell.contentViewRightConstraint.constant;
})
}
}
func updateConstraintsIfNeeded(_ cell:CustomCollectionViewCell, animated:Bool,completionHandler:#escaping ()->()) {
var duration:Double = 0
if animated{
duration = 0.1
}
UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseOut], animations: {
cell.layoutIfNeeded()
}, completion:{ value in
if value{ completionHandler() }
})
}
I have created a sample project here in Swift 3.
It is a modified version of this tutorial.
In the Collection View Programming Guide for iOS, in the section Incorporating Gesture Support, the docs read:
You should always attach your gesture recognizers to the collection view itself and not to a specific cell or view.
So, I think it's not a good practice to add recognizers to UICollectionViewCell.
I followed a similar approach to #JacekLampart, but decided to add the UISwipeGestureRecognizer in the UICollectionViewCell's awakeFromNib function so it is only added once.
UICollectionViewCell.m
- (void)awakeFromNib {
UISwipeGestureRecognizer* swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeToDeleteGesture:)];
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeGestureRecognizer];
}
- (void)swipeToDeleteGesture:(UISwipeGestureRecognizer *)swipeGestureRecognizer {
if (swipeGestureRecognizer.state == UIGestureRecognizerStateEnded) {
// update cell to display delete functionality
}
}
As for exiting delete mode, I created a custom UIGestureRecognizer with an NSArray of UIViews. I borrowed the idea from #iMS from this question: UITapGestureRecognizer - make it work on touch down, not touch up?
On touchesBegan, if the touch point isn't within any of the UIViews, the gesture succeeds and delete mode is exited.
In this way, I am able to pass the delete button within the cell (and any other views) to the UIGestureRecognizer and, if the touch point is within the button's frame, delete mode will not exit.
TouchDownExcludingViewsGestureRecognizer.h
#import <UIKit/UIKit.h>
#interface TouchDownExcludingViewsGestureRecognizer : UIGestureRecognizer
#property (nonatomic) NSArray *excludeViews;
#end
TouchDownExcludingViewsGestureRecognizer.m
#import "TouchDownExcludingViewsGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#implementation TouchDownExcludingViewsGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.state == UIGestureRecognizerStatePossible) {
BOOL touchHandled = NO;
for (UIView *view in self.excludeViews) {
CGPoint touchLocation = [[touches anyObject] locationInView:view];
if (CGRectContainsPoint(view.bounds, touchLocation)) {
touchHandled = YES;
break;
}
}
self.state = (touchHandled ? UIGestureRecognizerStateFailed : UIGestureRecognizerStateRecognized);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.state = UIGestureRecognizerStateFailed;
}
#end
Implementation (in the UIViewController containing UICollectionView):
#import "TouchDownExcludingViewsGestureRecognizer.h"
TouchDownExcludingViewsGestureRecognizer *touchDownGestureRecognizer = [[TouchDownExcludingViewsGestureRecognizer alloc] initWithTarget:self action:#selector(exitDeleteMode:)];
touchDownGestureRecognizer.excludeViews = #[self.cellInDeleteMode.deleteButton];
[self.view addGestureRecognizer:touchDownGestureRecognizer];
- (void)exitDeleteMode:(TouchDownExcludingViewsGestureRecognizer *)touchDownGestureRecognizer {
// exit delete mode and disable or remove TouchDownExcludingViewsGestureRecognizer
}
You can try adding a UISwipeGestureRecognizer to each collection cell, like this:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = ...
UISwipeGestureRecognizer* gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(userDidSwipe:)];
[gestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:gestureRecognizer];
}
followed by:
- (void)userDidSwipe:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
//handle the gesture appropriately
}
}
With iOS 14, you can use UICollectionViewLayoutListConfiguration in conjunction with UICollectionViewCompositionalLayout to get this functionality natively for free, no custom cells or gesture recognizes needed.
If your minimum deploy target is >= iOS 14.x, this is probably the preferred method from now on, and it will also let you to adopt modern cell configuration with UIContentView and UIContentConfiguration to boot.
There is a more standard solution to implement this feature, having a behavior very similar to the one provided by UITableView.
For this, you will use a UIScrollView as the root view of the cell, and then position the cell content and the delete button inside the scroll view. The code in your cell class should be something like this:
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(scrollView)
scrollView.addSubview(viewWithCellContent)
scrollView.addSubview(deleteButton)
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
}
In this code we set the property isPagingEnabled to true to make the scroll view to stop scrolling only at the boundaries of its content. The layout subviews for this cell should be something like:
override func layoutSubviews() {
super.layoutSubviews()
scrollView.frame = bounds
// make the view with the content to fill the scroll view
viewWithCellContent.frame = scrollView.bounds
// position the delete button just at the right of the view with the content.
deleteButton.frame = CGRect(
x: label.frame.maxX,
y: 0,
width: 100,
height: scrollView.bounds.height
)
// update the size of the scrolleable content of the scroll view
scrollView.contentSize = CGSize(width: button.frame.maxX, height: scrollView.bounds.height)
}
With this code in place, if you run the app you will see that the swipe to delete is working as expected, however, we lost the ability to select the cell. The problem is that since the scroll view is filling the whole cell, all the touch events are processed by it, so the collection view will never have the opportunity to select the cell (this is similar to when we have a button inside a cell, since touches on that button don't trigger the selection process but are handled directly by the button.)
To fix this problem we just have to indicate the scroll view to ignore the touch events that are processed by it and not by one of its subviews. To achieve this just create a subclass of UIScrollView and override the following function:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let result = super.hitTest(point, with: event)
return result != self ? result : nil
}
Now in your cell you should use an instance of this new subclass instead of the standard UIScrollView.
If you run the app now you will see that we have the cell selection back, but this time the swipe isn't working 😳. Since we are ignoring touches that are handled directly by the scroll view, then its pan gesture recognizer won't be able to start recognizing touch events. However, this can be easily fixed by indicating to the scroll view that its pan gesture recognizer will be handled by the cell and not by the scroll. You do this adding the following line at the bottom of your cell's init(frame: CGRect):
addGestureRecognizer(scrollView.panGestureRecognizer)
This may look like a bit hacky, but it isn't. By design, the view that contains a gesture recognizer and the target of that recognizer don't have to be the same object.
After this change all should be working as expected. You can see a full implementation of this idea in this repo

Resources