I have an ViewController class, where i have an UIView called templateView
on self.view, which consists of an UIView called gridView
, here i need to swipe on templateView
, for that i have added swipegesture like,
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction)];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self;
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;
[templateView addGestureRecognizer:swipeRight];
[templateView addGestureRecognizer:swipeLeft];
In the swipeRight
and swipeLeft
i need to move the gridView
left side and right side.. What i need to implement in these methods..?
I'd suggest
Use a gesture handler that takes a parameter (in case you ever add gestures to multiple views);
Make sure the view in question has userInteractionEnabled
turned on.
You don't need to set the gesture's delegate
unless you're implementing one of the UIGestureRecognizerDelegate
methods.
Thus, the configuration might look like:
templateView.userInteractionEnabled = YES;
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[templateView addGestureRecognizer:swipeRight];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[templateView addGestureRecognizer:swipeLeft];
And then the gesture handler might look like:
- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
CGRect frame = self.gridView.frame;
// I don't know how far you want to move the grid view.
// This moves it off screen.
// Adjust this to move it the appropriate amount for your desired UI
if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
frame.origin.x += self.view.bounds.size.width;
else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
frame.origin.x -= self.view.bounds.size.width;
else
NSLog(@"Unrecognized swipe direction");
// Now animate the changing of the frame
[UIView animateWithDuration:0.5
animations:^{
self.gridView.frame = frame;
}];
}
Note, if you're using auto layout and if the view is defined by constraints rather than translatesAutoresizingMaskIntoConstraints
, then this handler code would have to change appropriately. But hopefully this gives you the basic idea.
You can move the gridView by using some UIViewAnimations.
Create something like that:
-(void)swipeRightAction{
[UIView setAnimationDuration:1];
gridView.frame = CGRectMake(320,0);
[UIView commitAnimations];
}
This code will change the frame of your gridView. You need to change this parameter according to where you want to slide your view.
I didn't try the code, let me know if it works.