Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mandude

macrumors member
Original poster
Nov 19, 2009
64
0
ok so i got this UISwitch that when it is set to ON i would like it that if the user shakes the device... something happens like a label appearing. When it is set to off if you shake the device then nothing happens simple enough not for me though haha here's code:


header:

Code:
#define kAccelerationThreshold 2.2
#define kUpdateInterval (1.0f/10f)

@interface............{
UISwitch *switch;
UILabel *shookDeviceLabel;
}
@property (nonatomic, retain) IBOutlet UISwitch *switch;
@property (nonatomic, reatain) IBOutlet UILabel *shookDeviceLabel;
-(IBAction)switchChanged:(id)sender;
@end


main file:

Code:
@synthesize switch;
@synthesize shookDeviceLabel;

-(IBAction)switchChanged:(id)sender {
if (switch.on = YES) {
- (void)accelerometer:(UIAccelerometer *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration {
if (acceleration.x > kAccelerationThreshold 
            || acceleration.y > kAccelerationThreshold
            || acceleration.z > kAccelerationThreshold) {
shookDeviceLabel.hidden = NO;
}
}


//* pretty sure this is all completely wrong but help would be nice.
 
Code:
[B]-(IBAction)switchChanged:(id)sender[/B] {
if (switch.on = YES) {
[B]- (void)accelerometer:(UIAccelerometer *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration[/B] {
You can't define one method (accelerometer:didAccelerate:) inside another (switchChanged:). Step away from from the real code and go learn the basics of Objective-C.
 
What you would want to do is to start the accelerometer if the switch is on and set it to nil if the switch is off such as below.
Header
Code:
UISwitch *onOff;
	UILabel *shakeStir;
	
	UIAccelerometer *accelerometer;
	
}
@property(nonatomic, retain) IBOutlet UISwitch *onOff;
@property(nonatomic, retain) IBOutlet UILabel *shakeStir;

@property(nonatomic, retain) UIAccelerometer *accelerometer;

- (IBAction)switchChange;
MAIN
Code:
- (void)loadView {
	shakeStir.hidden = YES;
}

- (IBAction)switchChange{
	if (onOff.on == YES){
		self.accelerometer = [UIAccelerometer sharedAccelerometer];
		self.accelerometer.updateInterval = .1;
		self.accelerometer.delegate = self;
	}
	else {
		shakeStir.hidden = YES;
		self.accelerometer = nil;
	}
}

- (void)accelerometer:(UIAccelerometer *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration {
	if (acceleration.x > kAccelerationThreshold 
		|| acceleration.y > kAccelerationThreshold
		|| acceleration.z > kAccelerationThreshold) {
		shakeStir.hidden = NO;
	}
}
 
No problem, that was out of a guide basically 4 links down on google. People just need to search google better.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.