Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macfanboy

macrumors 6502a
Original poster
Jun 5, 2007
916
163
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!
 
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.
 
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?
Code:
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
 
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?
 
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.
 
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
 
UIImageView derived class

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?
 
i found somethin new:
Code:
- (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
 
solution???

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?
 
Use a button

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.
 
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.
 
Custom control

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. :)
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.