How to show popover on selecting an image - ipad

I am trying to show a popover on selection of a image in my iPad.So how can i do it?

Put a transparent button on the image and of the size of the image and then use the UIPopoverController to launch a pop over view... hope this helps.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mousePoint = [touches anyObject];
CGPoint point = [mousePoint locationInView:self.view];
_didClickMark=CGRectContainsPoint(mImageView.frame, point);
if(_didClickMark)
// show popover
}
This might help you.

Related

iOS know when touchesEnded if touches on top of certain view

I have to do some "snap to grid" behaviour,
I have a dragging image "sprite", but I need to "snap" this sprite to a certain button if touches go on top of that button,
so how can i know if touchesEnded is on top of a certain button,
here my code, but i dont know when touches where ended on top of butotn
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint location = [touched locationInView:touched.view];
CGRect recta = touched.view.frame;
if (CGRectIntersectsRect(recta, self.button_1.frame)) {
DLog(#"intersected");
}
}
So this is not happening,
can it be done?
or do i have to check the X and Y position of finishing touch against the button frame X and Y position by my self?
cheers
touched.view is the view on which the touch started. You want to find out if the location where the touch ended is on top of a specific button. You can do this by checking if the location of the touch relative to the button is within the button's bounds.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint locationRelative = [touched locationInView:self.button_1];
if (CGRectContainsPoint(self.button_1.bounds, locationRelative)) {
DLog(#"on top of button_1");
}
}
You can use CGRectIntersectsRect, which returns YES, if the two rectangles your pass to it intersect.

How to detect if Touch input intersects uiimageview

Can't seem to find an answer to this one anywhere.
My game starts when the user touches the screen, there is a path that the finger must stay within, if it touches/intersects the edges then I want it to run the method [self gameover].
The edge will be a UIImageView.
Thanks everyone.
I hope you have overridden the touches moved event. check either the user touch intersects your image view like below.
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point =[touch locationInView:self.view];
if (CGRectContainsPoint(self.imageView.frame, point))
{
//Intersects
[self gameOver];
}
}

How to get the location of a touch using touchesEnded inside a button

I am using the code below to try and get the location of a touch but it only works outside of the button. Eg. it prints the console line if the touch is outside the button but if not, it doesn't work. What is solution to this?
Here is the code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.appleClickedButton];
NSLog(#"%f",touchPoint);
NSLog(#"%f",touchPoint);
}
Thanks
Here's how you can get touch location when a UIButton is tapped. (just make sure you link TouchUpInside event with the IBAction)
- (IBAction)buttonTapped:(id)sender forEvent:(UIEvent*)event
{
UIView *button = (UIView *)sender;
UITouch *touch = [[event touchesForView:button] someObject];
CGPoint touchPointInButton = [touch locationInView:button];
NSLog(#"Location in button: %f, %f", touchPointInButton.x, touchPointInButton.y);
}
Once you get the touch point inside the button, you can calculate what the touch point would be in the super view.
When you touch inside the button. the event has been handled by the button. The view which contains the button will not get the touch event.
You can create a custom class inherit from the UIButton and override the method, but please make sure to call the the method in the super class or some actions such as TouchUpInside will not work correctly.
You could try UIButtons addTarget:action:forControlEvents: with a control event mask including UIControlEventTouchUpInside.

Decrease Touch Area of UIButton

I am trying to decrease the touch area of an UIButton. Is that even possible? When the user touches on the button and drags his touch outside the buttton the touch event should stop immediately when the graphic of the button ends. Unfortunately the area is much bigger than the actual graphic. I found a lot things on how to increase the area but not how to make it smaller.
Thanks for your help.
I came up one solution. You can subclass UIButton and override touchesMoved: so that it recognize the touch to be ended if it was outside of the button. Here is my snippet.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if(!CGRectContainsPoint(self.bounds, touchPoint))
{
[super touchesEnded:touches withEvent:event];
}
else
{
[super touchesMoved:touches withEvent:event];
}
}
The drawback of this is that if you go out of the button and come back again, the button will not become active. But otherwise, I think it should work fine.

Get the coordinates of a touch and place a button on that position?

I am trying to get the coordinates from a touch but I can't figure out how.
I am in a view controller and I try to use the method -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event but I think it doesn't work with view controllers.
Am I right?
I just want to put a Button on the position I touched.
Thanks in advance!
It belongs in a View rather than ViewController, and would look something like this:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)evt {
UITouch *touch=[touches anyObject];
CGPoint pt=[touch locationInView:self];
// ...make your button at 'pt'...
}

Resources