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);
Related
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.
I'm having a problem that I fear has a simple solution. Each time the 'createNewSetInDB:' method is called, a new UIButton* is created, assigned a target for when the user presses the button, and assigned a UILongPressGesture* for when a user long presses the button.
The 'openSet:' method is correctly being called when a user taps one of these buttons. The 'showHandles:' long press method is only being called for the LAST UIButton* that was created. So if the 'createNewSetInDB:' method gets called 4 times and therefore creates 4 UIButtons, the first three do not handle the UILongPressGesture. The 4th UIButton does.
Any ideas?
UILongPressGestureRecognizer *showHandlesLongPress;
- (void)viewDidLoad
{
showHandlesLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(showHandles:)];
showHandlesLongPress.minimumPressDuration = .5;
}
- (void)createNewSetInDB:(BOOL)doWeAddItToTheDatabase
{
UIButton *newSet = [[UIButton alloc]initWithFrame:
CGRectMake((sliderMusic.frame.origin.x + imgWhiteLine.frame.size.width / 2) + (_musicPlayer.currentPlaybackTime * 10),
6,
35,
25)];
[newSet addTarget:self action:#selector(openSet:) forControlEvents:UIControlEventTouchUpInside];
[newSet setTitleColor:[MobileMarcherVariables sharedVariableInstance].systemColor forState:UIControlStateNormal];
[newSet.layer setBorderColor:[[MobileMarcherVariables sharedVariableInstance].systemColor CGColor]];
[newSet.layer setBorderWidth:1];
[newSet.titleLabel setFont:[UIFont systemFontOfSize:15]];
[newSet setTitle:[NSString stringWithFormat:#"%i",totalNumberOfSets] forState:UIControlStateNormal];
[scrollviewMusic addSubview:newSet];
[arrayofSetButtons addObject:newSet];
[newSet addGestureRecognizer:showHandlesLongPress];
if (doWeAddItToTheDatabase) [[NSNotificationCenter defaultCenter] postNotification:newSetNotification];
}
- (void)showHandles:(UILongPressGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
NSLog(#"long press");
for (UIButton *but in arrayofSetButtons)
{
if (but.tag != gesture.view.tag)
{
but.alpha = .5;
}
}
[scrollviewMusic bringSubviewToFront:lefthandle];
[scrollviewMusic bringSubviewToFront:righthandle];
lefthandle.hidden = NO;
righthandle.hidden = NO;
lefthandle.frame = CGRectMake(gesture.view.frame.origin.x - 1,
gesture.view.frame.origin.y - gesture.view.frame.size.height,
2, 50);
righthandle.frame = CGRectMake((gesture.view.frame.origin.x - 1) + gesture.view.frame.size.width,
gesture.view.frame.origin.y - gesture.view.frame.size.height,
2, 50);
}
}
You have to assign one UILongPressGestureRecognizer per UIButton. They can all point to the same method.
- (void)createNewSetInDB:(BOOL)doWeAddItToTheDatabase
{
UIButton *newSet = [[UIButton alloc]initWithFrame:
CGRectMake((sliderMusic.frame.origin.x + imgWhiteLine.frame.size.width / 2) + (_musicPlayer.currentPlaybackTime * 10),
6,
35,
25)];
[newSet addTarget:self action:#selector(openSet:) forControlEvents:UIControlEventTouchUpInside];
[newSet setTitleColor:[MobileMarcherVariables sharedVariableInstance].systemColor forState:UIControlStateNormal];
[newSet.layer setBorderColor:[[MobileMarcherVariables sharedVariableInstance].systemColor CGColor]];
[newSet.layer setBorderWidth:1];
[newSet.titleLabel setFont:[UIFont systemFontOfSize:15]];
[newSet setTitle:[NSString stringWithFormat:#"%i",totalNumberOfSets] forState:UIControlStateNormal];
[scrollviewMusic addSubview:newSet];
[arrayofSetButtons addObject:newSet];
// Add gesture recognizer
//
UILongPressGestureRecognizer *showHandlesLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(showHandles:)];
showHandlesLongPress.minimumPressDuration = .5;
[newSet addGestureRecognizer:showHandlesLongPress];
if (doWeAddItToTheDatabase) [[NSNotificationCenter defaultCenter] postNotification:newSetNotification];
}
A gesture recognizer can only be attached to one view at a time. You're only creating one gesture recognizer. Each time you create a button, you attach the existing gesture recognizer to the new button, which removes it from the prior button.
Create a new gesture recognizer for each new button.
- (void)createNewSetInDB:(BOOL)doWeAddItToTheDatabase
{
UIButton *newSet = [[UIButton alloc]initWithFrame:
CGRectMake((sliderMusic.frame.origin.x + imgWhiteLine.frame.size.width / 2) + (_musicPlayer.currentPlaybackTime * 10),
6,
35,
25)];
[newSet addTarget:self action:#selector(openSet:) forControlEvents:UIControlEventTouchUpInside];
[newSet setTitleColor:[MobileMarcherVariables sharedVariableInstance].systemColor forState:UIControlStateNormal];
[newSet.layer setBorderColor:[[MobileMarcherVariables sharedVariableInstance].systemColor CGColor]];
[newSet.layer setBorderWidth:1];
[newSet.titleLabel setFont:[UIFont systemFontOfSize:15]];
[newSet setTitle:[NSString stringWithFormat:#"%i",totalNumberOfSets] forState:UIControlStateNormal];
[scrollviewMusic addSubview:newSet];
[arrayofSetButtons addObject:newSet];
UILongPressGestureRecognizer *showHandlesLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(showHandles:)];
showHandlesLongPress.minimumPressDuration = .5;
[newSet addGestureRecognizer:showHandlesLongPress];
if (doWeAddItToTheDatabase) [[NSNotificationCenter defaultCenter] postNotification:newSetNotification];
}
I am adding button subView to each row I click. I am trying to remove the button subView every time once I click on different row and add button subview to the other row I click.
Where would be my issue? It is adding subView to each row without removing any of them.
if (sender.tag == 212)
{
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//Debug shows that every-time myButton&myButton2 hasn't tag??
if (myButton2.tag != 0 && myButton.tag != 0)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
UITableViewCell *cell = (id)sender.superview.superview.superview;
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:commonTable.table];
NSIndexPath *indexPath1 = [commonTable.table indexPathForRowAtPoint:rootViewPoint];
itemdata1 = [itemList objectAtIndex:indexPath1.row];
if (closeManager) [closeManager release];
CGFloat __y = commonTable.y + commonTable.table.y - commonTable.table.contentOffset.y + cell.height*indexPath1.row + sender.y;
CGFloat __x = commonTable.x + commonTable.table.x + sender.x;
CGRect layerBox1 = CGRectMake(618, __y, 90, sender.height+3);
CGRect layerBox2 = CGRectMake(888, __y, 94, sender.height+3);
NSLog(#"%f and %f", __x, __y);
myButton.frame = layerBox1;
[[myButton layer] setBorderWidth:2.0f];
myButton.layer.borderColor = [UIColor redColor].CGColor;
[myButton addTarget:self action:#selector(func1:) forControlEvents:UIControlEventTouchUpInside];
myButton2.frame = layerBox2;
[[myButton2 layer] setBorderWidth:2.0f];
[myButton2 addTarget:self action:#selector(func2:) forControlEvents:UIControlEventTouchUpInside];
myButton2.layer.borderColor = [UIColor redColor].CGColor;
[self addSubview:myButton];
[self addSubview:myButton2];
myButton.tag = myButton2.tag = 666;
}
Edited for Bhumeshwer katre:
if (myButton&&myButton2)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
//Other variables
if(!myButton){
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = layerBox1;
[[myButton layer] setBorderWidth:2.0f];
myButton.layer.borderColor = [UIColor redColor].CGColor;
[myButton addTarget:self action:#selector(func1:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 666;
[self addSubview:myButton];
}
don't create each time your button like this
Here you are creating each time new instance of UIButton
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
And you are doing this
//Debug shows that every-time myButton&myButton2 hasn't tag??
if (myButton2.tag != 0 && myButton.tag != 0)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
//It will just remove new instance of UIButton and no more your UIButton will be there on UIView.
try like this
if(!myButton){
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
if(!myButton2) {
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
//Don't remove your button just change frame
if you still want to remove buttons then first remove
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
//then create new instance of button
Try adding tags to buttons before adding them to subview.
So this:
[self addSubview:myButton];
[self addSubview:myButton2];
myButton.tag = myButton2.tag = 666;
should become like this:
myButton.tag = myButton2.tag = 666;
[self addSubview:myButton];
[self addSubview:myButton2];
Regards,
hris.to
If you use tableview cell, Use this following cases,
1) Set tag to button while creating tableViewCell.
2) Hide that button by cell.yourButton.hidden = YES
3) When you click on row, just note down that indexPath as self.selectedIndexPath = indexPath in didSelectRowAtIndexPath:
4) Now reload your tableView. Just add one condition in cellForRowAtIndexpath: as below.
if (indexPath.row == self.selectedIndexPath.row)
{
cell.yourButton.hidden = NO;
}
else
cell.yourButton.hidden = YES;
for (UIView *view in self.view.subView) { // instead of self.view you can use your main view
if ([view isKindOfClass:[UIButton class]] && view.tag == ?) {
UIButton *btn = (UIButton *)view;
[btn removeFromSuperview];
}
}
i think you have to set tag your buttons first,because default tag of every UIControl is 0;
I am using UILongPressGestureRecogniser on a UIImageView in this way:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[ImageViewPhotoCard addGestureRecognizer:longPress];
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
//NSString *key = [array objectAtIndex:i];
UIButton* ButtonNote = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ButtonNote.frame = CGRectMake(100, 200, 80, 80); // position in the parent view and set the size of the button
[ButtonNote addTarget:self action:#selector(OpenNote:) forControlEvents:UIControlEventTouchUpInside];
ButtonNote.backgroundColor = [UIColor clearColor];
UIImage* btnImage = [UIImage imageNamed:#"purple_circle.png"];
[ButtonNote setBackgroundImage:btnImage forState:UIControlStateNormal];
[self.ViewA addSubview:ButtonNote];
[ArrayNotes addObject:ButtonNote];
[ButtonNote setTitle:#"" forState:UIControlStateNormal];
[ButtonNote setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
How is it possible to get the x and y coordinates of the point where the user has pressed?
CGPoint location = [gesture locationInView:self.view];
Is probably what you are looking for. This gives you a CGPoint of the touched point, according to the self.view coordinates
I am making an ipad app in which I have tableView with custom it works fine but I want that when user clicks on add button in custom cell then it should get the position x and y of that button and then set the imageView to the center of that button.
I have searced code it gets points but how to add make this for cell.addButton as cell is custom.
[cell.imageButton addTarget:self action:#selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
Here is the code for touch
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
But in this way it does not call the method which is on cell.addButton imageButtonAction.
As I understand from you , you need to set an image in a UIButton upon tap.
If you added the UITapGestureRecognizer you removed the default tap recognition and now only this would work:
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
You should remove the above code and only set this:
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
where the imageButtonAction: is :
- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
}
If you want to add an image next to the button let's say in the left of the button then the function should look like this:
- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];
}
UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];
//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:#"popupj.png"];
tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:#"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:#selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:#"popuptab1j.png"] forState:UIControlStateNormal];
[backimage addSubview:tab1];
now i am adding button to imageView but it does not get clicked
For UIButton you can use directly button function
-(IBAction) imageButtonAction:(id) sender
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
UIButton *btn = (UIButton *)sender;
//[btn setimage:[UIImage imagenamed:dimagename];
UIImageview *backimage = [[UIImageView alloc] initWithFrame:btn.frame];
[cell addSubview:backimage];
}