Can't Recognize Touch On navigation Bar's Image View.? - iphone

I have a View With Navigation Bar
Actually I have taken a Image View and Put it on a navigation bar....
As I want Button Not at right or Left but at somewhere on the navigation bar
By this method I can recognize touch on view but can not recognize touch on Navigation Bar ?
Can I change anything...
In this method...
Why?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
CGPoint p;
p.x=0;
p.y=0;
p = [[touches anyObject] locationInView:self.nvbrCalculateBar];
NSLog(#" %f",p.x);
if(p.x==0 && q.x==0)
{
}
else
{
//Load View
}
}

You can add a UITapGestureRecognizer to your UIImageView and that should do the trick
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapEvent:)];
[yourImageView addGestureRecognizer:tap];
[tap release];
- (void)tapEvent:(UITapGestureRecognizer *)gesture {
//Do whatever you need when user taps the imageView
}
Also set the userInteractionEnabled of the imageView to YES to be able to receive the gesture recognizer events.
Hope this helps.

The UINavigationBar itself is not set to return touches, since I believe its not part of the actual UIView (for which the touchesBegan) method applies. Instead, you can push various items onto the nav bar (e.g. buttons) which you can define interactions for.
See its reference here.

USer Interaction Must be enabled of UINavigation Bar Property, And then Apply touch method..

Can you try:
[imageView setUserInteractionEnabled:NO];
This should prevent your imageview to cancel the touches.

Related

Overlapping views and gestureRecognizer iPhone

I have a question - is there any possibilities in iPhone to make UITapGestureRecognizer respond the tap only on a part of UIImageView. Actualy, the problem is as follows - there is an UIImageView with gesture recognizer add and some part of these UIImageView is covered with other View. When tapping on this other View, UIImageView recognizes the tap. How this problem can be solved?
I'm not sure I understand your question. Do you want the gesture recognizer to be triggered when the view which overlaps the image view is tapped? If so, I guess you could just add the gesture recognizer to the overlapping view.
If you have two overlapping views and want to only handle the touch if the overlapped part was touched, I suggest adding the tap gesture recognizer to the main view and check if the overlap was touched.
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureRecognizerTriggered:)];
[self.view addGestureRecognizer:gestureRecognizer];
}
- (void)tapGestureRecognizerTriggered:(UITapGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.view];
if (CGRectContainsPoint(self.frontView.frame, location) &&
CGRectContainsPoint(self.backView.frame, location))
{
// Handle touch
}
}
If you want to handle the touch if the touch happens in the front CGRectContainsPoint(self.backView.frame, location) from the conditional statement.

How to implement close a subview when user touched its superview in iphone?

I have a view controller and I have created an another view in same view controller and added it as a sub view.When a button clicked the subview will appear and it takes half of the screen.How can I close the subview when user touched the superview(main view in background) ?
You can use a gesture recognizer and on a single tap action on the superview call removeFromSuperview on the subview.
In your viewDidLoad add something like this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[mainView addGestureRecognizer:singleTap];
And then add the function:
- (void) handleSingleTap: (id) sender
{
[secondView removeFromSuperView];
}
For more control of when you want the subview to be removed, you could override the mainViewController's responder method like so:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[subview removeFromSuperView];
}
This way you can actually see where in the screen the user tapped, and respond accordingly.

how to know uitableview is pressed when empty

i am making a chat application, and in the chat window there are uilabels in uitableviewcells. initially the keyboard will be present but when the user touches on any place on uitableview, i will make the chat window as fullscreen (dissappearing keyboard).
i cant find a way/trick to accomplish this.
i have tried the following method: by using tableview:didselectrowatindexpath, i am able to do it but, user needs to press on an existent uitableviewcell. but i want to understand the press even when uitableview is empty..
note: my chat tableview is interactive e.x. some rows will include image button which need to be pressable, so i cant just put an invisible button onto uitableview.
thank you for your thoughts
Aytunc Isseven
What you want to do is add a gesture recognizer to the UITableView that responds to the appropriate gestures. I would recommend against using UITapGestureRecognizer as the UITableView is already using taps for selecting the cells, so you might want to try the UILongPressGestureRecognizer. I put together a small sample of how you can do this as follows:
In my viewDidLoad I did the following:
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressFrom:)];
[self.tableView addGestureRecognizer:gesture];
and the handleLongPressFrom: is as follows:
- (void)handleLongPressFrom:(UILongPressGestureRecognizer *)recognizer {
NSLog(#"handleLongPressFrom: %#", recognizer);
// Add real code here
}
The full list of gestures can be found here.
Oh, if you did want to still use tap, check out this stack overflow question. I don't know if the method presented works fully, but it'd be a good place to start.
Using UITapGestureRecognizer with a UITableView:
Okay, since the tap gesture seems to be the correct one for your use case you can try and do the following. Step 1 is to set up the gesture recognizer as I listed above using the tap gesture instead of the long press gesture.
The code in viewDidLoad is very similar with an important addition ...
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
// we need to set the gesture delegate so we can allow the tap to pass through to the
// UITableViewCell if necessary.
gesture.delegate = self;
[self.tableView addGestureRecognizer:gesture];
The handleTapFrom: function is pretty much the same with just the different gesture recognizer as the parameter.
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
NSLog(#"handleTapFrom: %#", recognizer);
// Add real code here
}
The major changes to this approach is that we need to implement the UIGestureRecognizerDelegate protocol. Since our goal is to allow the tap gesture to pass through the UITableView to it's subviews (i.e. the UITableViewCell and it's components) we need to implement the gestureRecognizer:shouldRecieveTouch: function. The following implementation should cover what you are attempting.
#pragma mark UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// If the view that is touched is not the view associated with this view's table view, but
// is one of the sub-views, we should not recognize the touch.
if (touch.view != self.tableView && [touch.view isDescendantOfView:self.tableView]) {
return NO;
}
return YES;
}
The isDescendantOfView: function returns YES if the view it is testing against is the same as the view doing the testing, so we need to accommodate that case separately. You can generify this function by using gestureRecognizer.view instead of self.tableView, but I didn't think it was necessary in this case.
The trick is to make your viewController put a tap recognizer on the view but make it always opt out by returning NO from the delegate method "gestureRecognizerShouldBegin". That way gestureRecognizerShouldBegin gets called for every touch on the view, but you don't interfere with the normal event handling of the table.
- (void)viewDidLoad {
[super viewDidLoad];
[self detectTouchesOnView:self.tableView];
}
- (void)detectTouchesOnView:(UIView*)theView {
UITapGestureRecognizer* tapR = [[UITapGestureRecognizer alloc]initWithTarget:nil action:nil];
tapR.delegate = self;
[theView addGestureRecognizer:tapR];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// React to the UITableView being touched E.G. by hiding the keyboard as below.
[self.view endEditing:YES];
return NO;
}

touchesBegan to dismiss a view

I have a ViewController with a full screen UIImageView. I've hidden statusBar and NavigationBar, so there is no way to go back but tapping somewhere.
So i was thinking to go back just using this touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self dismissModalViewControllerAnimated:YES];
}
I also tried to use this way
-(void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES];
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissModalViewControllerAnimated:)];
[imageFrame addGestureRecognizer:tapRecognizer];
imageFrame.image = [UIImage imageWithContentsOfFile:image];
[tapRecognizer release];
}
I got stuck there, with this full screen image and i can't go back.
How can I dismiss the viewcontroller?
If you are using a navigation bar I'm assuming that you did a pushViewController to start this view controller? In which case you would want
[self.navigationController popViewControllerAnimated:YES];
to dismiss the controller.
Of course your solution assumes that the image view is in fact a modal view controller presented with presentModalViewController:animated: somewhere.
My suggestion would be to have a UIButton placed on top of your image view and sized the same. Hook up its Touch Up Inside to your go-back method and then set its alpha to 0 or Hide it through IB or properties. The button won't be displayed but it'll still receive touch events.
You should call dismissModalViewControllerAnimated: on the parent view controller, and not on the one you wanna hide.
Also make sure to set the UIImageView's userInteractionEnabled property to yes, because unlike regular UIView's it doesn't answer touch events by default.

How to mask out a view when filling an area?

could anyone tell me how to place a translucent black mask over the whole screen, but with the area of a particular UIView being excluded? I want to use this mask over a UITextField, which calls resignFirstResponder when the outside part of the textfield is tapped.
The subview tree would be like:
UIWindow
|-UIView
| |-UITextField
|
|-Mask
Thanks,
You can use the:
- (void)bringSubviewToFront:(UIView *)view
And send the UITextField to the front after you add the black mask view.
UPDATE
Ok this are the steps to do it (you can see the apple example for UIGestureRecognizers for more)
create a mask view (programmatically or with IB) and call it "maskView".
create a gestureRecognizer and add it to the maskView.
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
recognizer.delegate = self;
UIImageView *maskView = [[UIImageView alloc] init];
[maskView addGestureRecognizer:recognizer];
you will need to set your view controller as the delegate for "UIGestureRecognizerDelegate"
#interface YourViewController : UIViewController <UIGestureRecognizerDelegate>
add the maskView to your ViewController when you want to mask the screen. and then move the text field above the mask.
[self.view addSubView:maskView];
[self.view bringSubviewToFront:textField];
set this 2 functions:
in the first one you can set the action if the user touches the mask
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
//resign the first responder when the user taps the mask
//you can remove the mask here if you want to
}
in the second one you tell the app not to receive touches from the textField
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == textField)) {//checks if the touch is on the textField
return NO;
}
return YES;
}
Hope it make some sense
shani