Help with UISwitch affecting accelerometer

Discussion in 'iOS Programming' started by mandude, Dec 20, 2009.

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    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.
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
  4. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    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;
    	}
    }
    
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    I was trying to help. If you think it was a snob answer, I'm sorry for you. If you just want code you can copy-and-paste (my apologies, KoolStar), you won't get any more help from me. Good luck, though. :)
     
  6. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #6
    No problem, that was out of a guide basically 4 links down on google. People just need to search google better.
     

Share This Page