Creating a realistic Strum on iPhone?
So I have a guitar application that plays chords, like an autoharp. I have created the following UIView class to turn a swipe into a strum as the user's fingers swipe across the strings.
#import "swipetest.h"
@implementation swipetest
@synthesize eStringButton, aStringButton, dStringButton, gStringButton, bStringButton, ehStringButton;
- (void)viewDidLoad {
[NSThread detachNewThreadSelector:@selector(isolateStrums) toTarget:self withObject:nil]; }
- (void)isolateStrums {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self performSelectorOnMainThread:@selector(touchesMoved) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void开发者_Python百科)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [[touches anyObject] locationInView:self];
UIView *subview = [self hitTest:location withEvent:event];
if ([subview isKindOfClass:[UIButton class]])
{
if(CGRectContainsPoint(eStringButton.frame, location)) {
[eStringButton setHighlighted:YES];
[eStringButton sendActionsForControlEvents:UIControlEventTouchDown];
[NSTimer scheduledTimerWithTimeInterval:strumDelay target:self selector:@selector(ETimer) userInfo:nil repeats:NO];
}
else {
[eStringButton setHighlighted:NO];
}
if(CGRectContainsPoint(ehStringButton.frame, location)) {
[ehStringButton setHighlighted:YES];
[ehStringButton sendActionsForControlEvents:UIControlEventTouchDown];
}
else {
[ehStringButton setHighlighted:NO];
}
if(CGRectContainsPoint(bStringButton.frame, location)) {
[bStringButton setHighlighted:YES];
[bStringButton sendActionsForControlEvents:UIControlEventTouchDown];
}
else {
[bStringButton setHighlighted:NO];
}
if(CGRectContainsPoint(aStringButton.frame, location)) {
[aStringButton sendActionsForControlEvents:UIControlEventTouchDown];
[aStringButton setHighlighted:YES];
}
else {
[aStringButton setHighlighted:NO];
}
if(CGRectContainsPoint(dStringButton.frame, location)) {
[dStringButton setHighlighted:YES];
[dStringButton sendActionsForControlEvents:UIControlEventTouchDown];
}
else {
[dStringButton setHighlighted:NO];
}
if(CGRectContainsPoint(gStringButton.frame, location)) {
[gStringButton setHighlighted:YES];
[gStringButton sendActionsForControlEvents:UIControlEventTouchDown];
}
else {
[gStringButton setHighlighted:NO];
}
}
}
- (void)dealloc {
[eStringButton release];
[aStringButton release];
[bStringButton release];
[gStringButton release];
[dStringButton release];
[ehStringButton release];
[super dealloc];
}
@end
The problem is that the view registers rapid fire "hits" even at normal strum speeds. How can I slow down the interface response? I cannot use any commands that pause the NSthread. Is there a way to disable the UIButton for a predetermined period of time after it receives the UIControlEventTouchDown message, say for 0.1 seconds? Should I create individual threads for each string and pause those threads? Right now it kind of works, but can easily turn into the "matrix" sounding stutter guitar. Not good.
Is there a way to disable the UIButton for a predetermined period of time after it receives the UIControlEventTouchDown message, say for 0.1 seconds?
Sure - you can use an NSTimer to change the button's .userInteractionEnabled property. Just set it to 'NO' as soon as you detect your touch down, and fire a timer to set it back to 'YES' after a set number/fractions of seconds.
精彩评论