UILongPressGestureRecognizer coordinates of press - ios

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

Related

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

UILongPressGestureRecognizer not working for all UIButtons

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

How to get index of uiimage subview of uiscrollview

I need to detect selected image of uiimage which is placed inside scrollview.
My code is here:
int newscrollviewY = 40;
for(int i = 0; i<1; i++)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5, newscrollviewY, 310, 100)];
scrollView.showsVerticalScrollIndicator = YES;
scrollView.scrollEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.delegate=self;
int imgx = 1;
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
[scrollView addSubview:imageView1];
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
Please help to detect selected image using imageButton or something else.
Replace your selector with buttonClicked:
be Sure that you are adding ":" after selector method. follow or replace the code below:
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
then Add the selector definantion in .m file
- (void) buttonClicked: (id) sender
{
// here you can get the tag of button.
NSInteger tag = ((UIButton *)sender).tag;
// do your implementation after this
}
Hope this will solve your issue. Enjoy Coding..!!
They way you have set it up the tag of the 'button' is already the number of the image in your array of images. So all you need to do is get the tag from the 'button'.
Make sure the button clicked function retrieves the tag from the sender(id) that you receive.
For example a bit like this:
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
UIImage selectedImages = [images objectForKey: [button tag]];
}
As par you question if you are using UIImageview you have to use UITapGestureRecognizer that same like Button. you just need to add UITapGestureRecognizer like:-
for(int img=0; img<[images count]; img++)
{
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
imageView1.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(frameTapGesture:)];
frameTapGesture.numberOfTapsRequired=1;
imageView1.image = [images objectAtIndex:img];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[imageView1 setNeedsDisplay];
[imageView1 setUserInteractionEnabled:YES];
[imageView1 setMultipleTouchEnabled:YES];
imageView1.tag=img;
[_imgView addGestureRecognizer:frameTapGesture];
[scrollView addSubview:imageView1];
}.
- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view; //cast pointer to the derived class if needed
NSLog(#"%d", view.tag);
}
Reduce your code There are No need to add Button with same Frame of ImageView For getting Click Event or you can also set Image of UIButton for you want to like use UIBUtton then remove UIImageview Code else you simply use UITapGestureRecognizer with getting click event using - (void)frameTapGesture:(UITapGestureRecognizer*)sender
Please try this code
for(int img=0; img<[images count]; img++)
{
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setImage:[UIImage imageNamed:[images objectAtIndex:i]] forState:UIControlStateNormal];
imageButton.frame = CGRectMake(imgx,0,100,100);
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
imageButton.tag = img;
[scrollView addSubview:imageButton];
imgx = imgx + 103;
}
- (void) buttonClicked: (id) sender
{
UIButton *button = (UIButton *)sender;
UIImage selectedImage = button.currentImage;
}
set button target
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// get Button Action
-(void) buttonAction:(UIButton *)sender
{
NSLog(#"%dl",sender.tag);
// do your implementation after this
}

Delete button on UIWebView

Im getting touch location coordinates from UIWebView and displays the button with coordinates. It's working with coordinates match. So, i need to increase the button. I didn't use Array for button. When i delete particular button from UIWebView it delete all the button. Shall i use NSMutableArray?. Here x & y is the touch point coordinates in float value
if(x && y){
NSLog(#"x is %f",x);
NSLog(#"y is %f",y);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(click1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:#"click" forState:UIControlStateNormal];
button1.frame = CGRectMake(x, y, 30.0, 20.0);
// [btnArray addObject:button1];
button1.tag = gTag;
gTag++;
[wbCont.scrollView addSubview:button1];
}
Delete the button:
-(void)recycle:(id)sender{
for(int i=1;i<gTag;i++){
[[wbCont.scrollView viewWithTag:i] removeFromSuperview];
}
-(void)click1:(id)sender{
UIButton *mainBtn = (UIButton *)sender;
int mainTag = mainBtn.tag;
txtview = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,568)];
txtview.font = [UIFont fontWithName:#"Helvetica" size:12];
txtview.font = [UIFont boldSystemFontOfSize:12];
txtview.backgroundColor = [UIColor whiteColor];
txtview.scrollEnabled = YES;
txtview.pagingEnabled = YES;
txtview.editable = YES;
txtview.tag = mainTag*1000;
for(int i=0; i<[textArray count];i++){
txtview.text=[textArray objectAtIndex:i];
}
[self.view addSubview:txtview];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
[txtview addSubview:button];
recyclebtn=[UIButton buttonWithType:UIButtonTypeCustom];
recyclebtn.tag = mainBtn.tag;
[recyclebtn addTarget:self action:#selector(recycle:) forControlEvents:UIControlEventTouchDown];
[recyclebtn setImage:[UIImage imageNamed:#"recycle.png"] forState:UIControlStateNormal];
recyclebtn.frame=CGRectMake(0, 10, 30, 30);
[txtview addSubview:recyclebtn];
}
-(void)recycle:(id)sender {
UIButton *btnTemp = (UIButton *)sender;
[[wbCont.scrollView viewWithTag:btnTemp.tag] removeFromSuperview];
int txtTag = btnTemp.tag*1000;
[[self.view viewWithTag: txtTag] removeFromSuperview];
}

iOS Subview with buttons not responding to touches.

I have a UIView Subclass that has a frame/image with buttons in it. I'm trying to add it to my view controller however when I do it shows up but none of the buttons will register ANY touches.
Here is my UIView Subclass.
#import "ControlView.h"
#implementation ControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect slideFrame = CGRectMake(0, 402, 320, 82);
slider = [[UIView alloc] initWithFrame:slideFrame];
slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"shelf.png"]];
[self addSubview:slider];
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btnImage = [UIImage imageNamed:#"NavBar_01.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(12, 28, 70, 50);
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn1];
[slider bringSubviewToFront:btn1];
[btn1 addTarget:self action:#selector(home) forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_02.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_02_s.png"];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(87, 28, 70, 50);
[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn2];
[slider bringSubviewToFront:btn2];
// [btn2 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_03.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_03_s.png"];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(162, 28, 70, 50);
[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn3];
[slider bringSubviewToFront:btn3];
// [btn3 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_04.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_04_s.png"];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
btn4.frame = CGRectMake(237, 28, 70, 50);
[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn4];
[slider bringSubviewToFront:btn4];
// [btn4 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:recognizer];
}
return self;
}
-(void)slideUp
{
// Slide up based on y axis
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 320;
slider.frame = frame;
[UIView commitAnimations];
}
-(void)slideDown
{
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 425;
slider.frame = frame;
[UIView commitAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And this is how I'm adding it to my ViewController.
ControlView *cont = [[ControlView alloc]init];
[homeView.view addSubview:cont];
I tried adding the same code to a ViewController and the buttons etc.. worked so I'm sure this is something simple/stupid with Delegates/Self etc.. between the subview and the ViewController however I have a mental block.
You are doing this?
ControlView *cont = [[ControlView alloc]init];
you are supposed to do
ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];

Resources