I'm trying to implement a UIPanGestureRecognizer in my UITableViewController to use for a swipe to delete animation. Similar to the swipe to delete used in the Clear app, where if you swipe a UITableViewCell in left or right the cell moves and gets deleted.
I have tried implementing this in my UITableViewCell subclass but it never seems to receive the event.
This is the code I put in my UITableViewCell subclass to try this functionality. In my init method
UIGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
and then the methods to handle it:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:self.superview];
//might have to change view to tableView
//check for the horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
NSLog(#"Panning");
}
return NO;
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
//if the gesture has just started record the center location
NSLog(#"handlePan");
_originalCenter = self.center; //Declared as a CGPoint at the top of my TableViewCell
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
//translate the center (aka translate from the center of the cell)
CGPoint translation = [recognizer translationInView:self];
self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
// determine whether the item has been dragged far enough to delete/complete
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
// the frame this cell would have had before being dragged
CGRect originalFrame = CGRectMake(0, self.frame.origin.y, self.bounds.origin.x, self.bounds.size.height);
[UIView animateWithDuration:0.2 animations:^{
self.frame = originalFrame;}
];
}
}
The Cells don't move at all though. Not really sure what's going on here
If you don't want the cell's swipe gesture to happen simultaneously with the table view scroll gesture, then add a pan gesture to your cell and make it a delegate:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(doPan:)];
pan.delegate = self;
[self addGestureRecognizer:pan];
And implement the following delegate method to only start if the pan is horizontal:
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// note: we might be called from an internal UITableViewCell long press gesture
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer*)gestureRecognizer;
UIView *cell = [panGestureRecognizer view];
CGPoint translation = [panGestureRecognizer translationInView:[cell superview]];
// Check for horizontal gesture
if (fabs(translation.x) > fabs(translation.y))
{
return YES;
}
}
return NO;
}
Swift3 ..
override func awakeFromNib() {
super.awakeFromNib()
// do not use, say, layoutSubviews as layoutSubviews is called often
let p = UIPanGestureRecognizer(target: self, action: #selector(yourPan))
p.delegate = self
contentView.addGestureRecognizer(p)
}
}
override func gestureRecognizerShouldBegin(_ g: UIGestureRecognizer) -> Bool {
if (g.isKind(of: UIPanGestureRecognizer.self)) {
let t = (g as! UIPanGestureRecognizer).translation(in: contentView)
let verticalness = abs(t.y)
if (verticalness > 0) {
print("ignore vertical motion in the pan ...")
print("the event engine will >pass on the gesture< to the scroll view")
return false
}
}
return true
}
You need the following method in order for the gesture to be detected in sync with the scrollView's panGesture:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; //otherGestureRecognizer is your custom pan gesture
}
Remember to set the panGesture.delegate to your viewController. (Updated with OlivaresF's comment.)
Add the gesture recognizer to the content view.
[self.contentView addGestureRecognizer:recognizer];
Related
I have 2 custom-drawn subviews on a superview and 2 gesture recognisers to call 2 different methods. Problem is, when I tap on any of the views, tap on the second subview still fires the first method! Here's my code:
- (void) addTapGestures{
self.firstTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(firstSelector)];
self.firstTap.numberOfTapsRequired = 1;
[self.firstSubview addGestureRecognizer:self.firstTap];
self.secondTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(secondSelector)];
self.secondTap.numberOfTapsRequired = 1;
[self.secondSubview addGestureRecognizer:self.secondTap];
Will appreciate any hints!
You can get the tapped view by using hitTest method of UIView. Based on that you can decide which method should be called. Put this method in your superview containing both of your subviews.
Here is an example.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if ([self.firstSubView isEqual:hitView])
{
// You clicked on firstSubView, by returning firstSubView it will call its respective selector method
return self.firstSubView;
}
else if ([self.secondSubView isEqual:hitView])
{
return self.secondSubView;
}
return hitView;
}
Answer from my comments. Common problems I can think of:
1) self.firstSubview is equal to self.secondSubview
2) Views are overlapping and/or user interaction is disabled on one of them.
Second one turned out to be a problem.
Swift 2.0:
Try: -
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
view.endEditing(true)
super.touchesBegan(touches, withEvent: event)
//make condition here
}
OR
Use a UIPanGestureRecognizer. In the ViewController, install the recognizer on the “parent” view.
var pan = UIPanGestureRecognizer(target:self, action:"pan:")
pan.maximumNumberOfTouches = 1
pan.minimumNumberOfTouches = 1
self.view.addGestureRecognizer(pan)
In the recognizer action, first grab the location of the event. Then, depending on the state of the recogniser, implement the different parts of the drag functionality.
func pan(rec:UIPanGestureRecognizer) {
var p:CGPoint = rec.locationInView(self.view)
var center:CGPoint = CGPointZero
switch rec.state {
case .Began:
println("began")
self.selectedView = view.hitTest(p, withEvent: nil)
if self.selectedView != nil {
self.view.bringSubviewToFront(self.selectedView!)
}
case .Changed:
if let subview = selectedView {
center = subview.center
var distance = sqrt(pow((center.x - p.x), 2.0) + pow((center.y - p.y), 2.0))
println("distance \(distance)")
if subview is MyView {
if distance > threshold {
if shouldDragX {
subview.center.x = p.x - (p.x % snapX)
}
if shouldDragY {
subview.center.y = p.y - (p.y % snapY)
}
}
}
}
case .Ended:
if let subview = selectedView {
if subview is MyView {
// do whatever
}
}
// must do this of course
selectedView = nil
}
I added an UISwipeGestureRecogniser into my tableviewcell. Now I'd like to add a real time slide effect to my tableviewcell. If I currently swipe over the cell nothing happens. The cell should just be swipeable.
My code:
var originalCenter = CGPoint()
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
rightSwipe.direction = .Right
self.addGestureRecognizer(rightSwipe)
func handleSwipes(recogniser:UISwipeGestureRecognizer) {
let location : CGPoint = recogniser.locationInView(self)
if (recogniser.direction == .Right) {
if recogniser.state == .Began {
// when the gesture begins, record the current center location
originalCenter = center
}
if recogniser.state == .Changed {
let translation = recogniser.locationInView(self)
center = CGPointMake(originalCenter.x + translation.x, originalCenter.y)
}
if recogniser.state == .Ended {
}
println("Swipe Right")
}
}
I have custom cell swipes implemented in my previous projects... you need to animate the things to look exactly like the swipe which works in notification draw message swiping. I can give you snippet in Objective-C. you should be able to translate it into swift easily ;)
-(void)handleLeftCellSwipe:(UISwipeGestureRecognizer *)recognizer{
CGPoint location = [recognizer locationInView:self.localTableView];
NSIndexPath *swipedIndexPath = [self.localTableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.localTableView cellForRowAtIndexPath:swipedIndexPath];
if(swipedCell){
[UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[swipedCell.contentView setFrame:CGRectMake(swipedCell.contentView.frame.origin.x - 40, swipedCell.contentView.frame.origin.y, swipedCell.contentView.frame.size.width, swipedCell.contentView.frame.size.height)];
} completion:^(BOOL finished){
// Do whatever you need to do after animation completes
}];
}
}
You can easily do what you want to do by subclassing UIPanGestureRecognizer. But don't. Instead of adding a gesture recognizer, turn your cell into a UIScrollView. Now you can set its contentSize and bounds origin so that it can be scrolled left to right only. The advantage here is that you are using a built-in interface class that is made to be draggable automatically. You can use the delegate methods to keep track of what the user does if you need to.
I have a UITextField that has User Interaction Disabled. So if you tap on this text field, nothing happens. Normally to check if a text field was tapped Id try the delegate methods, but I cannot because user interaction is disabled. Is there any way I can check if the text field was tapped/touched? I change another element to hidden = no; when it is tapped so I was wondering if its even possible enabling user interaction.
Best option is to turn on User Interaction and disable edit action using delegate method.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
You can call your method inside that function to detect tap.
Maybe, you can add UITapGestureRecognizer in the superview, detect if the touch is inside the frame, and then do something.
Detect touch if it is inside the frame of the super view
Create UITapGestureRecognizer and add that to the UITextField's super view.
Implement the target selector and check if the gesture's state has ended.
Call your method.
Objective-C
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didRecognizeTapGesture:)];
[self.textField.superview addGestureRecognizer:tapGesture];
- (void) didRecognizeTapGesture:(UITapGestureRecognizer*) gesture {
CGPoint point = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateEnded) {
if (CGRectContainsPoint(self.textField.frame, point)) {
[self doSomething];
}
}
}
Swift 3
func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))
textField.superView?.addGestureRecognizer(tapGesture)
}
private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
let point = gesture.location(in: gesture.view)
guard gesture.state == .ended, textField.frame.contains(point) else { return }
//doSomething()
}
I add UILongPressGestureRecognizer to several UIButton with the code :
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(btnLong:)];
[btnOne addGestureRecognizer:longPress]; //there are btnTwo, btnThree for example
And when I long press on a button the method is called:
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
}
}
My question is, how can I know which UILongPressGestureRecognizer is triggered, because there is no tag property for UILongPressGestureRecognizer.
Give each button a unique tag number. Then in your action method you can do:
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
UIView *view = gestureRecognizer.view;
if (view.tag == 1) { // first button's tag
// process 1st button
} else if (view.tag == 2) { // second button's tag
// process 2nd button
}
}
}
Another option, if you have outlets for each button you can do:
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
UIView *view = gestureRecognizer.view;
if (view == self.firstButton) {
// process 1st button
} else if (view == self.secondButton) {
// process 2nd button
}
}
}
where firstButton and secondButton are your button properties. And yes, using == is appropriate for checking to see if the gesture's view is one of the buttons because you do mean to compare the object pointers.
Why not put the gesture rec on the common superview? Then you can determine which UIView was long-pressed by using locationInView, then accessing the view's tag property.
I have used UIView as subview to tableview cell. And I applied UILongGesture to that. this is the code worked for me.
func handleLongPressGesture(_ longPressGestureRecognizer: UILongPressGestureRecognizer){
if longPressGestureRecognizer.state == UIGestureRecognizerState.began
{
let touchPoint = longPressGestureRecognizer.location(in: tableViewObj)
if let indexPath = tableViewObj.indexPathForRow(at: touchPoint)
{
print(indexPath.row)
}
}
}
You have index path. You can do whatever you need to do.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];
[self.view2 addGestureRecognizer:tapGesture];
[tapGesture release];
In the above code only taps on view2 are recognized. If I comment out the third line then taps on view1 are recognized. If I'm right and you can only use a gesture recognizer once, I'm not sure if this is a bug or it just needs some more documentation.
A UIGestureRecognizer is to be used with a single view. I agree the documentation is spotty. That UIGestureRecognizer has a single view property gives it away:
view
The view the gesture recognizer is attached to. (read-only)
#property(nonatomic, readonly) UIView *view
Discussion You attach (or add) a gesture recognizer to a UIView object
using the addGestureRecognizer:
method.
I got around it by using the below.
for (UIButton *aButton in myButtons) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration=1.0;
[aButton addGestureRecognizer:longPress];
[longPress release];
}
Then in my handleLongPress method I just set a UIButton equal to the view of the gesture recognizer and branch what I do based upon that button
- (void)handleLongPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
UIButton *whichButton=(UIButton *)[gesture view];
selectedButton=(UIButton *)[gesture view];
....
}
For Swift 3 in case anyone requires this:
Based on Bhavik Rathod Answer above.
func setGestureRecognizer() -> UIPanGestureRecognizer {
var panRecognizer = UIPanGestureRecognizer()
panRecognizer = UIPanGestureRecognizer (target: self, action: #selector(pan(panGesture:)))
panRecognizer.minimumNumberOfTouches = 1
panRecognizer.maximumNumberOfTouches = 1
return panRecognizer
}
///set the recognize in multiple views
view1.addGestureRecognizer(setGestureRecognizer())
view2.addGestureRecognizer(setGestureRecognizer())
No you should not attach gesture recognizers to more than one view.
There is this explicit information in the Apple documentation:
Gesture Recognizers Are Attached to a View
Every gesture recognizer is associated with one view. By contrast, a
view can have multiple gesture recognizers, because a single view
might respond to many different gestures. For a gesture recognizer to
recognize touches that occur in a particular view, you must attach the
gesture recognizer to that view.
Event Handling Guide for iOS - Gesture Recognizers Apple Developer Library
While as others mention they might work in some cases it is clearly against the documentation and could change in any future iOS version.
What you can do is add separate gesture recognisers to the views you want to monitor and they can share a common action.
We can do something Like this, it's easy and simple
1) create function as below in your controller (this function will return GestureRecognizer)
-(UITapGestureRecognizer*)setRecognizer{
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openProfile)];
[gestureRecognizer setNumberOfTapsRequired:1];
return gestureRecognizer;
}
2) now set this recognizer in multiple views
[self.view1 addGestureRecognizer:[self setRecognizer]];
[self.view2 addGestureRecognizer:[self setRecognizer]];
Well if someone does not want to code for adding gesture view for multiple buttons like kwalker has answered above, and want to do it via Interface Builder this may help you.
1) You can add Long Press gesture Recognizer from Object Library like you add other objects like UIButtons and UILabels.
Initially what I ended up using was I took only one
2) Set referencing outlets to UIButton and sent actions with File's Owner.
Note: If you have multiple UIButton or any other object you will need separate gesture recognizer for each of them. For more details please refer to this question of mine.Getting wrong UIButton tag on Long press gesture recognizer
if you have fixed view I suggest you doing something like this
[self.view1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTapTap:)]];
[self.view2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTapTap:)]];
that way will reduce multiple different useless variable
You could create a generic extension on view to add gesture recognizers easily.
This is just an example but it could look like this
extension UIView {
func setGestureRecognizer<Gesture: UIGestureRecognizer>(of type: Gesture.Type, target: Any, actionSelector: Selector, swipeDirection: UISwipeGestureRecognizer.Direction? = nil, numOfTaps: Int = 1) {
let getRecognizer = type.init(target: target, action: actionSelector)
switch getRecognizer {
case let swipeGesture as UISwipeGestureRecognizer:
guard let direction = swipeDirection else { return }
swipeGesture.direction = direction
self.addGestureRecognizer(swipeGesture)
case let tapGesture as UITapGestureRecognizer:
tapGesture.numberOfTapsRequired = numOfTaps
self.addGestureRecognizer(tapGesture)
default:
self.addGestureRecognizer(getRecognizer)
}
}
}
To add a 2 tap recognizer on a view you would just call:
let actionSelector = #selector(actionToExecute)
view.setGestureRecognizer(of: UITapGestureRecognizer.self, target: self, actionSelector: actionSelector, numOfTaps: 2)
You could also easily add a swipe recognizer
view.setGestureRecognizer(of: UISwipeGestureRecognizer.self, target: self, actionSelector: actionSelector, swipeDirection: .down)
and so on.
Just remember that the target must be linked to the selector.
Override class by '<UIScrollViewDelegate>'
And use this method in .m class:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
This method will help you to enable multiple swipe on a single view..
What about re write (recreate) your GestureRecognize every time that you add a gesture recognizer pointing to the same func.
In below case it works. I am using IBOutletCollection
Swift 2:
#IBOutlet var topicView: [UIView]!
override func viewDidLoad() {
for view in self.topicView as [UIView] {
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "viewClicked:"))
}
}
func viewClicked(recognizer: UITapGestureRecognizer) {
print("tap")
}
I know this is an old post but I figured something similar and hopefully it's useful someone else. I simply stored my imageViews in an array and assigned it to to the same gesture recognizer in a function to set up each image view.
In my viewDidLoad():
imageViewList = [imageView, imageView2, imageView3]
setupImageViews(imageViews: imageViewList)
Function to setup image views:
func setupImageViews(imageViews: [UIImageView]) {
for imageView in imageViews {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
//set up image to be displayed with the right aspect
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
imageView.clipsToBounds = true
}
}
And in the action selector imageTapped(), you can have corresponding code for whichever image view tapped.
#objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
switch tapGestureRecognizer.view {
case imageView:
print("tapped Image View 1") //add your actions here
case imageView2:
print("tapped Image View 2") //add your actions here
case imageView3:
print("tapped Image View 3") //add your actions here
default:
print("Tap not detected")
}
_ = tapGestureRecognizer.view as! UIImageView
//additional code...
}
You can do it using this code my views which are imageviews in the xib.
- (void)viewDidLoad
{
firstIV.tag = 501;
secondIV.tag = 502;
thirdIV.tag = 503;
forthIV.tag = 504;
[self addTapGesturetoImageView: firstIV];
[self addTapGesturetoImageView: secondIV];
[self addTapGesturetoImageView: thirdIV];
[self addTapGesturetoImageView: forthIV];
}
-(void)addTapGesturetoImageView:(UIImageView*)iv
{
iv.userInteractionEnabled = YES;
UITapGestureRecognizer * textfielBGIVTapGasture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(textfielBGIVTapped:)];
textfielBGIVTapGasture.numberOfTapsRequired = 1;
[iv addGestureRecognizer:textfielBGIVTapGasture];
}
- (void)textfielBGIVTapped:(UITapGestureRecognizer *)recognizer {
int tag = recognizer.view.tag-500;
switch (tag) {
case 1:
{
//firstIV tapped;
break;
}
case 2:
{
//secondIV tapped;
break;
}
case 3:
{
//thirdIV tapped;
break;
}
case 4:
{
//forthIV tapped;
break;
}
default: {
break;
}
}
}