Touch event on uimageview added as subview to parent view - iphone

I have added the view programmatically in the main view. And in the parent view i am adding imageview as a subview programmatically. Now I have set the touch event for imageview on click but its not working. It is not detecting the touch event on click. I am using the following code of touch event of imageview.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == originalphoto)
{
[fullview setHidden:NO];
[self.view addSubview:fullview];
[setimage setImage:image];
}
}
Do anyone has idea that how to set touch event of imageview which has been added programmatically?
Thanks alot.

Why dont you use a gesture? it is lot easier. You could use the UITapGestureRecognizer and let it work for you: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITapGestureRecognizer_Class/Reference/Reference.html

//Add gesture to your view
UITapGestureRecognizer *Tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ResignResponder:)];
[tempV addGestureRecognizer:Tap];
[Tap release];
//method will be called on tapping the view
-(void)ResignResponder:(UIGestureRecognizer*)recognizer {
[LocSearchField resignFirstResponder];
[self.view sendSubviewToBack:tempV];
}

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:UIIMAGEVIEW];// uiimageview object
touchx = location.x;
touchy = location.y;
NSLog(#" x= %i ", touchx);
NSLog(#"y = %i",touchy);

Maybe you need to set property imageView.userInteractionEnabled = YES?

Related

Detect touch of UIImage added as subview of UIScrollView

I have UIScrollView with many UIImageViews.I need to drag and drop image from UIScrollView to another view. Outside the scrollView touch is worked. But inside scroll view touch is not worked. I used touchesBegan,touchesMoved etc method. Please help me.
-(IBAction)selectBut:(id)sender
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x,y,w,h)];
scrollView.userInteractionEnabled = YES;
int y = 0;
for (int i = 0; i < [myArray count]; i++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
image.userInteractionEnabled = YES;
y=y+35;
[scrollView addSubview:image];
}
[self.view addSubview:scrollView];
[scrollView setContentSize:CGSizeMake(150, 300)]
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1) {
NSLog(#"One touch !!");
}
}
you need to customize the UIImageView with your own view inherited from UIImageView. Provide touch methods in that customized subclass and add it on your UIScrollView..
Subviews of UIScrollview will never call touchesBegan method directly. You need to customized with subview to get touchesBegan properties of that added subview/customized view.
I mean to say take subclass of ImageView like
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];
CustomImageView class is should be inherited from UIImageView like
#interface CustomImageView : UIImageView
{
}
in # .m File
#import "CustomImageView.h"
#implementation CustomImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"%s", __FUNCTION__);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImageView.image = nil;
return;
}
}
Add a Tap gesture recognizer to the scroll view for enabling touch inside scroll view:
UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(methodName:)];
singlTap.cancelsTouchesInView = NO; //Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
[scrollViewName addGestureRecognizer:singlTap];
then write your code in specified method.
-(void)methodName:(UITapGestureRecognizer *)gesture
{
//your code here
}
I don't know if this help you. But try to set the exclusiveTouch property of the UIImageView to YES.
Did you add your scrollview as an IBOutlet? If its from xib and as you say touches are available outside the scroll and not within, I guess you might have accidentally unchecked userInteractionEnabled for the scrollview which inhibits touch events. And you have added UIImageViews as subviews to UIScrollView. For UIImageView you have to first set userInteractionEnabled to YES and then add any gestures if necessary to get events or use the touchesBegan, touchesMoved methods. Unless you set interaction enabled you wont receive touch events on UIImageView. If the parent view I mean the UIScrollView's interaction is disabled you wont get touches on UIImageView too. Hope this helps :)

touchesMoved reaching out of my view bounds

I have subclassed UIView and there initially my view will be in a default color and i need to fill some different color on touch (from x axis = 0 to user touched point),here the problem is touchesMoved even if i drag out of my self view bounds it is getting those points,how to restrict it to only for my self view bounds.
I googled & tried below snippets but of no luck
if([self pointInside:point withEvent:nil]){
[self fillColor];
}
My touchesMoved method is as below,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
endPoint = point;
NSLog(#"moved x: %f,y: %f",point.x,point.y);
if(CGRectContainsPoint([self frame], endPoint)){ // this also not working
[self fillColor];
}
}
Any help is appreciated in advance.
just set tag in viewDidLoad: method and use bellow logic..
fillColorView.tag = 111;
and use bellow logic in touchesMoved: method like bellow..
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tap = [touches anyObject];
CGPoint pointToMove = [tap locationInView:fillColorView];
if([tap.view isKindOfClass:[UIView class]])
{
UIView *tempView=(UIView *) tap.view;
if (tempView.tag == 111){
[self fillColor];
}
}
}
hope this help you...
In your touchesMoved method, CGPoint point = [touch locationInView:self]; replcae self by the view in which you wants the touch to be worked.
self will get the complete view, you should pass your drawingView at there, so that it will detetc touch only on that view.

iOS : detect UIImageView for UITouch Events

I've added some UIImageView dynamically and filled it with different images, what I am trying to do is "Allow user to set position for any UIImageView", for that I used
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Here I want the object of particular UIImageView on which user touched.
}
In that method I'm doing,
NSLog(#"%#",[touches anyObject]);
It returns output
<UITouch: 0x68b95e0> phase: Began tap count: 1 window: <UIWindow: 0x68875d0; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x68b6470>> view: <UIImageView: 0x6a74cf0; frame = (83.7763 83.7763; 182.447 182.447); transform = [0.968912, -0.247404, 0.247404, 0.968912, 0, 0]; alpha = 0.8; opaque = NO; tag = 3; layer = <CALayer: 0x6a74980>> location in window: {161, 230} previous location in window: {161, 230} location in view: {52.7761, 105.448} previous location in view: {52.7761, 105.448}
Note, in above output, it showing my UIImageView object on which I touched. But I want that object from it!!!
I want my UIImageView on which user touched?, I have already set property userInteractionEnabled=YES so the problem isn't with it!
I used below code to get it so, but it wont work.
NSInteger tag=[[[touches anyObject] view] tag]; //It only returns tag of UIView tag
I Google it but doesn't come with solution!
Thank you in advance for any help!
Here you go:
this is only for one imageview you can detect the other by the same if statement.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touch_point = [touch locationInView:self.view];
if (![imageView pointInside:touch_point withEvent:event])
{
NSLog(#"point inside imageview");
}
}
or you can also do this :p
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch.view == iv)
{
NSLog(#"i got you");
}
}
like this: (iv and iv2 are 2 different UIImageView`s)
if (touch.view == iv)
{
NSLog(#"i got you");
}
if (touch.view == iv2)
{
NSLog(#"i got you too :p");
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
if([[touch valueForKey:#"view"] isKindOfClass:[UIImageView class]])
{
UIImageView *viewSelected=(UIImageView *)[touch valueForKey:#"view"]; //it returns touched object
//for further differences can user viewSelected.tag
}
}
Code to get only the X and Y coords from the UIImageView:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touch_point = [touch locationInView:self.imgView];
if ([imgView pointInside:touch_point withEvent:event])
{
NSLog(#"point inside imageview");
cords=[NSString stringWithFormat:#"%f,%f",touch_point.x,touch_point.y];
NSLog(#"cords are%#",cords);
}
}
You can use UIButton with Image properties instead of UIImageView.
If you would like to call your own function ,It's pretty easy to handle many event like Touch up inside or Touch Cancel by adding selector.
UIButton *yourBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
/* Depend on your dynamic programming handle */
yourBtn.frame = CGRectMake(40, 140, 240, 30);
/* If you prefer just only image ,no need to set Title name */
[yourBtn setTitle:#"Your Button Title" forState:UIControlStateNormal];
/* choose yourFunction for each UIButton */
[yourBtn addTarget:self action:#selector(yourFunction) forControlEvents:UIControlEventTouchUpInside];
/* your button will be appear in the position that you have define */
[self.view addSubview:yourBtn];
Hope It helps you!
1 Subclass UIImageView and implement:
Responding to Touch Events
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
Responding to Motion Events
– motionBegan:withEvent:
– motionEnded:withEvent:
– motionCancelled:withEvent:
or:
2 Add UIGestureRecognizer to each UIImageView.
You could add the View that you add into an NSMutableArray and then just compare like this:
I am not in my mac, but It's something similar to this:
NSInteger viewID = [_views indexOfObject:[[touches anyObject] view]];
This return and number if not isn't exist do this:
if (viewID != NSNotFound) {
//it exist the view and its in the array
}

Touchesbegan not detecting what is being touched

I'm building a rotating banner using a NSTimer to keep track of the current image with the image being animated from 5 different images. I have a touchesBegan set up to keep handle the touch event on the banner if someone clicks it. My proof-of-concept works, but moving it into another project, it breaks.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == myImageView){
[self getImage];
NSLog(#"%#", currentImage);
}
}
Now when I put break points into my project, it grabs the touch just fine, but when it gets to the
if ([touch view] == myImageView)
it doesn't detect that the image view is being touched.
Not sure what would cause that but have you tried using a UIGestureRecognizer? Try something like the code below and see if the method gets called.
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageSelected)];
tapped.numberOfTapsRequired = 1;
[theImageView addGestureRecognizer:tapped];
//Memory Cleanup
[tapped release];
-(void)imageSelected
{
NSLog(#"Selected an Image");
}
First of all you have to set userInteractionEnabled to YES in your viewDidLoad method like below:
[myImageView setUserInteractionEnabled:YES];
Note that for the myImageView, checking User Interaction Enabled via Identity Inspector didn't work for me.
Then change
UITouch *touch = [[event allTouches] anyObject];
to
UITouch *touch = [touches anyObject];
so that the touchesBegan method looks like below:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == myImageView) {
// place any code here when myImageView is touched
}
}

Issues in handling touches on subviews(uiviews) in UIScrollView

I have craeted a UIScrollView using code and i have created subviews which i am creating dynamically from the DB that are added to the array of views and to the UIScrollviews.I am using touchesBegan and touchesMoved methods.The subview which i am selecting is not recognized,but the control comes into touchesBegan and touches moved method.I have posted the code below.Please anybody help me to overcome from this issue.
I have created the scrollview through code and i have subviews in an array named "arrayofviews".these subviews are the same views which are in the array.I am able to move these subviews which are from the DB on a normal view,but when i added these views to the scrollview its not working.
I have tried so many solutions which are in the net,but i couldn't get a proper solution.
.m file
- (void)viewDidAppear:(BOOL)animated
{
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 374)];
scrollview.backgroundColor=[UIColor clearColor];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(300, 600)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.delaysContentTouches=NO;
.
.
.
.
//here i am retrieving the controls from the DB and adding into the "arrayofviews" array which is an NSMutableArray
//I have added subviews in this part to the scroll like
[scrollview addSubview:vw1];
[scrollview addSubview:vw2];
.
.
.
scrollview.userInteractionEnabled=NO;
scrollview.scrollEnabled=FALSE;
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
scrollview.userInteractionEnabled=YES;
[self.nextResponder touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation;
touchLocation.x=scrollview.contentOffset.x;
touchLocation.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
vw.userInteractionEnabled=YES;
if([touch view]==vw)
{
vw.center=touchLocation;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //method to intiate the touch events
{
[self.nextResponder touchesMoved:touches withEvent:event];
UITouch *touch = [[event touchesForView:scrollview] anyObject];
CGPoint touchLocation1;
touchLocation1.x=scrollview.contentOffset.x;
touchLocation1.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
if([touch view]==vw)
{
vw.center=touchLocation1; //statement to move the control
}
}
}
change the line
scrollview.userInteractionEnabled=NO;
to
scrollview.userInteractionEnabled=YES;
reason
userInteractionEnabled tells the device, whether to accept touch events. As you disabled the touch on scrollView then how it could recived touches events...
cheers!!!