开发者

How do I access only one of my touches in my touches began event

开发者 https://www.devze.com 2022-12-14 21:20 出处:网络
I have: UITouch *touch = [touches anyObject]; if ([touches count] == 2) { //preforming actions } What I wa开发者_StackOverflow社区nt to do Is ask it inside of the ifstatement where the two touches

I have:

UITouch *touch = [touches anyObject];

    if ([touches count] == 2) {
        //preforming actions                                                             
    }

What I wa开发者_StackOverflow社区nt to do Is ask it inside of the ifstatement where the two touches are seperatly.


You can iterate over the touches:

if([touches count] == 2) {
    for(UITouch *aTouch in touches) {
        // Do something with each individual touch (e.g. find its location)
    }
}

Edit: if you want to, say, find the distance between the two touches, and you know there are exactly two, you can grab each separately then do some math. Example:

float distance;
if([touches count] == 2) {
    // Order touches so they're accessible separately
    NSMutableArray *touchesArray = [[[NSMutableArray alloc] 
                                     initWithCapacity:2] autorelease];
    for(UITouch *aTouch in touches) {
        [touchesArray addObject:aTouch];
    }
    UITouch *firstTouch = [touchesArray objectAtIndex:0];
    UITouch *secondTouch = [touchesArray objectAtIndex:1];

    // Do math
    CGPoint firstPoint = [firstTouch locationInView:[firstTouch view]];
    CGPoint secondPoint = [secondTouch locationInView:[secondTouch view]];
    distance = sqrtf((firstPoint.x - secondPoint.x) * 
                     (firstPoint.x - secondPoint.x) + 
                     (firstPoint.y - secondPoint.y) * 
                     (firstPoint.y - secondPoint.y));
}


Touches is already an array. There's no need to copy them into another array -- just use [touches objectAtIndex:n] to access touch n.

0

精彩评论

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

关注公众号