I have a UIScrollView .In that i have one ImageView.
I want to get UITOUCH point .
i know i can get location by
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}
But i want uitouch on image.
PLease help me.
try like this,
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: tapRecognizer.view]
}
AND set image view userInteractionEnabled to YES
UIImageView *image = [[UIImageView alloc]init];
image.frame = CGRectMake(55, 30, 50, 40);
image.image = [UIImage imageNamed:#"Default.png"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
singleTap.numberOfTapsRequired = 1;
self.view.tag =10;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:singleTap];
Related
Hello fellow programmers.,
I want to create dynamic UIView of size 100X100 around my double click on Main UIView (Everytime I double click diffrant place). I totally don't have any idea about it. So can't provide any code for it. Anyone can help??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];
NSLog(#"Point in myView: (%f,%f)", currentPos.x, currentPos.y);
UIView*hover = [[UIView alloc] initWithFrame:CGRectMake(currentPos.x - 50, currentPos.y - 50 , 100, 100)];
hover.backgroundColor = [UIColor grayColor];
[self.view addSubview:hover];
}
try this code
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addDynamicView:)];
self.view.userInteractionEnabled = TRUE;
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
adding the custom view
- (void)addDynamicView:(UITapGestureRecognizer*)aGesture{
/** add view with specified frame **/
CGRect rect = CGRectMake(10, 10, 100, 100);
UIView *lView = [[UIView alloc] initWithFrame:rect];
lView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lView];
}
Hey guys I'm trying to let users rotate and scale the size of UIImageView that I add programmatically but for some reason my gesture recognizers aren't working. To add the UIImageView I use a UITapGesture which I set up through the storyboard which works fine. Here's my code:
-(IBAction)addUIImageView:(UITapGestureRecognizer *)sender {
CGPoint tapLocation = [sender locationInView:_Image];
NSLog(#"Screen tapped");
UIImageView *ImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Image1.png"]];
[ImageView setCenter:[sender locationInView:_Image]];
ImageView.userInteractionEnabled = YES;
ImageView.multipleTouchEnabled = YES;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(resizeImage:)];
[ImageView addGestureRecognizer:pinchGesture];
UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotateImage:)];
[ImageView addGestureRecognizer:rotateGesture];
[self.Image addSubview:ImageView];
}
and then for the pinch and rotate gestures I have:
- (void)resizeImage:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
and
-(void)rotateImage:(UIRotationGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}
anyone know what's wrong and why my gestures won't work?
UIImageView was too small to properly pinch and rotate on simulator.
This is really basic stuff, but I can't seem to get it right (I'm new to programming). What I'm trying to do is, have my Xcode 5 App detect if an image with a certain name (say for ex. #"go.png" is pressed or touched. How can I do this? There are only two buttons, so I've been doing it with the (touchLocation.y and .x ...) method, but I need to have the Button being pressed method now. I have pasted the code below. I really appreciate your help everyone.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
switch (_flyGo) {
case HeadMenu:
if (touchLocation.y < self.size.height * 0.7) {
} else if (touchLocation.x < self.size.width * 0.3) {
[self switchNewFly:FlyTutorial];
} else {
[self giveRatingToApp];
}
break;
case FlyTutorial:
[self switchToFly];
break;
case FlyStatePlay:
[self flyPlayer];
break;
case FlyDisplayFalling:
break;
case FlyDisplayScore:
break;
case FlyDisplayDone:
if (touchLocation.x < self.size.width * 0.6) {
[self switchNewFly:FlyDisplayTutorial];
} else {
[self ShareMyScore];
}
break;
}
//I would like to get an image for ex. "flyer.png" to be detected as a touch instead of using (touchLocation.x or .y < self.size.width * 0.6)
Thanks!!!!
Yes, you can
Add touch recogniser to UIImageView (don't forget to set imgView.userInteractionEnabled = YES;
). Also set tag property for imgView to some value.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapAll:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[imgView addGestureRecognizer:tapRecognizer];
Then
- (void)handleTapAll:(UITapGestureRecognizer *)recognizer {
UIImageView *img = (UIImageView *)recognizer.view;
if(img.tag == some_vale) //your code
}
ViewController.h
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
......
ViewController.m
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:#"yourimage.png"];
imageView.userInteractionEnabled = YES;
imageView.exclusiveTouch = YES;
imageView.multipleTouchEnabled = YES;
// Add tap gesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tap setNumberOfTapsRequired:1];
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[self.view addSubview:imageView];
- (IBAction)handleTap:(UIGestureRecognizer *)recognizer { //Your code here }
I have trouble with creating list of ImageViews with tap gesture. When I create gesture, selector function is not called. When I create only one imageView function is called. Any idea why? It is all subview of a large scroll view.
This is my code:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[myScroll addSubview:clickForDetail];
tag++;
}
}
and selector function:
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(#"single Tap on imageview");
}
Is it possible somehow to get tag of a ImageView that is clicked?
You need to put different tap gesture for each view object
for(int k=0;k<MyList.count;k++){
for(int i=0;i<listSize;i++){
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
UIImageView *clickForDetail =[[UIImageView alloc]initWithFrame:CGRectMake(i*HELLO_WIDTH,k*LIST_ROW_HEIGH ,HELLO_WIDTH, LIST_ROW_HEIGH)];
clickForDetail.backgroundColor = [UIColor clearColor];
clickForDetail.tag = tag;
clickForDetail.userInteractionEnabled = YES;
[clickForDetail addGestureRecognizer:singleTap];
[epgScroll addSubview:clickForDetail];
tag++;
}
}
-(void)imageTaped: (UITapGestureRecognizer *)recognizer
{
NSLog(#"single Tap on imageview");
UIImageView *selectedTextView = (UIImageView *)recognizer.view;
}
I have a UIView and a tap gesture recognizer in it:
UIImageView *tabView = [[UIImageView alloc] initWithFrame:CGRectMake(41, 145, 702, 100)];
tabView.image = [UIImage imageNamed:#"inactive_tab"];
tabView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[tabView addGestureRecognizer:singleFingerTap];
[self.scrollView addSubview:tabView];
And I add another view on scrollview:
[self.scrollView addSubview:self.activeTab];
activeTab is over the inactiveTap. When I tap to activeTap, gesture recognizer fires, whis I dont want to be happened. How can I avoid this?
Use UIGestureRecognizerDelegate and its method gestureRecognizer:shouldReceiveTouch:.
You can check if the touch point is inside a view frame and return NO if you don't want the touch to happen on that view.`
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint touchLocation = [touch locationInView:self.view];
return !CGRectContainsPoint(self.activeTab.frame, touchLocation);
}