PDA

View Full Version : Touches on an image




macfanboy
Jul 30, 2008, 11:37 AM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

Can you specify an action if a touch is on an image? I have randomly appearing images and I can't figure it out. Btw if you didn't realize it by now, I'm not a master at objective-c. Thanks for taking the time to help a noob!



Jeremy1026
Jul 30, 2008, 11:41 AM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

Can you specify an action if a touch is on an image? I have randomly appearing images and I can't figure it out. Btw if you didn't realize it by now, I'm not a master at objective-c. Thanks for taking the time to help a noob!

You could always have a button display at a random location, with the image as the button background.

macfanboy
Jul 30, 2008, 11:48 AM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

wouldn't it be easier just to use an image and use this?
if (UITouch *is on the image*)
I know that's not the code but it seems easier. I guess I'm just asking the code to see if a touch is on the image

macfanboy
Jul 30, 2008, 02:58 PM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

anything?

ghayenga
Jul 30, 2008, 07:55 PM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

anything?

Yes, just enable user interaction for your UIImageView, then implement the touchesBegan method on it's containing view and it will get called and you can check to see if the touch was within the bounds of your UIImageView. If so, then you do something.

macfanboy
Jul 30, 2008, 07:59 PM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20)

Wow I'm so stupid. Thank you for helping me realize I couldve used the method on the imageview itself. That makes me feel stupid

caldwelljason
Aug 1, 2008, 03:10 PM
I am using a UIImageView-derived class and want to handle the touch event in the class, itself for better encapsulation.

I added the touchesBegan method on my class, but it never gets called. I am setting the userInteractionEnabled to YES on the object.

The touch event seems to pass to the UITableView that contains my control. So I end up with a cell selection, instead of an event in my control.

Anything else I should do to get touch events on my control?

macfanboy
Aug 1, 2008, 03:25 PM
i found somethin new:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([imageView frame], [touch locationInView:self])) {
[self YOURMETHODHERE];
}


this looks like it works, although i havent tried it

rowlandd
Aug 11, 2008, 05:19 PM
[QUOTE=caldwelljason;5955297]I am using a UIImageView-derived class and want to handle the touch event in the class, itself for better encapsulation.

I added the touchesBegan method on my class, but it never gets called. I am setting the userInteractionEnabled to YES on the object.

.....

I have this precise problem. Did you find a solution?

caldwelljason
Aug 12, 2008, 12:06 PM
I just put a button on top of my UIImageView. It is the save size and the image, and has no text or image. Now, when there is a touch event on the image, the button handles it.

I don't know if that is the best way to do it, but it works.

rowlandd
Aug 12, 2008, 01:43 PM
I just put a button on top of my UIImageView. It is the save size and the image, and has no text or image. Now, when there is a touch event on the image, the button handles it.

I don't know if that is the best way to do it, but it works.

That strikes me as an acceptable solution, but my problem is a bit different. I want to create a slider-like custom control. It would operate vertically and have its own graphics. I've written all the pieces, but I can't activate it.

The answer is probably in understanding how the "first responder" works, but the Apple documentation is vague about that.

caldwelljason
Aug 13, 2008, 09:37 AM
Mine was a custom control, as well.

Unfortunately, I abandoned it as my app evolved, so I no longer have the source for the button.

I can't recall if I derived from UIButton and added the UIImageView from within the button or if I derived from UIImageView and added the button from within the UIImageView.

I think I derived from UIButton and added the UIImageView from within the button. So from the control-consumer's perspective, it's just a button. But when it is clicked on, I trigger the animation from the UIImageView before passing the click event up to the containing view.

I probably had the same goal as you: a single, reusable control that acted like a button, but allowed by to animate between states on a touch event. :)

rowlandd
Aug 16, 2008, 06:54 PM
I think I derived from UIButton and added the UIImageView from within the button. So from the control-consumer's perspective, it's just a button. But when it is clicked on, I trigger the animation from the UIImageView before passing the click event up to the containing view.

I probably had the same goal as you: a single, reusable control that acted like a button, but allowed by to animate between states on a touch event. :)[/QUOTE]

Tried that -- inherit from UIButton, give it the size and image I want, write my own "touchesBegan" etc. and it worked! I even reset the image at points to give it a pseudo animation. Just what I wanted.