Swipe a mapkit away from view - ios-4.2

By default, when swipe a mapkit view, the map moves. This is great.
If I want to move away from the map view and load another viewcontroller's view, how do I accomplish that? I could add a button to do that, but I'd like to use gesture.
Thanks
THE FOLLOWING CODE WORKED:
(1) In the map view controller's header file, I added UIGestureRecognizerDelegate to support its protocol
(2) In map view controller's .m file , I added
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
(3) In map view controllers viewDidLoad method I added:
UISwipeGestureRecognizer *leftSwipe =
[[[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(leftSwipeReceiver:)] autorelease];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipe.delegate = self;
[self.view addGestureRecognizer:leftSwipe];
(4) This follow function is called for a left-swipe
- (void)leftSwipeReceiver:(UIGestureRecognizer *)recognizer
{
NSLog(#"leftSwipeReceiver:");
}

Related

UIView swipe gesture conflicts with tableview in objective c

I have more than 20 viewcontrollers in my project and added swipe gesture in global viewcontroller class.
UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(navigateToPreviousScreen:)];
rightSwipeGesture.cancelsTouchesInView = YES;
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipeGesture];
And override all sub class
- (void)navigateToPreviousScreen:(UISwipeGestureRecognizer *)sender {
[self.navigationController popViewControllerAnimated:YES];}
Now i got a problem (not exactly a problem), The some of the viewcontrollers has tableviews and that swipe is not work sometimes(also got some touch sensitive issue). Is there a way to solve it. I set cancelsTouchesInView = No but seems problem occurs.
Thanks in Advance.
You should be able to fix this by implementing & returning YES in the following UIGestureRecognizerDelegate method in the parent class.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

Put Swipe Gesture over UIWebView to Get Scroll Direction in IOS

I want to get the scroll direction in webView. I read the code about getting scroll direction here.
-(void)userDidScrollWebView:(id)scrollPoint{
// NSLog(#"scrolled:::");
NSString *x1 = [webView stringByEvaluatingJavaScriptFromString: #"scrollX"];
NSString *y1 = [webView stringByEvaluatingJavaScriptFromString: #"scrollY"];
NSLog(#"scroll x=%# y=%#", x1,y1);
if ([y1 isEqualToString: #"0"]) {
NSLog(#"RELAOD ME");
}
}
I have 2 Questions :-
About this code,I don't know where to call userDidScrollWebView method in my code so that I get regular updates about scrolling.
Another approach, I thought may be I could place Swipe Gesture over Web View but that's not working.
This is how I implemented swipe gesture in UIWebView.
Add <UIGestureRecognizerDelegate> protocol to ViewController.h
In ViewDidLoad Method add the following code:
UISwipeGestureRecognizer * swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeDown)];
swipeGestureDown.numberOfTouchesRequired = 1;
swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeGestureDown.delegate = self;
[self.webView addGestureRecognizer:swipeGestureDown];
Add Delegate method in ViewController.m :
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)swipeDown
{
NSLog(#"swipe down in webView");
}
Similarly you can add another gesture for swipe Up.

table view with UITapGestureRecognizer

Hi I am developing small IOS application in which I want to display search bar and below it table view. In which I want to hide keyboard when user click outside. For that reason I am using tap recogniser but becoz of that my table view stops listening for row selection.
Here Is my code
//inside view did load
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard {
[_searchBar resignFirstResponder];
}
but because of this my row selection of table view get disable. that mean didSelectRowAtIndexPath never get called. Any one have solution for this. Need Help. Thank you .
Try adding this line of code this will solve your problem..
tap.cancelsTouchesInView = NO;
You should implement UIGestureRecognizerDelegate and add the following:
//inside view did load
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
// UIGestureRecognizerDelegate methods
#pragma mark UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:yourTableView]) {
// Don't let selections of auto-complete entries fire the
// gesture recognizer
return NO;
}
return YES;
}
There is a "Search Bar and Search Display Controller" in the Utilities panel that sounds like it would be perfect for you. This is a good tutorial that explains how to implement it. This way you won't have the keyboard issue anymore.
There is no need to use TapGestureRecognizer. Use SearchBarDisplayController
hope this will work for you
download a demo project

Button calc stopped working

I have a simple program that does a calculation on a button press. The result is placed into a label using the following code:
//converts the float to 2 descimal places then converts it to a string
NSString *stringRectResult=[[NSString alloc]
initWithFormat:#"%1.2f",floatCalcResult];
//displays the string result in the label
resultLabel.text=stringRectResult;
It works perfectly, however, I added in code to hide the decimal keyboard when the user touches off the keyboard. That works, but when I added this code the button to update the label no longer worked. Can anyone help? The code to hide the keyboard is below. The app works when I comment it out but does not when it is active
In viewDidLoad:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(tap:)];
[self.view addGestureRecognizer:tapRecognizer];
Tap selector...
-(void)tap:(UIGestureRecognizer *)gr
{
[self.view endEditing:YES];
}
Thanks for any and all help.
The problem is that by intercepting all of the user's taps (in order to hide the keyboard), you're preventing any other user interface elements from being tapped. I would urge you to rethink your design; it's not usually necessary to have an explicit facility to hide the keyboard.
If you do want to keep this design, you can implement the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method in your class:
- (void) viewDidLoad
{
// ...
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(tap:)];
tapRecognizer.delegate = self;
[self.view addGestureRecognizer:tapRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Two things to keep in mind:
You need to mark your view controller as conforming to the UIGestureRecognizerDelegate protocol.
If you later add a second gesture recognizer to the view, you'll need to add a check inside the second method to treat the second recognizer differently.

MKMapView Not Calling regionDidChangeAnimated on Pan

I have an app with a MKMapView and code that is called each time the map changes locations (in regionDidChangeAnimated). When the app initially loads, regionDidChangeAnimated is called on pans (swipes), pinches, taps and buttons that explicitly update the map coordinates. After loading other views and coming back to the map the regionDidChangeAnimated is only called for taps and the buttons that explicitly update the map. Panning the map and pinches no longer call regionDidChangeAnimated.
I have looked at this stackoverflow post which did not solve this issue. The forum posts on devforums and iphonedevsdk also did not work. Does anyone know what causes this issue? I am not adding any subviews to MKMapView.
I did not want to initially do it this way, but it appears to work with no problems so far (taken from devforums post in question):
Add the UIGestureRecognizerDelegate to your header.
Now add a check for the version number... If we're on iOS 4 we can do this:
if (NSFoundationVersionNumber >= 678.58){
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchGestureCaptured:)];
pinch.delegate = self;
[mapView addGestureRecognizer:pinch];
[pinch release];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureCaptured:)];
pan.delegate = self;
[mapView addGestureRecognizer:pan];
[pan release];
}
Add the delegate methods to handle the gestures:
#pragma mark -
#pragma mark Gesture Recognizers
- (void)pinchGestureCaptured:(UIPinchGestureRecognizer*)gesture{
if(UIGestureRecognizerStateEnded == gesture.state){
///////////////////[self doWhatYouWouldDoInRegionDidChangeAnimated];
}
}
- (void)panGestureCaptured:(UIPanGestureRecognizer*)gesture{
if(UIGestureRecognizerStateEnded == gesture.state){
///////////////////[self doWhatYouWouldDoInRegionDidChangeAnimated];
}
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

Resources