开发者

How to flick through a deck of cards?

开发者 https://www.devze.com 2023-01-12 03:36 出处:网络
I\'m making an iPhone cards game where each use has a deck of cards, I\'d like to give the user the option to drag his finger through the cards and each card get\'s highlighted while his finger is on

I'm making an iPhone cards game where each use has a deck of cards, I'd like to give the user the option to drag his finger through the cards and each card get's highlighted while his finger is on it. This effect is already done in Uno for iPhone. My cards are put into UIButton, what i tried to do is to set a small image to the button in normal sta开发者_JAVA技巧te and a bigger image in the highlighted state, it did achieve the effect i was looking for but, the user has to highlight each card individually to be able to see the bigger picture.

Here is the code i used to set the normal and highlighted state of the UIButton:

//player413 is an IBOutlet to a UIButton, and img,imgHigh are UIImages
[player413 setImage:img forState:UIControlStateNormal] ;
[player413 setImage:imgHigh forState:UIControlStateHighlighted] ;

Any guidelines ?


Use a single view to handle all the touch interaction.

EDIT: Oh, all right.

When a view receives a touch hitTest:withEvent: is called recursively, until a view which "accepts" the touch is found.

Once hitTest:withEvent: returns a non-nil value, it's over (by default); that view "owns" the touch (see UITouch.view). Only that view gets touchesBegan/Moved/Ended/Cancelled:withEvent: callbacks.

If you want the touch to affect any card in the deck, the deck should implement touches*:withEvent:, and either set userInteractionEnabled=NO on subviews, or override hitTest:withEvent: so it returns "self" instead of one of the "cards".

Then, in touches*:withEvent:, detect which "card" the touch is on, and then do card.highlighted = YES. If you have multipleTouchEnabled=NO, you can assume that there's only one touch and use UITouch * touch = [touches anyObject].

(There are a handful of UIKit classes which somehow sit between the touch and its owning view: UIScrollView can intercept the touch and scroll instead; gesture recognizers cancel touches when they detect a gesure.)

0

精彩评论

暂无评论...
验证码 换一张
取 消