On Dec 8, 2009, at 8:59 am, Chunk 1978 wrote:

> this is what i have so far, but i have no idea how to write the
> touchesEnded method so that the appropriate label will clear.  so if 3
> fingers are touching the screen and displaying their different
> coordinates, if the 2nd finger that was pressed ends its touch, the
> 2nd label will clear ("Touch 2: {0, 0}") while the 1st and 3rd
> continue to track.  clearly i'm lost, i apologize.
> [...]
> - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
>        {
>        for (UITouch *touch in touches)
>                [touchesArray addObject:touch];
> 
Touches are delivered in mathematical sets; there is no ordering.  It's not 
quite clear what you're trying to achieve by adding the touches to the array, 
but it won't preserve ordering for you over the long term.  A user may make one 
touch, then put down another finger, lift the first, then put down a "third" 
whilst the second is still on screen.  Is that now touch 1 or touch 3?  (UIKit 
happens to recycle touch objects, but it's not clear if that's the same 
semantic you want.)

(As an aside, note also NSSet's initWithArray: method...)

It's still not clear precisely what you want to achieve (see 
<http://catb.org/~esr/faqs/smart-questions.html#goal>). Generally, though, you 
seem to want a mapping from a touch to a particular label to display the 
current coordinates, along the lines of the class below...

mmalc



#import <UIKit/UIKit.h>
#import <CoreFoundation/CoreFoundation.h>

@interface TouchesView : UIView {
    
    UILabel *touchLabel1;
    UILabel *touchLabel2;
    UILabel *touchLabel3;
    UILabel *touchLabel4;
    UILabel *touchLabel5;
    
    CFMutableDictionaryRef touchToLabelMapping;
    NSMutableArray *availableLabels;
    BOOL setUp;
}

@property (nonatomic, retain) IBOutlet UILabel *touchLabel1;
@property (nonatomic, retain) IBOutlet UILabel *touchLabel2;
@property (nonatomic, retain) IBOutlet UILabel *touchLabel3;
@property (nonatomic, retain) IBOutlet UILabel *touchLabel4;
@property (nonatomic, retain) IBOutlet UILabel *touchLabel5;

@end





@implementation TouchesView

@synthesize touchLabel1, touchLabel2, touchLabel3, touchLabel4, touchLabel5;

- (void)setUpTouchHandling {
    
    touchToLabelMapping = CFDictionaryCreateMutable (kCFAllocatorDefault, 5, 
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    availableLabels = [[NSMutableArray alloc] initWithObjects:touchLabel1, 
touchLabel2, touchLabel3, touchLabel4, touchLabel5, nil];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if (!setUp) {
        [self setUpTouchHandling];
    }
    
    for (UITouch *touch in touches) {
        if (touch.view == self) {
            UILabel *label = (UILabel *)[availableLabels objectAtIndex:0];
            CFDictionaryAddValue (touchToLabelMapping, touch, label);
            [availableLabels removeObjectAtIndex:0];
            label.text = NSStringFromCGPoint([touch locationInView:self]);
        }
    }
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *touch in touches) {
        if (touch.view == self) {
            UILabel *label = (UILabel 
*)CFDictionaryGetValue(touchToLabelMapping, touch);
            label.text = NSStringFromCGPoint([touch locationInView:self]);
        }
    }
}    


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        if (touch.view == self) {
            UILabel *label = (UILabel 
*)CFDictionaryGetValue(touchToLabelMapping, touch);
            label.text = @"{0, 0}";
            CFDictionaryRemoveValue (touchToLabelMapping, touch);
            [availableLabels addObject:label];
        }
    }
}    


- (void)dealloc {
    
    CFRelease(touchToLabelMapping);
    [availableLabels release];
    
    [touchLabel1 release];
    [touchLabel2 release];
    [touchLabel3 release];
    [touchLabel4 release];
    [touchLabel5 release];
    
    [super dealloc];
}


@end

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to