I try to implement tapping on a custom annotation, when it is tapped, the row of this on table will be highlighted,
I tried adding gesture recognizer to custom annotation, but it does not work.
I alsa tried annotation did select method, the result is the same.
My code is as below;
UITapGestureRecognizer *singlePress =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSinglePress:withIndex:temp:)];
[annotationView addGestureRecognizer:singlePress];
[annotationView setUserInteractionEnabled:YES];
Add callout button on annotationView such like,
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
myCustomAnnotationView.rightCalloutAccessoryView = rightButton;
and call method
-(void)showDetails:(UIButton *) calOutBtn
{
// do your stuff;
}
EDITED:
In your Case you need to add such like,
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(TouchViewMethod:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[ annotationView addGestureRecognizer:gestureRec];
Related
I have a button that behaves in one way when user taps on it and another when user double taps on it. In case the user double taps the button, i don't want the single tap behavior to happen.
How can i prevent the call to the touch down event in case double tap was recognized?
You can use Target-action; For UIControlEvents, you can use "UIControlEventTouchDown" and "UIControlEventTouchDownRepeat" like this:
UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(150, 200, 50, 50);
[button addTarget:self action:#selector(buttonSingleTap:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(buttonMutipleTap:) forControlEvents:UIControlEventTouchDownRepeat];
[self.view addSubview:button];
- (void)buttonSingleTap:(UIButton *)btn{
[self performSelector:#selector(buttonAction:) withObject:btn afterDelay:0.5];
}
- (void)buttonAction:(UIButton *)sender{
NSLog(#"single tap");
}
- (void)buttonMutipleTap:(UIButton *)btn{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(buttonAction:) object:btn];
NSLog(#"mutiple tap!");
}
But there will be 0.5sec to delay!
As I understand you want different behavior on the same button,So simply apply two different tap gesture.The below code may help you.
UIButton *btn1=[[UIButton alloc]init]; //your button
//Two diff method call for two diff behaviour
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapEvent:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapEvent:)];
//specify the number of tapping event require to execute code.
singleTap.numberOfTapsRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[singleTap requireGestureRecognizerToFail:DoubleTap];
//Apply multiple tap gesture to your button
[btn1 addGestureRecognizer:singleTap];
[btn1 addGestureRecognizer:doubleTap];
I know how to create popovers if my button added to story board, but how can I create popover if my button created via code.
UIButtonS *button = [UIButtonS buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(siteButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];
[self.view addSubview:button];
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
}
else if (sender.state == UIGestureRecognizerStateBegan){
//create popover for button
}
}
You are already doing it right, but you are overthinking. There is no need to check the state of the gesture recognizer. If the target function has been triggered, it means that the user has done a long press. Also, note that not all of the values of the property state may be supported. As the documentation says: Some of these states are not applicable to discrete gestures.
So your code should look like this (unless you want to implement dragging or something similar):
UIButtonS *button = [UIButtonS buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(siteButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];
[self.view addSubview:button];
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
//create popover for button
}
If your target is iOS 6+ you should use a UIPopoverController to create the popover, otherwise use UIAlertView.
For my current project, I need to drop some pins on a image(NOT a map), and also should be able to click the pin to add some comments to this pin. I wonder if I can use MKAnnotation/MKAnnotationView to do this. I have searched on Internet for a while. I only find tutorials about how to customize MKAnnotation with other images.
If I cannot use MKAnnotation, what should I use? Any tutorials about this will be great helpful.
Thanks.
If you want to click on the pin, you should just place a UIButton on top the image view. MKAnnotationView can only placed within a map view.
You can do something like that:
#property (nonatomic,strong) UIButton *button;
- (void)viewDidLoad
{
[super viewDidLoad];
_button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
UIImage *buttonImage = [UIImage imageNamed:#"image.jpg" ];
[_button setImage:buttonImage forState:UIControlStateNormal];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTouched:)];
tapGesture.numberOfTapsRequired = 1;
[_button addGestureRecognizer:tapGesture];
[self.view addSubview:_button];
}
-(IBAction)imageTouched:(UITapGestureRecognizer *)sender
{
CGPoint touchedPoint = [sender locationInView:_button];
UIButton *pin = [[UIButton alloc] initWithFrame:CGRectMake(touchedPoint.x, touchedPoint.y, 10, 10)];
}
You can also link in from the storyboard of course.
Set the image of the button for the state UIControlStateHighlighted or UIControlStateSelected if you don't want it to flash/show user interaction
I have added an ui button to a disclouser button
And implemented selector method
but the action selector doesn't respond to the button why and what to do
//To add a discloser button to show route map
UIButton *detailBtn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.leftCalloutAccessoryView=detailBtn;
[detailBtn addTarget:self action:#selector(showRoute:) forControlEvents:UIControlEventTouchUpInside];
DisplayMap *ann=(DisplayMap *)annotation;
detailBtn.tag = ann.detailButtonTag;
UIButton *btnOnPopup=[[UIButton alloc] initWithFrame:CGRectMake(- 207, -6, 300, 43)];
btnOnPopup.backgroundColor = [UIColor whiteColor];
[btnOnPopup addTarget:self action:#selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];
btnOnPopup.tag = ann.detailButtonTag;
btnOnPopup.userInteractionEnabled = YES;
[detailBtn insertSubview:btnOnPopup aboveSubview:detailBtn];
the selector
[btnOnPopup addTarget:self action:#selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];
(Second button btnOnPopup) is not working.
Your btnOnPopup is out of detailBtn's bounds so touches won't even get to it. Adding a button to a button is not really a great practice anyway. Adding it to a button's superview might be a little bit better (and will make your touches work, but you might need to adjust frame)
[detailBtn.superview insertSubview:btnOnPopup aboveSubview:detailBtn];
Add view and tap gesture recognizer instead of button:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureHandler:)];
[singleTap setNumberOfTapsRequired:1];
[view addGestureRecognizer:singleTap];
...
[detailBtn addSubview:view];
...
- (void)tapGestureHandler:(UIGestureRecognizer *)gestureRecognizer
{
// your code for tap handler is here
}
How can I put a info button in the imageview frame reserved in uitableViewcell?
Thanks in advance!
All you need to do is create the button and add it to the cell:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton setFrame:CGRectMake(10,10,infoButton.frame.size.width, infoButton.frame.size.height)];
[infoButton addTarget:self action:#selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:infoButton];
Edit---
As you want to use the imageView property of the cell, you are required to provide your own image and you need to set a gesture recogniser so that you know when the imageView has been tapped.
[cell.imageView setImage:[UIImage imageNamed:#"info-button.png"]];
[cell.imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(infoImageTapped:)];
[tap setNumberOfTapsRequired:1];
[cell.imageView setGestureRecognizers:[NSArray arrayWithObject:tap]];
[tap release];
- (void) infoImageTapped:(UITapGestureRecognizer *) tap {
//show info view.
}