iOS - Gesture and uislider - objective-c

I'm developing an app for iOS with a menu like Facebook with ECSlidingViewController, a nice project to manage the horizontal gesture on the app.
The problems borns when I put into the view an UISlider... I can't touch it nice because the sliding motion of the sliders is being mistaken as a swipe left/right.
This is the header file of che class with all the methods and this is the code to put in the main view:
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
if (![self.slidingViewController.underRightViewController isKindOfClass:[UnderRightViewController class]]) {
self.slidingViewController.underRightViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"UnderRight"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
This last line is the code that integrate the gesture on the app.
Is there a method to disable the gesture only on the uislider?
Thanks at all.

You can add a delegate to self.slidingViewController.panGesture and have it ignore touches that are on a UISlider view. The following question and answer have all the information you need: Pan gesture interferes with UISlider.

Related

Gesture Recognizer not working on iPad but working on iPhone, Why?

Why does this method not work on iPad?
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
NSLog(#"should receive");
return YES;
}
I have copied the same VC from iPhone Storyboard to iPad storyboard.
The code is simple, and all I'm trying to do is get the NSlog to appear.
While this is working on the iPhone simulator, it is not working on the iPad simulator (or a real device, checked with iPad 4).
Any suggestions as to what I should do to make it work on the iPad?
I am trying to work out what I have missed but nothing comes to mind.
STORYBOARD
IF you added your gesture recognizer in the storyboard.
Check that you set it as a delegate ..by right clicking the recognizer and dragging the mouse into the view controller. Pop up will show it clearly.
IN CODE
If the recognizer is added in code..then simply make sure you conform to protocol
and set its delegate.
#interface ViewController : UIViewController <XXXGestureRecognizerDelegate>
-(void)viewDidLoad
{
self.someGestureRecognizer.delegate = self;
//other code
}

Transparent UIView on top detecting touches

I have an iPad project structured with a UISplitViewController:
RootViewController
DetailviewController
Both of them are detecting touches with Gesture Recognizer inside their own Class.
I would like to create a transparent UIView on top of all the Classes to detect ONLY a Diagonal Swipe (from the left bottom corner to the right top corner).
So, when the swipe will be detected I will launch a function otherwise nothing appended and the touch should be passed on the low level view.
I tried these two solutions:
Add a GestureRecognizer on this top transparent view but this will hide all touches to the lower hierarchy views.( with userInteraction enabled: YES ofcourse);
The other solution is to make the init like this
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.01]];
[self setUserInteractionEnabled:NO];
}
return self;
}
and try to detect the swipe with
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
But at this point all the touches are not detected.
Anybody have a nice solution?
I will not create a transparent UIView like you are mentioning. I will add a UISwipeGestureRecognizer to the UISplitViewController's view this is already the view that contains all your subviews. You can have access to the view within the app delegate:
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
// attach the swipe gesture to the view that embeds the rootView and the detailView
UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:splitViewController.view action:#selector(swipeUpdated:)];
Can't you just add a gesture recognizer to the UISplitViewController's view?
You should look into Container Controllers. You can make your own SplitViewController and make a third view on top of the controller that detects the swipe. Custom container controllers are pretty straight forward and gives you a lot of flexibility.

How do I make the keyboard go away when a user clicks on the background of the view?

I have a UITextField in my iOS app. When a user enters text and clicks Return, the keyboard goes away due to a call to an IBAction with "resignFirstResponder."
However, XCode does not let me drag a line from the UIView itself to File Owner. How do I associate touching the background of a UIView with an IBAction that makes the keyboard go away?
You can use UITapGestureRecognizer. see: Dismiss keyboard by touching background of UITableView
so instead of tableview, just add it to your view instead:
UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)] autorelease];
gestureRecognizer.cancelsTouchesInView = NO; //so that action such as clear text field button can be pressed
[self.view addGestureRecognizer:gestureRecognizer];
and have a method to hide your keyboard
- (void) hideKeyboard {
[self.view endEditing:YES];
}
You've already noticed that you can't drag from the UIView to the file's owner to assoctiate an action with a touch.
The way to work around this is to change the class of the background view from UIView to UIControl and hook up an action from there to a method in your controller to stop editing.
That's because a UIControl can respond to touch events, and a UIView does not, but a UIControl subclasses UIView, and so it can be used in place of a UIView.
I wrote an example project a while ago that uses this technique. Have a look at the secondViewController's xib file and see how I've change the class of the background view and hooked it up to a an action in the controller to dismiss the keyboard.
Use the touchesBegan with Event and end editing on the view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
One easy way to do it is to create a big transparent UIButton behind the view.

How to disable gesture recognizer in navigationBar?

I have a question about disabling gesture recognizer in nationBar
Now, I'm developing an E-book application for iPad. I used UIGestureRecognizer to implement the effect of turning pages. but I coincidently found that if I swiped in the navigation bar at the top of screen, it also worked. So, how can I disable the gesture recognizer in navigation bar and just enable it for the rest of screen?
Assuming you have an outlet to your navigation bar, you should be able to handle this in your gesture handling method
- (void)handleGesture:(UIGestureRecognizer *)gesture {
if (CGRectContainsPoint([myNavBar frame], [gesture locationInView:self.view])) {
// gesture occured in your navigation bar, so return;
return;
}
// continue with your normal code for handling the gesture;
}
That should do the trick for you

How to add a vertical swipe gesture to iPhone app for all screens?

I'd like to add a gesture to my app so when the user swipes vertically it triggers a method to do something. The swipe can be up or down. I've never done anything with gestures so this is my first use of a gesture other than what is included in a UITableView for deleting rows.
The other problem is that most of my screens are UITableViews so the user could be simply scrolling the UITableView. So I am wondering if I could use a two finger swipe (vertical) to detect the gesture to run the code vs. a single finger swipe to scroll the UITableView?
Thank you in advance.
Neal
This goes in ApplicationDidLaunch:
UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired = 2;
swipeGesture.direction = (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown);
[window addGestureRecognizer:swipeGesture];
then implement
- (void) swipedScreen:(UISwipeGestureRecognizer*)swipeGesture {
// do stuff
}
Use the documentation for UIGestureRecognizer and UISwipeGestureRecognizer.
Also if you wish to detect the direction of the swipe you will have to setup two separate gesture recognizers. You can not get the direction of a swipe from a swipe gesture recognizer, only the directions it is registered to recognize.
In swift 4.0, that goes on the method didFinishLaunchingWithOptions of the AppDelegate:
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen(swipeGesture:)))
swipeGesture.numberOfTouchesRequired = 2
swipeGesture.direction = [UISwipeGestureRecognizerDirection.up, UISwipeGestureRecognizerDirection.down]
window?.addGestureRecognizer(swipeGesture)
And the action:
#objc func swipedScreen(swipeGesture: UISwipeGestureRecognizer){
Swift.print("hy")
}