I am new to ios, so I apologize in advance if I am missing something obvious.
I am creating a puzzle where I would like the individual puzzle pieces to increase in size on touch and decrease on letting go.
Currently I have:
-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
if(recognizer.state == UIGestureRecognizerStateBegan)
else if(recognizer.state == UIGestureRecognizerStateEnded)
}
The puzzle piece increases size when the pan begins (which is also when the statebegan) and decreases in size when the pan ends (as expected). I would like the size to increase once the user has touched the piece and before the puzzle piece moves. This is seen in Words With Friends when selecting a tile.
I have tried
-(IBAction)handleTap:(UITapGestureRecognizer *)recognizer{
if(recognizer.state == UIGestureRecognizerStateBegan)
else if(recognizer.state == UIGestureRecognizerStateEnded)
}
This will increase the puzzle piece only after the finger has lifted.
MY QUESTION:
Is there a way to increase the size of a puzzle piece once the finger has touched the puzzle piece and then continue with the pan gesture.
Thank you in advance.
I needed to do this too, and Jake's suggestion worked perfectly for me. In case it helps anyone who comes across this in the future, here is my subclass implementation of UIPanGestureRecognizer
(the header remains unchanged):
#import "ImmediatePanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation ImmediatePanGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
self.state = UIGestureRecognizerStateBegan;
}
@end
This is all you need—this will fire as soon as you put your finger down on the view, update as soon as you move your finger a single point in any direction, and provide the functionality of a regular UIPanGestureRecognizer
(like translationInView
and velocityInView
) before a regular one would've fired, all without breaking any existing functionality.
Copypaste for Swift 5, 2020
import UIKit // (nothing extra needed to import with Swift5)
fileprivate class ImmediatePanG: UIPanGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
self.state = .began
}
}
Swift 3/4/5 Solution
class InstantPanGestureRecognizer: UIPanGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if self.state == .began { return }
super.touchesBegan(touches, with: event)
self.state = .began
}
}
I have implemented George WS's answer. With a little testing I realized that additional touch events that occur after the initial touch, but before the initial touch ends are not being properly handled. Here is my updated implementation. It's a bit naive, but prevents bizarre behavior caused by the UIGestureRecognizerStateBegan happening multiple times.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.state >= UIGestureRecognizerStateBegan) {
return;
}
[super touchesBegan:touches withEvent:event];
self.state = UIGestureRecognizerStateBegan;
}
According to the documentation UIPanGestureRecognizer
only enters UIGestureRecognizerStateBegan when "the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan". If you want something to happen as soon as you touch, you could try subclassing UIPanGestureRecognizer and over riding touchesBegan:withEvent:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With