How do I handle event when user touch up outside the UIView? - iphone

I have a custom popup menu in my iOS application. It is UIView with buttons on it. How do I handle event when user touch up outside this view? I want to hide menu at this moment.

You should create a custom UIButton that takes up the whole screen. Then add your subview on top of that button. Then when the user taps outside of the subview they will be tapping the button.
For example, make the button like this:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(yourhidemethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = self.view.frame;
[self.view addSubview:button];
(where yourhidemethod: is the name of the method that removes your subview.) Then add your subview on top of it.
Update: It looks like you're wanting to know how to detect where touches are in a view. Here's what you do:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self]; //location is relative to the current view
// do something with the touched point
}

Put it in full-screen transparent view and handle touches for it.

One idea is to have an invisible view(uicontrol) that is as big as the screen which then holds this custom popup.

It sounds like you'd be better served by deriving your menu from UIControl rather than UIView. That would simplify the touch handling, and all you'd have to do in this case would be to set a target and action for UIControlEventTouchUpOutside. The target could be the menu itself, and the action would hide or dismiss the menu.

Related

Is there a way to get the location of a users touch while pressing UIButton?

While my user is dragging his finger while holding down a UIButton, can I get the touch location for the finger at that point? If I can't do this it seems that I could probably create a UIImageView and give it button qualities. After this I would implement the touches began, moved, ended methods.
These methods; however, do not run while a UIButton has been touched.
The reason I need this is because I wanted to find out the direction of a touch drag outside movement, and for various other reasons too, but if this is impossible I will have to create UIImageViews I think.
Also, I do not subclass UIButton, I am too much beginner to take on apples clusters.
you should following this:
[myButton addTarget:self action:#selector(dragHandler:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[myButton addTarget:self action:#selector(dragHandler:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
-(void)dragHandler:(UIButton *)sender withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
NSLog(#"touchInfo:%#", NSStringFromCGPoint([touch locationInView:self.view]);
}

Tapping on Buttons over MapView is zooming the Map..?

I have a MapView, Over which i have added a UIView (0, 216, 320, 200) contain 10 UIButtons. This UIView is to work like a menu, and Shows and Hides on a certain button action.
The problem here is, While the Menu is showing, when i tap a button single ime, it works perfectly, but tapping it very quickly multiple times makes the MapView Zoom(Which is under the UIView which contain the button). It means the touch event is passing to MapView.
Detail: Tapping on the button very quickly cause in MapView Zoom, and the Button Action performs when you stop tapping.
How can i prevent that?
What you could do is disable user interaction for the duration of your button's handler. So in the selector do:
-(void)buttonSelector:(id)sender {
_mapView.userInteractionEnabled = NO;
// ... do what your button needs to do
_mapView.view.userInteractionEnabled = YES;
}
Of course, this assumes your button gets the event before the map view... which I think it should because it's on top of the map view.
To handle the rapid tapping, you could create a subclass of UIButton and use the following code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_mapView.userInteractionEnabled = NO;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_mapView.userInteractionEnabled = YES;
}
In the builder change the class of the UIButton to your subclass. If you debug the touch in the set passed to the touchesEnd method and look at the tapCount, you'll see all your taps have been caught here.
Yes, so the problem was, I was adding UIView as subview over MapView.. I fixed it by adding the UIView as subview on self.view

How to perform some Action on imageview clicked?

I'm working on a photo Collage application. I want to perform an action on image view so any one can help me to solve my problem. when i pick image from photo library and shown on image view after when double click on image view i want to push or present a new view to crop this image .....so anyone tell me what i do to solve my problem...
Use a UIButton of type custom and put an image inside it. And you are done.
Add the Gesture for ImageView
UITapGestureRecognizer *sg=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(HandleEvent)];
sg.numberOfTapsRequired=2;
[BottomBar addGestureRecognizer:sg];
[sg release];
and do your task here:
-(void)HandleEvent
{
// your task
}
Thanks
You should use a UIButton instead of an UIImageView. You can set its style to custom and then asign it an image. UIButton provides all the methods for double tap already built in.
If you are using UIImageView, make sure you use setUserInteractionEnabled:YES. Then you need to check your touches in touchesBegan, ended, (canceled).
You are set on using UIImageView, you will have to subclass UIImageView and override the touch methods you want to intercept. For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Call super to make sure you don't lose any functionality
[super touchesBegan:touches withEvent:event];
// Perform the actions you want to perform with the tap.
}
Using UIButtons as replacements for your UIImageViews is a great idea you can just drag a connection from each button to an outlet in your view controller and handle the action. You can set the background image on the buttons as you are doing currently with the UIImageViews. Set your button type to custom and you'll be good to g
If you are using UIImageView class ,first set setUserInteractionEnabled:YES . Then set UITapGestureRecognizer event to image view.
try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
CGPoint viewPoint = [imageView convertPoint:locationPoint fromView:self.view];
if ([imageView pointInside:viewPoint withEvent:event]) {
//do something
}
}
Make a button, and clear its color. On the back of button, put your image view, so that your imageview seems to be visible and the button doesn't. Now attach event to the button.

How to get the id of button & implementing touchesMoved Simultaneously?

I have a situation i have a UIButton Class in which upon selecting a button i am getting the id of the buttton based upon it i am changing the color of the button by using [self addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
And by using touchesBegan, touchesMoved, touchesEnded method i can drag drop the button object to any part of screen.
Now the problem is if i uses touchesBegan, touchesMoved, touchesEnded method then i am not getting id of button so i am not able to change the color.
So how can i able to get both the problem solved?
one of the parameters for touchesBegan is a set of UITouch objects. UITouch has property "view", which is the view that you tapped on. So something like
-(void)touchesBegan:touches withEvent:e
{
id* myButton = [touches anyObject].view;
}
If you call the following method on your parent view (containing all the buttons) in touchesBegan: it should return you the button you are touching....
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Hope this helps.

How to drag and drop a UIButton

I have four UIButtons in my interface file. I have outlets attached to each button. I want to be able to drag and drop them across the screen. What is the best way to do this? How can I do this? please help!!!
try this u can drag and drop your button what ever you want
implement this code in ViewDidLoad
[self.shoeimageOutlet addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.shoeimageOutlet addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
it will call the method written below
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
There are 3 possibilities nowadays on how to enable items – that is UIViews and UIControls – to be draggable.
override touchesMoved
add a target/selector for dragging control events
add a pan gesture recognizer
The complete tutorial found here !!!