I want to know the UIGestureRecognizer working or not in Objective-C++ because i've implemented this one but tap method never calling. So please let me know is it possible or not in Objective-C++.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imgView.image = [UIImage imageNamed:#"dharm"];
[self.view addSubview:imgView];
imgView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
tapRecognizer.numberOfTapsRequired = 1;
[tapRecognizer setDelegate:self];
[imgView addGestureRecognizer:tapRecognizer];
}
- (void)tap:(id)sender {
NSLog(#"Tap Pressed");
}
Try adding [imgView setUserInteractionEnabled:YES];
Related
I have the following code:
m_singleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, nWidth - 1, nHeight - 1)];
m_singleView.backgroundColor = [UIColor clearColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[m_MainView addSubview:m_singleView];
The issue that I'm having is that the pinch event for some reason does not fire. However, if i change line from [m_singleView addGestureRecognizer:pinchGesture] to [m_MainView addGestureRecognizer:pinchGesture]; then everything will work fine... can I not add the event for subview only?
Thanks!
Yes,you can add gesture to subview. i tested your code like below works fine.
First add delegate.
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
UIView *m_singleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 50, self.view.frame.size.height - 50)];
self.view.backgroundColor=[UIColor greenColor];
m_singleView.backgroundColor = [UIColor redColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch)];
pinchGesture.delegate=self;
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[self.view addSubview:m_singleView];
}
-(void)pinch{
NSLog(#"In PInch");
}
you have used pinchgesture so you can use it like below.
Use Debug view hierarchy to see whether the size of m_singleView are 0 or not? If so, the size of m_singleView needs to be changed until you can touch it.
I have unknown amount of UIImageViews which are created in the code, not in the xib file and i need to handle the taps on those images. The handling of each of this image view is going to be the same. How do i do this?
Demo code for your implemantation
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView1 setUserInteractionEnabled:YES];
[imgView1 addGestureRecognizer:singleTap];
imgView1.tag = 1;
imgView1.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView1];
UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView2 setUserInteractionEnabled:YES];
[imgView2 addGestureRecognizer:singleTap2];
imgView2.tag = 2;
imgView2.backgroundColor = [UIColor blueColor];
[self.view addSubview:imgView2];
}
-(void)tapDetected:(UITapGestureRecognizer *)gestureRecognizer{
UIImageView *myImg = (UIImageView*)gestureRecognizer.view;
NSLog(#"tag : %ld",(long)myImg.tag);
}
Try this code to add a gesture recogniser
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(action:)];
[_imgview addGestureRecognizer:tap];
_imgview.userInteractionEnabled = YES;
You can add UITapGestureRecognizer to detect touch on UIImageView.
Just use below method with argument, your image view and a unique tag, and you're done !
- (void) setTapGestureOnImageView:(UIImageView *)imageView withTag:(NSInteger)tag {
//this is important, by default user interaction isn't enabled, we have to enable it.
imageView.userInteractionEnabled = YES;
//create a tap gesture (in example this is sinlge tap) with target and action to call when user tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(detectTap:)];
//you can customize taps too
//tap.numberOfTapsRequired = 2;
//tap.numberOfTouchesRequired = 2;
//add gesture on image view
[imageView addGestureRecognizer:tap];
}
- (void)detectTap:(UIGestureRecognizer *)recognizer {
//Get the tapped image view from recognizer
UIImageView *imageView = (UIImageView *)recognizer.view;
//Check for condition, which image view tapped
if(imageView.tag == 1) {
//do something 1st imageview
}
else if(imageView.tag == 2) {
//do something for 2nd imageview
}
else {
//do something else
}
}
May be this will be useful to you
- (void)viewDidLoad
{
NSArray *imagesArray = [NSArray arrayWithObjects:#"statement_card_1.png", #"statement_card_2.png", #"statement_card_3.png", #"statement_card_4.png", #"statement_card_5.png", nil];
short xPadding = 10;
for (int i = 0; i< imagesArray.count; i++)
{
UIImage *imageRecipe =[UIImage imageNamed:[imagesArray objectAtIndex:i]];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPadding, xPadding+(i*60),imageRecipe.size.width, imageRecipe.size.height)];
imgView.tag =IMAGETAG + i;
[imgView setImage:imageRecipe];
imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
[imgView addGestureRecognizer:singleTap];
[self.view addSubview:imgView];
}
}
-(void)tapDetected:(UITapGestureRecognizer *)recognizer
{
UIImageView *TempImg = (UIImageView *)recognizer.view;
NSLog(#"tag : %ld",(long)TempImg.tag);
}
I know that when the error is "'blah' was not declared in this scope" means when the object was not created correctly, but when I am making a Notification Center widget for iOS, using the WeeLoader template and THEOS to compile, I am getting this error: 'UITapGestureRecognizer' was not declared in this scope.
Here is my .mm file:
- (void)loadFullView {
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/WeeAppTest.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 316, 71);
bgView.userInteractionEnabled = YES;
[bgView addGestureRecognizer:Tap];
[_view addSubview:bgView];
[bgView release];
[Tap release];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 316, 71)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.text = #"Hello world";
lbl.textAlignment = UITextAlignmentCenter;
[_view addSubview:lbl];
[lbl release];
}
-(void) handleTapGesture:(UIGestureRecognizer *) sender {
}
What am I doing wrong? I am pretty sure that I am declaring everything correctly, by the way, the code does work with out the UITapGestureRecognizer.
Thank you.
I think there are a few things going on here.
I think you could be getting the out of scope error because the gesture recognizer should be declared in the viewDidLoad or the init methods.
Also, in the UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)]; You call your method handleSingleTap:
Therefore your method should be -(void) handleSingleTap:(UITapGestureRecognizer *) sender
I have a horizontal scrollview filled with UIImageViews.
I want to detect a tap on the UIImageView and have its background color changed.
Somehow the tap gesture is not working or so.
However, when I add a tap gesture to the scrollview, it works. The scrollview.background color can be changed.
But I want to detect a tap on the UIImageViews it contains!
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor orangeColor];
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)];
scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height);
for (int aantal=0; aantal < 6; aantal++) {
UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)];
item.backgroundColor = [UIColor yellowColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
tap.numberOfTapsRequired = 1;
tap.cancelsTouchesInView=YES;
item.userInteractionEnabled = YES;
[item addGestureRecognizer:tap];
[contentOfScrollView addSubview:item];
}
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
//[scrollView addGestureRecognizer:tap];
scrollView.userInteractionEnabled=YES;
scrollView.delaysContentTouches=NO;
[scrollView addSubview:contentOfScrollView];
[self.view addSubview:scrollView];
And this is the imageTapped function.
-(void)imageTapped:(UITapGestureRecognizer *)gesture
{
NSLog(#"tapped!");
gesture.view.backgroundColor = [UIColor whiteColor];
}
User interaction is set to NO by default for UIImageView, so you need to set it to YES.
You set it to yes for "item", but not for contentOfScrollView.
Your error is here:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
You need to change the target to "self" instead of "item", then it won't crash.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
I'm stuck at how to declare the uibutton in uiview:
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"Added holderView.");
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
removeSticker.frame = CGRectMake(0, 0, 100, 100);
[removeSticker setImage:[UIImage imageNamed:#"cancel-disabled.png"] forState:UIControlStateNormal];
[removeSticker addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[holderView addSubview: removeSticker];
[removeSticker setHidden: YES];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressRecognizer setDelegate:self];
[holderView addGestureRecognizer:longPressRecognizer];
[stickerView addSubview:holderView];
}
- (void)buttonClicked:(id)sender {
UIView *holderView = [(UIButton *)sender superview]; [holderView removeFromSuperview];
}
Here I want to make the uibutton *removeSticker visible to be able to do buttonClicked, however it said undeclared identifier *holderView:
-(void)longPress:(id)sender {
for(UIButton *removeSticker in holderView.subviews)[removeSticker setHidden: NO];
}
How should I write this line? Please help, thanks a lot.
Just this
-(void)longPress:(id)sender {
[removeSticker setHidden:NO];
}
The reason you get that error is that the scope for holderView is limited to the block it is declared in. For future reference, if you want it to have scope across your entire class, it should be an ivar or property.
You also want to make removeSticker an ivar for the same reason.
To make it an ivar, put the declaration in the header file:
UIButton *removeSticker;
Then in your viewWillAppear code you change this line
UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
to this
removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
But barley has it right in his answer, too. sender is already pointing to the removeSticker object, whether you declare it as an ivar or not.
Try this.
-(void)longPress:(id)sender {
[(UIButton *)sender setHidden:NO];
}
In your original code, you are referencing a variable in different scope (i.e., holderView). Thats why the error.