J'ai un UICollectionView
dans mon UIViewController
et je veux qu'il réponde aux gestes à l'intérieur et à l'extérieur de UICollectionView
. Par défaut, UICollectionView
répond uniquement aux gestes dans sa propre view
mais comment puis-je le faire répondre aux balayages en dehors de sa view
?
Merci.
J'ai écrit une sous-class de vue qui accomplit juste ceci:
#import <UIKit/UIKit.h> @interface TouchForwardingView : UIView @property (nonatomic, weak) IBOutlet UIResponder *forwardingTarget; - (instancetype)initWithForwardingTarget:(UIResponder *)forwardingTarget; @end #import "TouchForwardingView.h" @implementation TouchForwardingView - (instancetype)initWithForwardingTarget:(UIResponder *)forwardingTarget { self = [super init]; if (self) { self.forwardingTarget = forwardingTarget; } return self; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self.forwardingTarget touchesBegan:touches withEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self.forwardingTarget touchesEnded:touches withEvent:event]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; [self.forwardingTarget touchesCancelled:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; [self.forwardingTarget touchesMoved:touches withEvent:event]; } @end
Dans le Générateur d'interface, définissez la sous-vue de la vue contenant sur TouchForwardingView, puis affectez la vue de collection à la propriété forwardingTarget.
Version rapide de la réponse de Nailer, cela transmettra tous les gestes effectués sur le viewcontroller à la collection
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { collectionView.touchesBegan(touches, withEvent: event) } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { collectionView.touchesEnded(touches, withEvent: event) } override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { collectionView.touchesCancelled(touches, withEvent: event) } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { collectionView.touchesMoved(touches, withEvent: event) }