Popover for button created via code for long press - ios

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.

Related

Add UITapGestureRecognizer on the left and right side of uibarbutton item to increase tap area

I am creating a barbuttonitem with the following code and i am adding a gesture recognizer to the navigation bar. So i need to know when the user tapped on the left or right of the barbuttonitem by 25 pixels. Is this possible using the gesture recognizer? Any help appreciated...
UIButton *searchButton = [self createSearchButton];
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureForTopLeftNotificationBarButtonItem:)];
[self.navigationController.navigationBar addGestureRecognizer:gr];
- (UIButton *) createSearchButton {
UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setImage:[UIImage sdk_imageName:#"Search_header_black"]
forState:UIControlStateNormal];
[searchButton addTarget:self action:#selector(searchBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
searchButton.size = CGSizeMake(32, 30);
searchButton.imageEdgeInsets = UIEdgeInsetsMake( 0, 8, 0, 0);
return searchButton;
}

Exclude specific area for long tap recognizer

I have a method that add button on top-left of my collection view cell, and longPress recognizer. The problem is, when i set longPressRecognizer minimumPressDuration to something like 0.0001, i cant tap a button, because, instead of tapping button activates longPressRecognizer method. Please take a look:
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[[RACObserve(self, shouldEdit) deliverOnMainThread] subscribeNext:^(id x) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
if (self.shouldEdit){
self.layout.longPressGestureRecognizer.minimumPressDuration = 0.1;
int ind = indexPath.row;
NSLog(#"1 blk called");
button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[cell addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(cell.mas_left).with.offset(0);
make.top.equalTo(cell.mas_top).with.offset(0);
make.width.height.equalTo(#(20));
}];
}
I use reactive cocoa and masonry, but this actually doesn't matter, what i want is exclude button area from area, that i can use for longGestureRecognizer.
self.layout.longPressGestureRecognizer.minimumPressDuration = 0.1;
you can use self.myview.longPressGestureRecognizer.minimumPressDuration = 0.1; and in myview don't place button in myview.by this you can able to tap button

UIButton - prevent call to touch down event when touch repeat is called

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];

Send button on Long press gesture

how can send button name, tag or text in long press gesture? I need to know the button name that long presed on.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTagS:[[ToalData objectAtIndex:i] objectAtIndex:4]];
[button addTarget:self action:#selector(siteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
///For long//
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];
////
NSString *string1 = [NSString stringWithFormat:#"%#", [[ToalData objectAtIndex:i] objectAtIndex:1]];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);
[self.view addSubview:button];
(void)handleLongPress:(UILongPressGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
NSLog(#"UIGestureRecognizerStateEnded");
}
else if (sender.state == UIGestureRecognizerStateBegan){
NSLog(#"UIGestureRecognizerStateBegan");
}
}
Every gesture recognizer is attached to a "view", which is a property on the gesture recognizer.
So in your "handleLongPress" method, you can do something like:
UIButton *button = (UIButton *)sender.view;
NSLog(#"title is %#", button.titleLabel.text);
NSLog(#"tag is %d", button.tag);

UIButton added to UIButton not responding to selector

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
}

Resources