Reputation: 255
I have the following problem.
I am using a UILongPressGestureRecognizer
to put a UIView into a "toggle mode". If the UIView
is in "toggle mode" the user is able to drag the UIView around the screen. For dragging the UIView around the screen I am using the methods touchesBegan
, touchesMoved
and touchesEnded
.
It works, but: I have to lift my finger in order to drag it, because the touchesBegan
method got already called and therefore is not called again and therefore I can't drag the UIView
around the screen.
Is there any way to manually call touchesBegan
after UILongPressGestureRecognizer
got triggered (UILongPressGestureRecognizer
changes a BOOL value and the touchesBegan
only works if this BOOL is set to YES).
Upvotes: 3
Views: 3889
Reputation: 3850
I would suggest you to use UIPanGestureRecognizer as it a recommended gesture for dragging.
You can configure the min. and max. number of touches required for a panning, using the following the properties:
maximumNumberOfTouches
minimumNumberOfTouches
You can handle the states like Began, Changed and Ended, like having animation for the required states.
Using the below method translate the point to the UIView in which you want it.
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
example:
You have to use a global variable to retain the old frame. Get this in UIGestureRecognizerStateBegan.
When the state is UIGestureRecognizerStateChanged. You can use the
-(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{ if(panGesture.state==UIGestureRecognizerStateBegan){ //retain the original state }else if(panGesture.state==UIGestureRecognizerStateChanged){ CGPoint translatedPoint=[panGesture translationInView:self.view]; //here you manage to get your new drag points. } }
Velocity of the drag. Based on the velocity you can provide a animation to show bouncing of a UIView
- (CGPoint)velocityInView:(UIView *)view
Upvotes: 0
Reputation: 437502
UILongPressGestureRecognizer
is a continuous gesture recognizer, so rather than resorting to touchesMoved
or UIPanGestureRecognizer
, just check for UIGestureRecognizerStateChanged
, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gesture];
}
- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
// user held down their finger on the screen
// gesture started, entering the "toggle mode"
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
// user did not lift finger, but now proceeded to move finger
// do here whatever you wanted to do in the touchesMoved
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
// user lifted their finger
// all done, leaving the "toggle mode"
}
}
Upvotes: 10