Skip to main content

How to detect particular UIImageView for UITouch Events



I've added 5 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.

}



NSLog(@"%@",[touches anyObject]); //It returns output like below,



/*<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 my UIView tag







I google it but doesn't come with solution!





Thank you in advance for any help !


Comments

  1. 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");
    }

    ReplyDelete
  2. 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!

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.