Doubt in Touch event

Discussion in 'iOS Programming' started by iphonejudy, May 8, 2009.

  1. macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #1
    Code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
           if ([touch view] == baseImage1)               
    	{  
    
                UIImage *anImage = [UIImage imageNamed:@"dragon1.png"]; 
          
             }
    
    }
    baseImage1 -> Imageview

    Can we specify touch event for the image with in the imageview?
     
  2. macrumors regular

    Joined:
    Apr 9, 2009
    #2
    not if you use "touch" without declaring it, but generally that works, yes
     
  3. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #3

    I will explain you the problem.

    I want to color the image,when the user touch the image.

    I am having the image ,which is having a white space around(Normally a image having a white space know.because when we save an image.It would be save with a white rectangle .(surrounded the image) know?

    My problem is the white space also considered as a image.Am i right?

    So when the user touch outside of the image (white rectangle).The image will be colored.

    Its a wrong thing.The image should be colored.when the user touch within the image.

    Any solution?
     
  4. macrumors regular

    Joined:
    Apr 9, 2009
    #4
    well, I guess the easiest solution would be to check for the position the user touched. if you know the size of the white border than you can easily determine weather the user really tapped the image or just the border. You can get the coordinated of the tap with
    locationInView:
    of UITouch. So if you border is 5px width the x coordinates of the tap need to be bigger than 5 and the y coordinates of the tap also need to be bigger than 5 - and then you are in the image.

    oh and btw: wrap you code in code-tags, make more clear what you need to know next time and for god's sake post functional code! like I said above: the code you posted will not work because "touch" is undeclared!
     
  5. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #5
    I have coordinates values in an array,I need to compare the array values with my touch coordinates

    I used the below code.


    Code:
    CGPoint location = [touch locationInView:baseImage2];
    		float x=location.x;
    		float y=location.y;
    		int *a={(10,10),(20,20)};
    		if(location = a)
    		{
    			NSLog(@"I am touch");	
    			
    		}

    But i got error.
     
  6. macrumors regular

    Joined:
    Apr 9, 2009
    #6
    1) there is an obvious error in that code, I am not gonna tell you where
    2) location is no array, it's a CGPoint. But even if you created another CGPoint, you cannot compare structures. you need to compare the x and y value seperatly.
     
  7. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #7
    sorry.


    ok.

    I thought to do onething.that is,

    Store all x and y coordinates of image in an array,

    I need to compare the touchpoint(coordinates) with the array.

    So we cant compare CGPoint with the array right?

    Anyother idea compare my coordinates with coordinates in array?
     
  8. macrumors regular

    Joined:
    Apr 9, 2009
    #8
    yeah, just don't compare the whole thing but rather compare the x and y values seperatly, like I said.

    If you need to do that often, you can write a method that does that. though I would not compare a CGPoint with an array, but rather a CGPoint with a CGPoint, it's more logical.4

    besides that, I just saw that your code says
    int *a
    int is not an object-type, you can't use pointers there. seems to me like you should really learn the basics again!
     
  9. macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #9
    Besides that

    Code:
    location = a
    
    Is an assign, not a compare.
     
  10. macrumors regular

    Joined:
    Apr 9, 2009
    #10
    sssh, that was the obvious mistake I didn't wanna tell :D
     
  11. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #11
    Hi,




    Code:
      CGRect area = CGRectMake(0, 0, testimage.size.width, testimage.size.height);
    
    I want to check the touch event for the area?

    Is it possible?
     
  12. macrumors regular

    Joined:
    Apr 9, 2009
    #12
    yes, with simple mathematic or by creating a view on that position.
     
  13. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #13
    CGRect area = CGRectMake(0, 0, testimage.size.width, testimage.size.height);

    Its a area taken from my image;

    Code:
    if ([touch view] == area)   //But we cannot assign like this know?
    {
    
    NSLog(@"I am your area");
    
    
    } 
     
  14. macrumors regular

    Joined:
    Apr 9, 2009
    #14
    ... seriously?
    of course you cannot compare a UIView with a CGRect. Where is the sense in that?

    If you really don't know this one, then start ALL the way back from the beginning and try to get the hang of the logic of programming. seems to me like you don't really know what you are doing, sorry. but this one is REALLY easy
     
  15. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #15
    I want to take particular area from my image and i want to check the touch event with that
     
  16. macrumors regular

    Joined:
    Apr 9, 2009
    #16
    I know.
    Really, I'm all for helping new programmers and stuff, but I don't see the sense in doing any kind of thinking for you. this is just ... basic logic ... . you need to be able to figure these things out for yourself if you really want to accomplish anything. that's why I am not telling you how to do it but rather tell you to do the thinking this time!
     
  17. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
  18. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #18
    I cant specify touch event to the area.

    Can u give me any idea?
     
  19. macrumors regular

    Joined:
    Apr 9, 2009
    #19
    yes. basic math. if you want to check if the touch event is in an area, check if the coordinates of the touch are within that area. it's that simple.
     
  20. thread starter macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #20

    yeah.I am trying to do this only

    see my previous thread

     
  21. macrumors regular

    Joined:
    Apr 9, 2009
    #21
    let's do this again: SEPERATLY. You CANNOT compare a CGPoint and an array, you have to compare the two coordinates SEPERATLY. SEPERATLY. SEPERATLY.

    but your syntax is still plain wrong, if you keep it like that then nothing will help ^^
     
  22. macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009

Share This Page