mardi 3 mars 2015

Run function while UIButton is pressed

I am building a remote control car using my iPhone as the controller.


I built a simple button as seen below:



-(void)moveArduinoForward
{
UInt8 buf[3] = {0x01, 0x00, 0x00};
buf[1] = 50;
buf[2] = (int)num >> 8;
NSData *data = [[NSData alloc] initWithBytes:buf length:3];
[self.bleShield write:data];
}

-(void)stopArduino
{
UInt8 buf[3] = {0x05, 0x00, 0x00};
buf[1] = 50;
buf[2] = (int)num >> 8;
NSData *data = [[NSData alloc] initWithBytes:buf length:3];
[self.bleShield write:data];
}



self.moveForwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.moveForwardButton.frame = CGRectMake(430.0, 175.0, 117.0, 133.0);
[self.moveForwardButton setImage:[UIImage imageNamed:@"fwdUp.png"] forState:UIControlStateNormal];
[self.moveForwardButton setImage:[UIImage imageNamed:@"fwdDown.png"] forState:UIControlStateHighlighted];
[self.moveForwardButton addTarget:self action:@selector(moveArduinoForward) forControlEvents:UIControlEventTouchDown];
[self.moveForwardButton addTarget:self action:@selector(stopArduino) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
[self.view addSubview:self.moveForwardButton];


This currently doesn't work as I'd like. It only fires the moveArduinoForward event once, when the finger touches the button. I'd like it to continuously fire. I've tried multiple ways of doing this to no avail, any thoughts?




Aucun commentaire:

Enregistrer un commentaire