The Easiest Way to Save and Share Code Snippets on the web

Touches Moved Update

objc | by: jimmcgowan

posted: Feb, 5th 2012 | jump to bottom

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	// as this method will be called regulary, it is a good place to 
	// do the periodic update of the FMOD event system.
	FMOD_EventSystem_Update(eventSystem);
 
	// iterate through each moved touch
	for (UITouch *movedTouch in touches)
	{
		FMOD_EVENT *fingerDragEvent = NULL;
 
		// if there is currently an FMOD finger drag (squeak) event 
		// playing for this touch, then skip over it for now - individual 
		// snap sounds for each touch should not overlap
		fingerDragEvent = (FMOD_EVENT *)[(NSValue *)[fmodEventsForTouches objectForKey:
				[NSNumber numberWithInteger:[movedTouch hash]]] pointerValue];
		if (fingerDragEvent != NULL) 
		{
			FMOD_EVENT_STATE eventState;
			checkFMODError(FMOD_Event_GetState(fingerDragEvent, &eventState));
			if (eventState == FMOD_EVENT_STATE_PLAYING)
			{
				continue;
			}
		}
 
 
		// get the speed of the movement
		NSTimeInterval previousTimestamp = [[previousTouchTimestamps objectForKey:
		[NSNumber numberWithInteger:[movedTouch hash]]] doubleValue];
		CGPoint previousLocation = [movedTouch previousLocationInView:self.view];
 
		NSTimeInterval currentTimestamp = movedTouch.timestamp;
		CGPoint currentLocation = [movedTouch locationInView:self.view];
 
		double distanceMoved = vectorForPoints(previousLocation, currentLocation).magnitude;
		NSTimeInterval durationOfMove = currentTimestamp - previousTimestamp;
 
		double speed = distanceMoved / durationOfMove;
 
 
		// create an instance of the FMOD finger drag (squeak) event
		FMOD_EventGroup_GetEvent(eventGroup, "FingerDrag", 
		                         FMOD_EVENT_DEFAULT, &fingerDragEvent);
 
		// get the speed parameter of the event
		FMOD_EVENTPARAMETER *speedParam;
		FMOD_Event_GetParameterByIndex(fingerDragEvent, 0, &speedParam);
 
		// get the required range of the parameter and constrain the speed value
		float min, max;
		FMOD_EventParameter_GetRange(speedParam, &min, &max);
		float constrainedValue = constrainFloat(speed, min, max);
 
		// set the new value
		checkFMODError(FMOD_EventParameter_SetValue(speedParam, constrainedValue));
 
		// trigger the event
		checkFMODError(FMOD_Event_Start(fingerDragEvent));
 
		// add the event to the dictionary for this touch so we can check its state later
		[fmodEventsForTouches setObject:[NSValue valueWithPointer:fingerDragEvent] 
		                      forKey:[NSNumber numberWithInteger:[movedTouch hash]]];
	}
}
27 views