How to implement common tap Gesture which will work for all View controllers - ios

i want to use one common tap gesture for show/hide slider view in view controller with multiple View controller classes.
please provide solution how to implement this for ios .
slider view will show / hide by tap of view of View Controller.
and slider view contain tableview so tableview cell also be select when user click on tableview cell in ios.
is there any way to create a Abstract class for that.
thanks in advance.

You can add tapgesture to main content view of VC like,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:recognizer];
And hanlde tap in method like,
-(void)handleTap : (UITapGestureRecognizer*)recognizer {
NSLog(#"tap detect");
}
Hope this will help :)

Related

iOS SWRevealViewController how to hide the left part programatically

I included SWRevealViewController objective-c library with my swift project,
it's working fine,
my question is that i want to hide that left menu programtically when the user clicks on an empty space inside its table view.
i didn't know how, could you help please
If you want to hide the left menu programmatically just call the method
- (IBAction)revealToggle:(id)sender;
which is in SWRevealViewController.m class
If you want to capture the click on tableView's area where there are no cells, you can follow SunburstEnzo's advice.
Add a UITapUITapGestureRecognizer on the viewDidLoad method of the class with the tableView.
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tableviewTapped)];
tapGesture.numberOfTapsRequired = 1;
[tapGesture setCancelsTouchesInView:NO]; //really important
[self.tableView addGestureRecognizer:tapGesture];
- (void) tapped
{
[self.revealViewController revealToggle:nil];
}

How can I add multiple taps to a button or a UIView?

I want one of my screens to be accessed only if the user taps three times on a button within a second. eg. if there's a button on View controller A, a user should tap 3 times within a second on that button to get to View Controller B.
Any help is appreciated!
Buttons are not set up to respond to multiple taps. You'll have to simulate that yourself.
As others have said, you can create a tap gesture recognizer and attach it to any view. For some views you'll need to set the userInteractionEnabled flag to true before it will respond though.
If you want a button to handle double-taps you'll need to have the button with no action installed but a 2 click gesture recognizer attached.
You need to add tap gesture recogniser to your custom View.
-(void)handleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer{
NSLog(#"3 tapped");
//add code to present another View Controller
}
-(void)buttonView{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGestureRecognizer.numberOfTapsRequired=3;
[self.customButtonView addGestureRecognizer:tapGestureRecognizer];
}
Swift:
func handleTapGesture(tapGestureRecognizer: UITapGestureRecognizer) {
NSLog("3 tapped")
//add code to present another View Controller
}
func buttonView() {
var tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
tapGestureRecognizer.numberOfTapsRequired = 3
self.customButtonView.addGestureRecognizer(tapGestureRecognizer)
}
You can call this method in viewDidAppear.

Hide UITableView on touches outside the tableview

I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;
I want to hide UITableView when a user touches outside its frame. Thanks for any help!
Best Approach
Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.
Show Overlay View first - alpha will be 0.5f and background color should be clear color.
show the table view.
NOTE: over lay view should have tap recogniser which will hide the overlay and table view
in View did load
UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(overlayDidTap:)];
[myOverLayView addGestureRecognizer:tapRecog];
- (void)overlayDidTap:(UITapGestureRecognizer *)gesture
{
//hide both overlay and table view here
}
Bad Approach
We should not add tap recogniser on main view itself. Because it may have lots of
controls inside of it. so when user tap on it. it will perform its operation. So to avoid
it we can simulate the same behaviour by above approach
You can get touch position by this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[self.view addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:self.View];
}
Then just check if the point is in tableview frame. If not then hide the tableview. Hope this help. :)
Subclass UITableView, override pointInside:withEvent:. It is templated for this reason.
Something like:
-(BOOL)pointInside:(CGPoint) point withEvent:(UIEvent*) event
{
BOOL pointIsInsideTableView = [super pointInside:point withEvent:event];
BOOL pointIsOutsideTableView = // Some test
return pointIsInsideTableView || pointIsOutsideTableView;
}
So you can catch the touch in table view implementation, where it belongs logically in this case.

iOS scroll view childs are not getting touched

I'm new to iOS so I'm getting problem with scroll view.
My scroll view having image views as subviews. Whenever I'm trying to touch image inside scroll view, scroll view touch is getting enable instead of subview.
How to resolve this problem?
Thanks
Add a Tap gesture recognizer to the scroll view for enabling touch inside scroll view:
UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(methodName:)];
singlTap.cancelsTouchesInView = NO;
[scrollViewName addGestureRecognizer:singlTap];
then write your code in specified method.
-(void)methodName:(UITapGestureRecognizer *)gesture
{
//your code here
}
Thats all

Can't Add IBAction Button in Container View

In my app, I have a view controller that contains container views embedded in a view. The container views are stacked on top of each other and hidden to start.
viewDidLoad
self.containerOne.hidden = YES
self.containerTwo.hidden = YES
I'd like to add an IBAction button to the first container view, that when touched, unhides the second container view. I can't seem to be able to add a IBAction connection from that button to the .h file. As if I'm not allowed to do that. Am I doing something wrong is this just not allowed? If it's not allowed, how do you suggest I create a parent view that has multiple child views that I can reveal one at a time? Thanks in advance for your time.
I don't believe UIContainerViews have actions associated with them. I haven't tested this, but you should try something where you add tapGestureRecognizers to the views and hide them depending on which action is fired:
- (void)viewDidLoad
{
UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(containerOneTapped)];
firstTap.cancelsTouchesInView=NO;
[self.containerOne addGestureRecognizer:firstTap];
[firstTap release];
UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(containerTwoTapped)];
secondTap.cancelsTouchesInView=NO;
[self.containerTwo addGestureRecognizer:secondTap];
[secondTap release];
}
-(void)containerOneTapped
{
self.containerTwo.hidden=NO;
}
-(void)containerTwoTapped
{
self.containerOne.hidden=NO;
}

Resources