Reputation: 4678
I'm writing a simple card game and now I'd like to make the card clicking part of it.. All my cards are UIView objects and there is 2 UIImageView in it to show the back or the front of the cards. I want to handle the mouse click and I don't really know how to do it. Is there an interface to add? Thanks!
Upvotes: 2
Views: 7298
Reputation: 6770
@Convolution is right: use UITapGestureRecognizer to recognize a tab on an object that has no click event. This is the easiest and cleanest solution.
Subclassing in order to recognize a click only pays off when you are creating a custom object that you will be reusing a lot of the time.
Upvotes: 2
Reputation: 488
Simply set a custom button under the image. Then you just have to make the image non-touchable. This way the method you have hooked up with your button will get executed on touchevents.
Upvotes: 1
Reputation: 9913
You can make your cards buttons (perhaps not the best solution) and handle button events in your code. You could use gesture recognizers on your cards or your superview. You can make each card a subclass of UIView and override the UIResponder methods which handle touch events.
Or you can get really tricky and make the cards CALayers, and just handle UIResponder events in your parent view. This last suggestion will probably allow you the greatest flexibility in animating your cards around as the user plays your game.
Anyway I think the UIResponder methods are what you're after. UIView is a subclass of UIResponder, so you'll have to implement them in some subclass that you define.
Upvotes: 6
Reputation: 2351
Have you looked into the UIGestureRecognizer class? It's great for linking a function call to some kind of interaction. There are different type of gestures, taps, swipes, pans. It sounds like a tap gesture might help you out to change the UIImageView each time the card is tapped on.
Upvotes: 3
Reputation: 89509
Instead of using UIImageView
for displaying the cards, consider using UIButtons
(with "custom" for the button type so the only borders that are seen are the boundaries of the images). You can then set your card images to be the images for those buttons.
Upvotes: 1