Observe touches on multiple views in one motion - objective-c

I have a parent view with 3 separate child views. The child views are spread out within the parent with no overlap (and with some space in between). As a user moves her finger around the screen (without lifting it), I'd like to track touches as they enter and exit each of the child views.
Example: If the user begins touching somewhere on the screen outside of the child views, then swipes her finger over child 1, off of child 1, over child 2, and then lets go, I would expect these events to be triggered:
Touch began
Touch entered child 1
Touch exited child 1
Touch entered child 2
Touch ended
It seems as if touchesBegan:withEvent: and touchesEnded:withEvent: methods would be helpful in this case, but when I define them on the child view controllers, they don't do exactly what I want -- if the user begins touching outside the child view, then swipes over the child view, no touch events are triggered on the child itself.
Current Solution: I'm currently using a solution that feels really hacky to accomplish this. I'm observing touchesBegan:withEvent:, touchesEnded:withEvent:, and touchesMoved:withEvent: on the parent, grabbing the coordinates of each event, and determining if they lie within the bounds of a child. If they do, I trigger the appropriate events as described above.
This method mostly works, but feels very inefficient. It feels like the framework should handle this work for me. My state management code also sometimes misses an "enter" or "exit" trigger and I suspect it's because touch events were either dropped or came to me in an unexpected order. Am I missing a better method here?

The simplest solution would be something like:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[self.view addGestureRecognizer:pan];
// Do any additional setup after loading the view.
}
- (void)pan:(UIPanGestureRecognizer *)sender
{
static NSInteger startViewIndex;
static NSInteger endViewIndex;
CGPoint location = [sender locationInView:self.view];
if (sender.state == UIGestureRecognizerStateBegan)
{
if (CGRectContainsPoint(self.view0.frame, location))
startViewIndex = 0;
else if (CGRectContainsPoint(self.view1.frame, location))
startViewIndex = 1;
else if (CGRectContainsPoint(self.view2.frame, location))
startViewIndex = 2;
else
startViewIndex = -1;
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
if (CGRectContainsPoint(self.view0.frame, location))
endViewIndex = 0;
else if (CGRectContainsPoint(self.view1.frame, location))
endViewIndex = 1;
else if (CGRectContainsPoint(self.view2.frame, location))
endViewIndex = 2;
else
endViewIndex = -1;
if (startViewIndex != -1 && endViewIndex != -1 && startViewIndex != endViewIndex)
{
// successfully moved between subviews!
NSLog(#"Moved from %1d to %1d", startViewIndex, endViewIndex);
}
}
}
Perhaps a little more elegant would be to define your own custom gesture recognizer (that way if you aren't dragging from one of your subviews, it will fail which will allow other gesture recognizers you might have going on elsewhwere to work ... probably not an issue unless you're use multiple gesture recognizers; it also isolates the gory details of the gesture logic from the rest of your view controller):
#interface PanBetweenSubviewsGestureRecognizer : UIPanGestureRecognizer
{
NSMutableArray *_arrayOfFrames;
}
#property NSInteger startingIndex;
#property NSInteger endingIndex;
#end
#implementation PanBetweenSubviewsGestureRecognizer
#synthesize startingIndex = _startingIndex;
#synthesize endingIndex = _endingIndex;
- (void)dealloc
{
_arrayOfFrames = nil;
}
- (id)initWithTarget:(id)target action:(SEL)action
{
self = [super initWithTarget:target action:action];
if (self)
{
_arrayOfFrames = [[NSMutableArray alloc] init];
}
return self;
}
- (void)addSubviewToArrayOfFrames:(UIView *)view
{
[_arrayOfFrames addObject:[NSValue valueWithCGRect:view.frame]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
for (NSInteger i = 0; i < [_arrayOfFrames count]; i++)
{
if (CGRectContainsPoint([[_arrayOfFrames objectAtIndex:i] CGRectValue], location))
{
self.startingIndex = i;
return;
}
}
self.startingIndex = -1;
self.endingIndex = -1;
self.state = UIGestureRecognizerStateCancelled;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
for (NSInteger i = 0; i < [_arrayOfFrames count]; i++)
{
if (CGRectContainsPoint([[_arrayOfFrames objectAtIndex:i] CGRectValue], location))
{
self.endingIndex = i;
return;
}
}
self.endingIndex = -1;
self.state = UIGestureRecognizerStateCancelled;
}
#end
Which you could then use as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
PanBetweenSubviewsGestureRecognizer *pan = [[PanBetweenSubviewsGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[pan addSubviewToArrayOfFrames:self.view0];
[pan addSubviewToArrayOfFrames:self.view1];
[pan addSubviewToArrayOfFrames:self.view2];
[self.view addGestureRecognizer:pan];
// Do any additional setup after loading the view.
}
- (void)pan:(PanBetweenSubviewsGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded && sender.startingIndex >= 0 && sender.endingIndex >= 0 && sender.startingIndex != sender.endingIndex)
{
// successfully moved between subviews!
NSLog(#"Moved from %1d to %1d", sender.startingIndex, sender.endingIndex);
}
}

Related

Having Draggable NSTabViewItem

is it possible to provide draggable NSTabViewItem,
Basically what i want, if i down L Button on the label of NSTabViewITem and Move, i should allow to drag the TabView item,
I want to do it for Moving of NSTabView Item and have one more feature, if user drag a Label of NSTabView Item and move it to a perticular region, then i should allow to remove that NSTabView Item,
I could able to find only one way of having PSMTab bar, but i have other features also on NSTabView Item that i will be missing if i go with that approach.
Thanks for looking into it,
Somehow i could able to do it.... posting some important piece of code...
1 -- Have to have Custom TabView class for Handling mouse events.
// Interface posted below,
#import <Cocoa/Cocoa.h>
typedef enum __itemDragState{
itemNotDragging = 0,
itemDragStatNormal = 0,
itemDragging = 1,
itemDropped = 2
} ItemDragStat;
#protocol CustomTabViewDelegate <NSObject>
#required
-(bool)allowDrag;
-(bool)allowDrop;
-(void)dragEnter;
-(void)acceptDrop;
-(void)draggingCancelled;
-(void)itemDropped:(id)draggedTabViewItem;
-(void)itemDroppedCompleted:(id)droppedTabViewItem;
#end
#interface CustomTab : NSTabView{
ItemDragStat eItemDragStat;
id draggedItem;
}
#property(assign)id draggedItem;
#end
Now some of the important implementation
#import "CustomTab.h"
#include "Log.h"
#implementation CustomTab
#synthesize draggedItem;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
# if 0
// don't delete it, might need later on
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
# endif
- (void)mouseUp:(NSEvent *)theEvent{
log(" Mouse up ");
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
NSTabViewItem *anItem = [self tabViewItemAtPoint: location];
if ( anItem == nil ) {
// if its mouse up else where, reject dragging regardless
eItemDragStat = itemDragStatNormal;
log("Item will not be dropped");
return;
}
if ( ![anItem isEqual:[self selectedTabViewItem]]){
log("Mouse up is in nonselected item");
if ( eItemDragStat == itemDragging){
log("Item will be dropped into this ");
id droppedTabViewItem = anItem;
if ( droppedTabViewItem && [droppedTabViewItem respondsToSelector:#selector(itemDropped:)]){
id selectedTabViewItem = [self selectedTabViewItem];
[droppedTabViewItem performSelector:#selector(itemDropped:) withObject:selectedTabViewItem];
}
}
}
eItemDragStat = itemDragStatNormal;
// return;
// [super mouseUp:theEvent];
}
- (void)mouseDown:(NSEvent *)theEvent{
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
draggedItem = [self tabViewItemAtPoint:location];
NSTabViewItem *anItem = [self tabViewItemAtPoint: location];
if (anItem != nil && ![anItem isEqual: [self selectedTabViewItem]])
{
[self selectTabViewItem: anItem];
}
}
- (void)mouseDragged:(NSEvent *)theEvent{
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
id tabViewItemId = [self tabViewItemAtPoint:location];
NSTabViewItem *anItem = [self tabViewItemAtPoint: location];
if (anItem){
if (![anItem isEqual:draggedItem]){
if (tabViewItemId && [tabViewItemId respondsToSelector:#selector(allowDrag)]){
eItemDragStat = itemDragging;
}else{
// drag will be cancelled now.
// tell client item to stop dragging
if (eItemDragStat == itemDragging){
if ( draggedItem && [ draggedItem respondsToSelector:#selector(draggingCancelled)]){
[draggedItem performSelector:#selector(draggingCancelled)];
draggedItem = nil;
}
}
eItemDragStat = itemNotDragging;
// if we have +cursor then it should be reset
}
}else{
log(" Mouse dragged");
}
}else{
// dragging went elsewhere, lets close this dragging operation
if ( draggedItem && [ draggedItem respondsToSelector:#selector(draggingCancelled)]){
[draggedItem performSelector:#selector(draggingCancelled)];
draggedItem = nil;
}
// here reset the mouse pointer
eItemDragStat = itemNotDragging;
}
}
#end
It needs some more fine tuning and its going on....

NSPathControl with popups for each component of the path?

From Apple's sample code and reading the docs I can see no way configuring the NSPathControl to behave similarly to e.g. the 'jump bar' in the Xcode Editor window:
I.e. have it represent a path (or other kind of hierarchy) and make each component of the path a clickable popup to navigate the hierarchy..?
Anybody having luck faking such behaviour using a NSPathControlDelegate listening to clicks and showing a menu in a temporary window?
Seems like a common design where one would even expect some OSS implementation - but no such luck yet googling for it..
I made a subclass of NSPathControl so that I can use mouseDown: to popup the context menus of the component cells at the right position. I added also a delegate to the menu to create deeper menus on demand.
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSPathCell *cell = self.cell;
NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point
withFrame:self.bounds
inView:self];
NSRect componentRect = [cell rectOfPathComponentCell:componentCell
withFrame:self.bounds
inView:self];
NSMenu *menu = [componentCell menuForEvent:event
inRect:componentRect
ofView:self];
if (menu.numberOfItems > 0) {
NSUInteger selectedMenuItemIndex = 0;
for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) {
if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) {
selectedMenuItemIndex = menuItemIndex;
break;
}
}
NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex];
[menu popUpMenuPositioningItem:selectedMenuItem
atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2)
inView:self];
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
if (event.type != NSLeftMouseDown) {
return nil;
}
return [super menuForEvent:event];
}
I extended Stephan's answer slightly to accommodate for lazily loading the menu items. I created a small protocol to call for the menu rather than having to build the menu's ahead of time for each cell:
NSPathControlExtended.h
#protocol NSPathControlExtendedDelegate <NSPathControlDelegate>
#required
- (NSMenu *)pathControl:(NSPathControl *)pathControl menuForCell:(NSPathComponentCell *)cell;
#end
#interface NSPathControlExtended : NSPathControl
#property (weak) id <NSPathControlExtendedDelegate> delegate;
#end
NSPathControlExtended.m
#import "NSPathControlExtended.h"
#implementation NSPathControlExtended
#synthesize delegate;
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSPathCell *cell = self.cell;
NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point
withFrame:self.bounds
inView:self];
NSRect componentRect = [cell rectOfPathComponentCell:componentCell
withFrame:self.bounds
inView:self];
NSMenu *menu = [delegate pathControl:self menuForCell:componentCell];
if (menu.numberOfItems > 0) {
NSUInteger selectedMenuItemIndex = 0;
for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) {
if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) {
selectedMenuItemIndex = menuItemIndex;
break;
}
}
NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex];
[menu popUpMenuPositioningItem:selectedMenuItem
atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2)
inView:self];
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
if (event.type != NSLeftMouseDown) {
return nil;
}
return [super menuForEvent:event];
}
#end

UIViewController stops receiving touch events on UIWebView

I am working on a magazine viewer and I have to use UIWebView for pages because of html5 interactive contents. First I tried uiwebviews in a UIScrollView but scrollview was too slow on sliding pages.
So now I am trying to write my own scrollview-like code. I have a MainView.xib, a view controller (Viewer) and extended UIWindow class (TouchCapturingWindow) that I take from here : http://wyldco.com/blog/2010/11/how-to-capture-touches-over-a-uiwebview/
when I try to slide pages fast, there is no problem but when I touch and slowly slide by not pulling my finger, "Viewer" view controller stops receiving touch events and so pages stop moving. I am logging TouchCapturingWindow, it still sending events. I searched many information and tutorials but I couldn't make it work. How can I make it continuously receive touch events?
I uploaded a simple Xcode project that contains only this part of my project. You can download here : http://testdergi.mysys.com/touchEvents.zip
When you run project, you should first download pages (180Kb) by tapping "Download Pages" button, then tap the "Open Viewer" button.
You can also look over the code below :
TouchCapturingWindow.h :
#interface TouchCapturingWindow : UIWindow {
NSMutableArray *views;
#private
UIView *touchView;
}
- (void)addViewForTouchPriority:(UIView*)view;
- (void)removeViewForTouchPriority:(UIView*)view;
#end
TouchCapturingWindow.m :
#implementation TouchCapturingWindow
- (void)dealloc {
}
- (void)addViewForTouchPriority:(UIView*)view {
if ( !views ) views = [[NSMutableArray alloc] init];
[views addObject:view];
}
- (void)removeViewForTouchPriority:(UIView*)view {
if ( !views ) return;
[views removeObject:view];
}
- (void)sendEvent:(UIEvent *)event {
//get a touch
UITouch *touch = [[event allTouches] anyObject];
//check which phase the touch is at, and process it
if (touch.phase == UITouchPhaseBegan) {
for ( UIView *view in views ) {
//if ( CGRectContainsPoint([view frame], [touch locationInView:[view superview]]) ) {
NSLog(#"TouchCapturingWindow --> TouchPhaseBegan");
touchView = view;
[touchView touchesBegan:[event allTouches] withEvent:event];
break;
}
}
else if (touch.phase == UITouchPhaseMoved) {
NSLog(#"TouchCapturingWindow --> TouchPhaseMoved");
if ( touchView ) {
[touchView touchesMoved:[event allTouches] withEvent:event];
}
else
{
NSLog(#"touch view is nil");
}
}
else if (touch.phase == UITouchPhaseCancelled) {
NSLog(#"TouchCapturingWindow --> TouchPhaseCancelled");
if ( touchView ) {
[touchView touchesCancelled:[event allTouches] withEvent:event];
touchView = nil;
}
}
else if (touch.phase == UITouchPhaseEnded) {
NSLog(#"TouchCapturingWindow --> TouchPhaseEnded");
if ( touchView ) {
[touchView touchesEnded:[event allTouches] withEvent:event];
touchView = nil;
}
}
//we need to send the message to the super for the
//text overlay to work (holding touch to show copy/paste)
[super sendEvent:event];
}
#end
Viewer.h :
#interface Viewer : UIViewController{
int currentPage;
int totalPages;
IBOutlet UIView *pagesView;
int lastTchX;
int difference;
BOOL hasMoved;
int touchBeganSeritX;
}
- (IBAction)backBtnClicked:(id)sender;
- (void)loadImages;
- (void)animationDidStop;
- (void)loadSinglePage:(int)pageNo;
#property (nonatomic, retain) IBOutlet UIView *pagesView;
#end
Viewer.m :
#interface Viewer ()
#end
#implementation Viewer
#synthesize pagesView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)backBtnClicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)loadImages
{
for(UIView *subView in pagesView.subviews)
{
[subView removeFromSuperview];
}
currentPage = 1;
totalPages = 12;
for(int count = 1; count <= 12; count++)
{
[self loadSinglePage:count];
}
}
- (void)loadSinglePage:(int)pageNo
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
NSString *pagesDir = [NSString stringWithFormat:#"%#/pages", cachesDir];
int pageX = (pageNo - 1) * 768;
UIWebView *aPageWebView = [[UIWebView alloc] init];
[aPageWebView setFrame:CGRectMake(pageX, 0, 768, 1024)];
aPageWebView.backgroundColor = [UIColor clearColor];
aPageWebView.opaque = YES;
[aPageWebView setClearsContextBeforeDrawing:YES];
aPageWebView.clipsToBounds = NO;
[aPageWebView setScalesPageToFit:YES];
NSString *hamData = [NSString stringWithFormat:#"<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"user-scalable=yes, width=1024, height=1365, maximum-scale=1.0\"><style type=\"text/css\">body {margin:0; padding:0;}</style></head><body bgcolor=\"#508CCF\"><div id=\"touchable\" style=\"top:0px; left:0px; width:1024px; height:1365px; background-image:url(%#.jpg)\"></div></body></html>", [[NSNumber numberWithInt:pageNo] stringValue]];
[aPageWebView loadHTMLString:hamData baseURL:[NSURL fileURLWithPath:pagesDir isDirectory:YES]];
aPageWebView.scrollView.bounces = NO;
[aPageWebView.scrollView setMaximumZoomScale:1.3333f];
[aPageWebView.scrollView setMinimumZoomScale:1.0f];
aPageWebView.scrollView.zoomScale = 1.0f;
[aPageWebView setMultipleTouchEnabled:YES];
[pagesView addSubview:aPageWebView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
UITouch *myTouch = [[event allTouches] anyObject];
int curTchX = [myTouch locationInView:self.view].x;
lastTchX = curTchX;
hasMoved = NO;
touchBeganSeritX = pagesView.frame.origin.x;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Viewer : .....moved");
hasMoved = YES;
UITouch *myTouch = [[event allTouches] anyObject];
int curTchX = [myTouch locationInView:self.view].x;
difference = curTchX - lastTchX;
int newX = (pagesView.frame.origin.x + difference);
if(newX <= 0)
{
[pagesView setFrame:CGRectMake((pagesView.frame.origin.x + difference), 0, pagesView.frame.size.width, 1024)];
}
lastTchX = curTchX;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
if(hasMoved == YES)
{
hasMoved = NO;
int curSeritX = pagesView.frame.origin.x;
curSeritX = curSeritX / (-1);
int newX = 0;
if(difference < 0) //Sağa geçilecek
{
if((currentPage + 1) <= totalPages)
{
currentPage++;
}
}
else //Sola geçilecek
{
if((currentPage - 1) >= 1)
{
currentPage--;
}
}
newX = (currentPage - 1)*768*(-1);
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
// Do your animations here.
[pagesView setFrame:CGRectMake(newX, 0, pagesView.frame.size.width, pagesView.frame.size.height)];
}
completion:^(BOOL finished){
if (finished) {
// Do your method here after your animation.
}
}];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesCancelled");
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
I am answering my own question. I found something after a hard work. I changed "(void)sendEvent:(UIEvent *)event" method like the code below. I saw that use of "[super sendEvent:event];" after UITouchPhaseMoved phase is problematic.
- (void)sendEvent:(UIEvent *)event {
//we need to send the message to the super for the
//text overlay to work (holding touch to show copy/paste)
//[super sendEvent:event];
//get a touch
UITouch *touch = [[event allTouches] anyObject];
//check which phase the touch is at, and process it
if (touch.phase == UITouchPhaseBegan) {
for ( UIView *view in views ) {
//if ( CGRectContainsPoint([view frame], [touch locationInView:[view superview]]) ) {
NSLog(#"TouchCapturingWindow --> TouchPhaseBegan");
touchView = view;
[touchView touchesBegan:[event allTouches] withEvent:event];
break;
}
[super sendEvent:event];
}
else if (touch.phase == UITouchPhaseMoved) {
NSLog(#"TouchCapturingWindow --> TouchPhaseMoved");
if ( touchView ) {
[touchView touchesMoved:[event allTouches] withEvent:event];
int curTchX = [touch locationInView:self].x;
NSLog(#"curTchX : %d", curTchX);
}
else
{
NSLog(#"touch view is nil");
}
}
else if (touch.phase == UITouchPhaseCancelled) {
NSLog(#"TouchCapturingWindow --> TouchPhaseCancelled");
if ( touchView ) {
[touchView touchesCancelled:[event allTouches] withEvent:event];
touchView = nil;
}
[super sendEvent:event];
}
else if (touch.phase == UITouchPhaseEnded) {
NSLog(#"TouchCapturingWindow --> TouchPhaseEnded");
if ( touchView ) {
[touchView touchesEnded:[event allTouches] withEvent:event];
touchView = nil;
}
[super sendEvent:event];
}
//we need to send the message to the super for the
//text overlay to work (holding touch to show copy/paste)
}
But the new problem is; if I add a video in a web page, and try to forward the video with its slider, page is moving. I have to find a way to detect if user taps on a video.

TouchesMoved only returns touches that have changed ignoring touches that haven't changed

I'm working on a simple game where "particles" are attracted to user touches.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touch = [touches allObjects];
for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) {
lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self];
}
isTouching = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touch = [touches allObjects];
for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) {
lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touch = [touches allObjects];
for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) {
lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self];
}
if (!stickyFingers) {
isTouching = NO;
}
}
lastTouches is an array of CGPoints that another part of the program uses to move the particles.
The problem I am having is that the way I have it setup now, whenever any of the 3 functions are called, they overwrite the array of CGPoints and the numberOfTouches. I didn't think this would be a problem, but it turns out that TouchesMoved only gets the touches that have changed and doesn't get the touches that have stayed the same. As a result, if you move one of your fingers but not another, the program forgets about the finger that isn't moving and all the particles go towards the moving finger. If you move both fingers, the particles move in between the two fingers as they should.
I need someway to hold on to the touches that haven't moved while updating the ones that have.
Any Suggestions?
Set theory to the rescue!
//Instance variables
NSMutableSet *allTheTouches;
NSMutableSet *touchesThatHaveNotMoved;
NSMutableSet *touchesThatHaveNeverMoved;
//In touchesBegan:withEvent:
if (!allTheTouches) {
allTheTouches = [[NSMutableSet alloc] init];
touchesThatHaveNotMoved = [[NSMutableSet alloc] init];
touchesThatHaveNeverMoved = [[NSMutableSet alloc] init];
}
[allTheTouches unionSet:touches];
[touchesThatHaveNotMoved unionSet:touches];
[touchesThatHaveNeverMoved unionSet:touches];
//In touchesMoved:withEvent:
[touchesThatHaveNeverMoved minusSet:touches];
[touchesThatHaveNotMoved setSet:allTheTouches];
[touchesThatHaveNotMoved minusSet:touches];
//In touchesEnded:withEvent:
[allTheTouches minusSet:touches];
if ([allTheTouches count] == 0) {
[allTheTouches release]; //Omit if using ARC
allTheTouches = nil;
[touchesThatHaveNotMoved release]; //Omit if using ARC
touchesThatHaveNotMoved = nil;
}
[touchesThatHaveNotMoved minusSet:touches];
[touchesThatHaveNeverMoved minusSet:touches];
In the above, touchesThatHaveNotMoved will hold the touches that didn't move in the last touchesMoved:, and touchesThatHaveNeverMoved will hold the touches that have not moved even once since they began. You can omit either or both variables, and all the statements that involve them, that you don't care about.

enabling multi touch... need help with storing the 2nd touch

need to be able to store 2 touches, how I go about this i how no idea...
this is how I'm doing a single touch
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/////checks whether the screen has been touched and stores its location and converts the coordinates to be avaible for use////
UITouch* myTouch = [touches anyObject];
CGPoint locationLeft = [myTouch locationInView: [myTouch view]];
locationLeft = [[CCDirector sharedDirector]convertToGL:locationLeft];
how would I be able to store the 2nd touch?
Thanks in advance
You should use ccTouchBegan (notice the singular 'touch' instead of 'touches') when you need to handle multi-touches. (IMO people should just ditch the ccTouchesBegan/Moved/Ended and just use ccTouchBegan/Moved/Ended).
Each of ccTouchBegan/Moved/Ended is called for each touch which mean you can easily differentiate between multiple touches. Example:
- (void)registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (self.firstTouch == nil) {
// we got the first touch
self.firstTouch = touch;
}
else if (self.secondTouch == nil) {
// we got the second touch
self.secondTouch = touch;
}
// return YES to consume the touch (otherwise it'll cascade down the layers)
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
if (touch == self.firstTouch) {
// we got the first touch
// do stuff
}
else if (touch == self.secondTouch) {
// we got the second touch
// do stuff
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
if (touch == self.firstTouch) {
// first touch ended so remove both touches
self.firstTouch = nil;
self.secondTouch = nil;
}
else if (touch == self.secondTouch) {
// second touch ended so remove touch only
self.secondTouch = nil;
}
}
Have you tried iterating through the touches like this?
for (UITouch *touch in touches){
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
location = [self convertToNodeSpace:location];