Exclude specific area for long tap recognizer - ios

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

Related

Tap and Long Press Gesture when creating grid of buttons Objective-C

Good evening, I'm trying to create a grid of buttons, like in the following code.
I'd like to add t a TapGesture and a LongPress Gesture to each of the buttons; how do I do it?
I know this is a way to do it...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(longPress:)];
longPress.minimumPressDuration = 3;
//[self.griglia[1][1] addGestureRecognizer:longPress];
If I do so, it says "Property "griglia" not found on object of type Gioco
Gioco.m/Gioco.h is my class
and UIButton* griglia[9][9]; is my button grid
Could you correct the following code?
for(int i=0, y=212, p=0; i<9; i++)
{
for(int k=0, x=37; k<9; k++)
{
griglia[i][k] = [UIButton buttonWithType:(UIButtonTypeCustom)];
//[griglia[i][k] addTarget:self action:(#selector(click)) forControlEvents: UIControlEventTouchDown];
[griglia[i][k] setTag: 0];
if(i==posizioni[0][p] && k==posizioni[1][p])
{
numero=[NSString stringWithFormat:#"%d", matrice[i] [k]];
[griglia[i][k] setTitle: numero forState: UIControlStateNormal];
[griglia[i][k] setEnabled:NO]; //Numeri non modificabili
[griglia[i][k] setTag: matrice[i][k]];
}
[griglia[i][k] setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
griglia[i][k].frame=CGRectMake(x, y, 27, 27);
[self.view addSubview:griglia[i][k]];
if(k==2 || k==5)
x=37+3+k*34;
else
x=37+k*34;
}
if(y==2 || y==5)
y=212+4+i*34;
else
y=212+i*34;
}
This is not making sense.
You are creating a UILongPressGestureRecognizer
and then
//[griglia[i][k] addTarget:self action:(#selector(click))
forControlEvents: UIControlEventTouchDown];
Those are two different things.
A touchdown is a gesture, but very different than the long press.
I would suggest you try to add a gesture recognizer, and not add a touchdown event to your buttons.
Or...
take a look at this answer instead:
Adding Tap Gesture on UIImage

Create a function that is able to check the title of the button pressed

I have a loop for creating buttons dynamically.
for (int b = 0; b < _currentPlayerTeams.count; b++)
{
HNTeamObject *team = _currentPlayerTeams[b];
CGFloat buttonY = 168 + ((b + 1) * distanceBetweenButtons) - 23;
NSString *buttonTitle = [NSString stringWithFormat:#"%# %#",team.teamName, team.seriesName];
UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"images.png"] forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self
action:#selector(chooseTeamOne:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle: buttonTitle forState:UIControlStateNormal];
button.titleLabel.text = (#"%#", buttonTitle);
button.frame = CGRectMake(labelAndButtonX, buttonY, 268.0, 46.0);
[self.view addSubview:button];
}
I need to make a function that can act as the selector that for each button that is created and look at the button's title. Since I am making the buttons in the loop they are local and do not appear in another function that I create. Any help would be appreciated. I apologize in advance because I am very new to coding and am not quite up to speed with what is going on. Thanks.
Each button will call the chooseTeamOne: function when it is pressed so I assume you want to get the buttons title in that method.
To do so, use the UIButton's title property:
NSLog(#"%#", button.currentTitle);
This will log the button's current title. What is important to note is that I am referencing "button" which is the instance of the UIButton which called the chooseTeamOne method. I am assuming chooseTeamOne takes a UIButton button as a parameter.
If your method takes an 'id' as a parameter you would need to cast the sent object to a UIButton like this:
- (IBAction)chooseTeamOne:(id)sender {
UIButton *button = (UIButton *)sender;
NSString *buttonTitle = button.currentTitle;
}
Do you need a separate selector to return the title? If not then..
I imagine that your chooseTeamOne function is something along the lines of:
-(IBAction)chooseTeamOne:(id)sender {
...do things...
}
in which case, just add
-(IBAction)chooseTeamOne:(id)sender {
UIButton *button = sender;
NSString *stringThatYouWant = button.titleLabel.text; //get the text on the button
...do things...
}

Popover for button created via code for long press

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.

How to remove UIButton in iOS7

I want to create and remove uibutton. (iOS7)
My viewDidLoad function creates uibutton in the following way:
for (char a = 'A'; a <= 'Z'; a++)
{
position = position+10;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(getByCharacter:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%c", a] forState:UIControlStateNormal];
button.frame = CGRectMake(position, self.view.frame.size.height-40, 10.0, 10.0);
button.tintColor = [UIColor whiteColor];
[self.view addSubview:button];
}
Now I want to remove specific uibutton based on button text.
- (IBAction)getByCharacter:(id)sender{
}
I don't know how to do this. Can anyone provide some sample code?
You can remove the button by calling
[sender removeFromSuperview];
in your callback (getByCharacter:). When you attach the selector to the button, the sender parameter will be the button which triggered the event. That means that you have an instance of it. With this instance you can now calll the removeFromSuperview method.
First you need to get the button's title text and then remove it from superView if it matches to the specific character,
- (IBAction)getByCharacter:(id)sender{
UIButton *myButton = (UIButton *) sender;
if([myButton.titleLabel.text isEqualToString:#"C"]{
[myButton removeFromSuperView];
}
}

Show an image in a UIButton on long press gesture

I have the following problem. I have a UIScrollView on which I have a couple of buttons with icons set as images on these buttons. I have a long press recognizer attached to each button. How can I show a smaller delete icon on the sender button on long press gesture? My goal is to create the behaviour that is presented by iOS when the user wants to delete a specific application.
This is the code for a buttons (with images):
//set the button with the image of conference here.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(3, 3, w-5, h-5);
CALayer * l = [button layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];
[button setImage:thumb forState:UIControlStateNormal];
button.property = confInfo;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
bView.tag = i;
//Add gesture recognizer to be used for deletion of conference.
UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressGestureRecognizerStateChanged:)];
pahGestureRecognizer.minimumPressDuration = 1.0;
[button addGestureRecognizer:pahGestureRecognizer];
This code is in a cycle (see i in the code). My long tap action is like this:
- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateEnded:
NSLog(#"Tapped!!!");
break;
default:
break;
}
}
How I can pass a button on which I clicked to this action to show the smaller X image on the right upper corner of the button ?
Your gesture recognizer should be attached to the UIButton via its view property.
case UIGestureRecognizerStateEnded:
NSLog(#"Tapped!!!");
[((UIButton*)gestureRecognizer.view) setImage:thumbWithX forState:UIControlStateNormal];
break;

Resources