I'm trying to make an UIViewImage
, and an UILabel
draggable, and i having some problems to figure out how to make the Views, only draggable at one direction, for example, i want to drag the views to left(x) or down(y), but never left and down at same time.
I tried to use the UISwipeGestureRecognizer
, to see in which direction i should move UP, DOWN or LEFT and RIGHT, but things are not working as i tough.
To make things easier this is what a have on viewController.h
:
///Images
@property (nonatomic) UIImageView *mainImage; // Draggable Image
@property (nonatomic) UISwipeGestureRecognizer *swipeDirection; // This will be used on TouchesMoved.
//Label
@property(nonatomic) UILabel *mainLabel; // Draggable Label
I'm adding the Swipe recognizer in the viewDidLoad
Method, just for the Left (for now):
UISwipeGestureRecognizer *detectSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(setSwipe:)];
[detectSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:detectSwipe];
Here is where i set the "swipeDirection" property:
-(void)setSwipe:(UISwipeGestureRecognizer *)gesture {
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) { // this is just for testing
self.swipeDirection = gesture;
NSLog(@"Gesture = %@", gesture);
}
}
Here is where I'm actually trying to move things (currently only to left):
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches ) {
CGPoint currentLocalation = [touch locationInView:touch.view];
if (self.swipeDirection.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Swipe Left");
if (currentLocalation.x < startLocation.x) { // Startlocation is a global CGPoint.
NSLog(@"Current Y = %f, starting Y = %f", currentLocalation.y, startLocation.y);
CGFloat distance = startLocation.x - currentLocalation.x;
CGPoint newPosition = CGPointMake(160 - distance, 170);
CGPoint labelPosition = CGPointMake(160 - distance, 390);
self.mainImage.center = newPosition;
self.mainLabel.center = labelPosition;
}
} else {
NSLog(@"Other direction");
}
}
What should i do? I'm looking at this from the wrong direction? Any help will be appreciated.
It looks like you're trying to use a swipe gesture for something it's not fit for (i.e. tracking finger movement and moving views along with the finger). This is exactly what UIPanGestureRecognizer
is for. Here's an example of how this would work with a pan gesture:
First, attach the gesture recognizer:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
[self.view addGestureRecognizer:panGesture];
Then, handle finger movement:
-(void)onPan:(UIPanGestureRecognizer*)recognizer
{
// Find out how far the finger has moved (+x is right, -x is left, +y is down, -y is up)
CGPoint movement = [recognizer translationInView:recognizer.view];
if(movement.x < 0){
// Finger moved left by movement.x pixels
}
if(movement.y > 0){
// Finger moved down by movement.y pixels
}
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
In this example, I'm accepting both left and downwards movement at the same time. I'll leave it up to you to figure out how to decide which direction gets precedence if the user drags down and left simultaneously. One suggestion would be to compare the absolute value of the x and y directions, and act on whichever is larger