I have a scrollview that I am loading images into that the user can touch and play a sound. However, the program is getting confused when I press one image with one finger and then another one with a different finger. It thinks you are pushing the same button again and therefore plays the sound again (so you have two of the same sounds playing at the same time even though you may have pressed a different sound button).
I tried setting exclusiveTouch for each UIImage but that didn't seem to work in this case for some reason. What am I missing or is there a better way to do this?
Here is some code:
for creating buttons....
- (void) createButtons {
CGRect myFrame = [self.outletScrollView bounds];
CGFloat gapX, gapY, x, y;
int columns = 3;
int myIndex = 0;
int viewWidth = myFrame.size.width;
int buttonsCount = [g_AppsList count];
float actualRows = (float) buttonsCount / columns;
int rows = buttonsCount / columns;
int buttonWidth = 100;
int buttonHeight = 100;
if (actualRows > rows) rows++;
//set scrollview content size to hold all the glitter icons library
gapX = (viewWidth - columns * buttonWidth) / (columns + 1);
gapY = gapX;
y = gapY;
int contentHeight = (rows * (buttonHeight + gapY)) + gapY;
[outletScrollView setContentSize: CGSizeMake(viewWidth, contentHeight)];
UIImage* myImage;
NSString* buttonName;
//center all buttons to view
int i = 1, j = 1;
for (i; i <= rows; i++) {
//calculate gap between buttons
gapX = (viewWidth - (buttonWidth * columns)) / (columns + 1);
if (i == rows) {
//this is the last row, recalculate gap and pitch
gapX = (viewWidth - (buttonWidth * buttonsCount)) / (buttonsCount + 1);
columns = buttonsCount;
}//end else
x = gapX;
j = 1;
for (j; j <= columns; j++) {
//get shape name
buttonName = [g_AppsList objectAtIndex: myIndex];
buttonName = [NSString stringWithFormat: @"%@.png", buttonName];
myImage = [UIImage imageNamed: buttonName];
TapDetectingImageView* imageView = [[TapDetectingImageView alloc] initWithImage: myImage];
[imageView setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
[imageView setTag: myIndex];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled: YES];
[imageView setMultipleTouchEnabled: NO];
[imageView setExclusiveTouch: YES];
[imageView setDelegate: self];
//add button to current view
[outletScrollView addSubview: imageView];
[imageView release];
x = x + buttonWidth + gapX;
//increase button index
myIndex++;
}//end for j
//increase y
y = y + buttonHeight + gapY;
//decrease buttons count
buttonsCount = buttonsCount - columns;
}//end for i
}
and for playing the sounds...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//stop playing
theAudio.stop;
// cancel any pending handleSingleTap messages
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap) object:nil];
UITouch* touch = [[event allTouches] anyObject];
NSString* filename = [g_AppsList objectAtIndex: [touch view].tag];
NSString *path = [[NSBundle mainBundle] pathForResource: filename ofType:@"m4a"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio setNumberOfLoops:-1];
[theAudio setVolume: g_Volume];
[theAudio play];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
BOOL allTouchesEnded = ([touches count] == [[event touchesForView:self] count]);
if (allTouchesEnded) {
//stop playing
theAudio.stop;
}//end if
//stop playing
theAudio.stop;
}