Adding gesture recognizer control to JTRevealSideBarDemoV2 - ios

I am using JTRevealSideBarDemoV2 for the implementation of the side panels like facebook and Path application. I have configured it in every sense and just want to add one more feature into this library. What I want is to apply gesture recognizer control in this library just similar to facebook. can somebody help?
The link for downloading this library is:-https://www.cocoacontrols.com/controls/jtrevealsidebar
Thanks!
#wattson:- this is the code which I have added for implementing Gesture Recognizer. BBut it is behaving very abnormally. If you have downloaded the code for JTRevealSideBarDemoV2 then you can analyze the code which i have written for.
The code is:
(void)setUpGestures{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(movePanel:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.view addGestureRecognizer:panRecognizer];
}
-(void)movePanel:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender
translationInView:self.view];
CGPoint velocity = [(UIPanGestureRecognizer*)sender velocityInView:[sender view]];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
//UIView *childView = nil;
if(velocity.x > 0) {
if (!JTRevealedStateRight) {
[self revealLeftSidebar:(UIPanGestureRecognizer *)sender];
}
} else {
if (!JTRevealedStateLeft) {
[self revealRightSidebar:(UIPanGestureRecognizer *)sender];
}
}
// Make sure the view you're working with is front and center.
//[self.view sendSubviewToBack:];
[[sender view] bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
}
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
if(velocity.x > 0) {
// NSLog(#"gesture went right");
} else {
// NSLog(#"gesture went left");
}
if (!_showPanel) {
[self revealLeftSidebar:(UIPanGestureRecognizer *)sender];
} else {
if (JTRevealedStateLeft) {
[self revealRightSidebar:(UIPanGestureRecognizer *)sender];
} else if (JTRevealedStateRight) {
[self revealLeftSidebar:(UIPanGestureRecognizer *)sender];
}
}
}
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {
if(velocity.x > 0) {
// NSLog(#"gesture went right");
} else {
// NSLog(#"gesture went left");
}
// Are you more than halfway? If so, show the panel when done dragging by setting this
value to YES (1).
_showPanel = abs([sender view].center.x - self.view.frame.size.width/2) >
self.view.frame.size.width/2;
// Allow dragging only in x-coordinates by only updating the x-coordinate with
translation position.
[sender view].center = CGPointMake([sender view].center.x + translatedPoint.x, [sender
view].center.y);
[(UIPanGestureRecognizer*)sender setTranslation:CGPointMake(0,0) inView:self.view];
// If you needed to check for a change in direction, you could use this code to do so.
if(velocity.x*_preVelocity.x + velocity.y*_preVelocity.y > 0) {
// NSLog(#"same direction");
} else {
// NSLog(#"opposite direction");
}
_preVelocity = velocity;
}
}

Related

UIRotationGestureRecognizer Not working (Pinch / Rotate ) Obj-C [duplicate]

This question already has an answer here:
Handling Multiple GestureRecognizers
(1 answer)
Closed last year.
I can select the image, however the rotate/pinch functions are not working, this is the code;
in my viewDidLoad I init like;
[self initSingleTouchGesturesOnView: self.gestureView];
[self initMultiTouchGesturesOnView: self.gestureView];
Those methods are;
-(void) initSingleTouchGesturesOnView: (UIView *) targetView {
//Pan
bool found = FALSE;
for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
if ([recognizer isKindOfClass: [UIPanGestureRecognizer class]])
found = TRUE;
if (!found) {
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget: self action: #selector(pan:)] autorelease];
[panRecognizer setDelegate: self];
[targetView addGestureRecognizer: panRecognizer];
}
}
-(void) initMultiTouchGesturesOnView: (UIView *) targetView {
//Pinch
bool found = FALSE;
for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
if ([recognizer isKindOfClass: [UIPinchGestureRecognizer class]])
found = TRUE;
if (!found) {
UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)] autorelease];
[pinchRecognizer setDelegate:self];
[targetView addGestureRecognizer:pinchRecognizer];
}
//Rotate
found = FALSE;
for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
if ([recognizer isKindOfClass: [UIRotationGestureRecognizer class]])
found = TRUE;
if (!found) {
UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)] autorelease];
[rotationRecognizer setDelegate:self];
[targetView addGestureRecognizer:rotationRecognizer];
}
}
For the gestures themselves;
#pragma mark - GestureDelegate
-(void)pan:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView: self.gestureView];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
self.currentFaceView.dragFirstX = [self.currentFaceView center].x;
self.currentFaceView.dragFirstY = [self.currentFaceView center].y;
}
translatedPoint = CGPointMake(self.currentFaceView.dragFirstX+translatedPoint.x, self.currentFaceView.dragFirstY+translatedPoint.y);
[self.currentFaceView setCenter:translatedPoint];
}
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
self.currentFaceView.dragLastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (self.currentFaceView.dragLastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = self.currentFaceView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[self.currentFaceView setTransform:newTransform];
self.currentFaceView.dragLastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender {
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
self.currentFaceView.dragLastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (self.currentFaceView.dragLastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = self.currentFaceView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[self.currentFaceView setTransform:newTransform];
self.currentFaceView.dragLastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
I also use;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
But they do not seem to respond, no matter what, it's almost like when I am trying a gesture such as pinch, the screen 'taps' away, like I have lifted my fingers off without having done so.
So the solution in my case was actually changing this part of the code;
It now works perfectly.
-(void) initSingleTouchGesturesOnView: (UIView *) targetView {
//Pinch
bool found = FALSE;
for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
if ([recognizer isKindOfClass: [UIPinchGestureRecognizer class]])
found = TRUE;
if (!found) {
UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)] autorelease];
[pinchRecognizer setDelegate:self];
[targetView addGestureRecognizer:pinchRecognizer];
}
//Rotate
found = FALSE;
for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
if ([recognizer isKindOfClass: [UIRotationGestureRecognizer class]])
found = TRUE;
if (!found) {
UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)] autorelease];
[rotationRecognizer setDelegate:self];
[targetView addGestureRecognizer:rotationRecognizer];
}
found = FALSE;
for (UIGestureRecognizer *recognizer in targetView.gestureRecognizers)
if ([recognizer isKindOfClass: [UIPanGestureRecognizer class]])
found = TRUE;
if (!found) {
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget: self action: #selector(pan:)] autorelease];
[panRecognizer setDelegate: self];
[targetView addGestureRecognizer: panRecognizer];
}
}

How to slide a button from the bottom after sliding from the edge?

I have implemented the following two methods
-(void)addGestureToWebView {
[self setBottomEdgeGesture:[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(handleBottomEdgeGesture:)]];
[[self bottomEdgeGesture] setEdges:UIRectEdgeBottom];
[[self bottomEdgeGesture] setDelegate:self];
[[self webView] addGestureRecognizer:[self bottomEdgeGesture]];
}
and
-(IBAction)handleBottomEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == [gesture state] || UIGestureRecognizerStateChanged == [gesture state]) {
//Do something
}
}
The method handleBottomEdgeGesture should slide a button with three dots from the bottom.
I tried
UIButton *btnDots = [[UIButton alloc] initWithFrame:CGRectMake(280.0, 528.0, 20, 20)];
[btnDots setTitle:#"..." forState:UIControlStateNormal];
[btnDots setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
but nothing happened. I didn't see any dots at my display. I am new to Objective-C and some things not so easy to develop.
Can anyone give me a good advice?
Since you created a button programmatically you also need to add it to sub view. Then on gesture recognizer you should change the frame of the button. A reference to the button will be needed.
Lets say you are doing this in the view controller. Create a property for dot button. In view did load create the button and assign it to the property:
UIButton *dotButton = [[UIButton alloc] initWithFrame:CGRectMake(.0f, self.view.frame.size.height, self.view.frame.size.width, 20.0f)];
// do additional button setup
[self.view addSubview:dotButton];
[dotButton addTarget:self action:#selector(onDotButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.dotButton = dotButton;
Then in gesture recognizer you have 2 options. Either just handle the event and show the button animated or move its frame as your finger moves:
- (void)onGesture:(UIGestureRecognizer *)sender
{
#ifdef USE_ONLY_ON_EVENT
if(sender.state == UIGestureRecognizerStateBegan)
{
[UIView animateWithDuration:.23 animations:^{
self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height-self.dotButton.frame.size.height, self.view.frame.size.width, self.dotButton.frame.size.height);
}];
}
#else
if(sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged)
{
CGPoint location = [sender locationInView:self.view];
CGFloat yOffset = self.view.frame.size.height - location.y;
if(yOffset > self.dotButton.frame.size.height)
{
yOffset = self.dotButton.frame.size.height;
}
self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height-yOffset, self.view.frame.size.width, self.dotButton.frame.size.height);
}
else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled)
{
CGPoint location = [sender locationInView:self.view];
CGFloat yOffset = self.view.frame.size.height - location.y;
if(yOffset > self.dotButton.frame.size.height*.5f) // more then half of a button visible
{
// show full button
[UIView animateWithDuration:.23 animations:^{
self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height-self.dotButton.frame.size.height, self.view.frame.size.width, self.dotButton.frame.size.height);
}];
}
else
{
// hide button
[UIView animateWithDuration:.23 animations:^{
self.dotButton.frame = CGRectMake(.0f, self.view.frame.size.height, self.view.frame.size.width, self.dotButton.frame.size.height);
}];
}
}
#endif
}
I hope you get the basic idea from the code.

remove gesture for UIButton

I'm using UIGestureRecognizer for pan, rotate, pinch. But i'm applying to whole view. I need to remove gesture for button other than subviews. But when i use pan the button also affecting. How to restrict button move from self.view. I used below code for UIPanGestureRecognizer.
UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(ondbPan:)];
[self.view addGestureRecognizer:dbpan];
[closeButton removeGestureRecognizer:dbpan];
Pan:
- (void)ondbPan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint offset = [gesture translationInView:self.view];
CGPoint center = gesture.view.center;
center.x += offset.x;
center.y += offset.y;
gesture.view.center = center;
[gesture setTranslation:CGPointZero inView:self.view];
}
}
Try with bellow code that delegate of UIGestureRecognizer return FALSE if that subview is kind of class UIButton Class. also set delegate dbpan.delegate = self; while you setting and add UIPanGestureRecognizer.
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return FALSE;
}
else
{
return TRUE;
}
}
UPDATE:-
I dont Know why that not working at your end i test this creating one demo as well using this code:-
.h file
#interface myviewcontroller : UIViewController<UIGestureRecognizerDelegate>
and .m class
- (void)pan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint location = [gesture locationInView:self.view];
[demoView setCenter:location];
}
}
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return FALSE;
}
else
{
return TRUE;
}
}
- (void)viewDidLoad
{
UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(pan:)];
dbpan.delegate=self;
[self.view addGestureRecognizer:dbpan];
[super viewDidLoad];
}
-(IBAction)B1called
{
NSLog(#"This is called button 1");
}
-(IBAction)B2called
{
NSLog(#"This is called button 2");
}
And it's Output is
On Swift:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if (touch.view!.isKindOfClass(UIButton)) {
return false
}
return true
}
Note: Make sure UIGestureRecognizerDelegate is added and tapGesture.delegate = self on viewDidLoad().
Try this...
while (closeButton.gestureRecognizers.count) {
[closeButton removeGestureRecognizer:[closeButton.gestureRecognizers objectAtIndex:0]];
}

Move UITableView with UISwipeGestureRecognizer

I am trying to mix an UISwipeGestureRecognizer with an UITableView.
What I would like to do is, while I am doing the swipe gesture, move at the same time the UITableView outside the window and refresh the table data.
I am going to show you with images...
This is my view:
My View
And I would like to get something like this:
Desired View
I am able to move the table when the swipe gesture is ended, but not while I am doing the gesture, and that is what I want.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
swipeToRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToGroup:)];
[swipeToRight setDelegate:self];
[swipeToRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self table]addGestureRecognizer:swipeToRight];
swipeToLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(changeToContact:)];
[swipeToLeft setDelegate:self];
[swipeToLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self table]addGestureRecognizer:swipeToLeft];
}
- (void)changeToGroup:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
- (void)changeToContact:(UISwipeGestureRecognizer *)swipeGesture
{
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
I thought that I could do it with UIGestureRecognizerStateBegan, adding the animations inside that event, but I can't receive it...
Could anyone help me?
Thanks a lot!!.
Try this,
use UIPanGestureRecognizer
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan:)];
[recognizer setMaximumNumberOfTouches:1];
[recognizer setDelegate:self];
[[self table]addGestureRecognizer:recognizer];
and in
- (void) handlePan:(UIPanGestureRecognizer *)recognizer {
UIView *view = self.tableView;
if(recognizer.state == UIGestureRecognizerStateBegan) {
// remember where the pan started
//panGestureOrigin is a CGPoint variable
panGestureOrigin = view.frame.origin;
//optional to keep an enum to keep direction
//self.panDirection = MenuPanDirectionNone;
}
CGPoint translatedPoint = [recognizer translationInView:view];
if(translatedPoint.x > 20) {
//self.panDirection = MenuPanDirectionRight;
[self handleRightPan:recognizer];
} else if(translatedPoint.x < 0) {
//self.panDirection = MenuPanDirectionLeft;
[self handleLeftPan:recognizer];
}
}
- (void) handleRightPan:(UIPanGestureRecognizer *)recognizer {
//animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Right to group");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:0];
}
}
- (void) handleLeftPan:(UIPanGestureRecognizer *)recognizer {
// animate here
if(recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"Left to contact");
[self updateTableData]; //Here I move the table and update data.
[segmentedControl setSelectedSegmentIndex:1];
}
}

UISwipeGestureRecognizer diagonal swipe issue

Well, I have some code to add 4 recognizers to a view, like this
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) {
UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
sgr.direction = d;
[self.view addGestureRecognizer:sgr];
}
[self restore];
[self populate];
[self displaymap];
}
and a recognizer like this
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
printf("Guesture: %d\n", recognizer.direction);
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
printf("a\n");
[self move: 'a'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
printf("d\n");
[self move: 'd'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
printf("w\n");
[self move: 'w'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)
{
printf("s\n");
[self move: 's'];
}
else if (recognizer.direction == (UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp))
{
printf("y\n");
[self move: 'd'];
}
}
The problem is, it never detects the up | right direction, anybody know a way to fix this?
That’s not how the direction property works. UISwipeGestureRecognizer only recognizes swipes in a single direction at a time. You’ll need to do something more complicated involving a UIPanGestureRecognizer and determining its direction from the result of its -translationInView: / -velocityInView: methods.

Resources