Link UIPanGestureRecognizer output to UISlider?

Discussion in 'iOS Programming' started by lexk, Apr 16, 2013.

  1. lexk macrumors newbie

    Joined:
    Aug 8, 2010
    #1
    I'm creating a musical app using libpd that has two ways of controlling the volume and pitch of a waveform - two independent UISliders and an XY pad using UIPanGestureRecognizer.

    What I'd like to be able to do is to have the actions of the XY pad reflected by the sliders and so was wondering how/if that that was possible? (i.e. the Y axis is already controlling volume but to see the slider react to it as well)

    This is where I'm at with the separate controls so far and any help would be greatly appreciated. Thanks!

    Code:
    // Volume slider
    -(IBAction)sineVol:(id)sender
    {
        UISlider *vslider = (UISlider *)sender;
        [PdBase sendFloat:vslider.value toReceiver:@"sine_vol"];
    }
    
    // Pitch slider    
    -(IBAction)sinePitch:(id)sender
    {
        UISlider *pslider = (UISlider *)sender;
        [PdBase sendFloat:pslider.value toReceiver:@"sine_pitch"];
    }
    
    // XY Pad
    -(IBAction)sineXYPad:(UIPanGestureRecognizer *)trigger
    {
        float sinepadHeight = sinexyview.bounds.size.height;
        float sinepadWidth = sinexyview.bounds.size.width;
        CGPoint location = [trigger locationInView:sinexyview];
    
        if ((location.y >= 0) && (location.y < sinepadHeight) && (location.x >= 0) && (location.x < sinepadWidth))
        {
        float sineVolXY = ((location.y) / 250);
        [PdBase sendFloat:sineVolXY toReceiver:@"sine_vol"];
    }
    
        if ((location.y >= 0) && (location.y < sinepadHeight) && (location.x >= 0) && (location.x < sinepadWidth))
        {
        float sinePitchXY = ((location.x) / 5);
        [PdBase sendFloat:sinePitchXY toReceiver:@"sine_pitch"];
        }
    }

    [​IMG]
     
  2. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2

    The value property of a slider is read/write. If you set the value, the slider moves to that position.

    In your pan gesture recognizer action method, just set your sliders' values based on the sineVolXY and sinePitchXY you already calculate. (To do that you will need to create and link up outlets to your 2 sliders.)
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    I believe the OP wants to use the "XY pad" (the gray area beneath the sliders) and have touches in that area affect the values shown for the two sliders. lexk, correct me if I'm wrong. I'm not really familiar with what an "XY pad" is.
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    That was my understanding as well. The pan gesture recognizer action method is what gets triggered when the user slides his/her finger on the "XY pad". Simply adding code that sets the new value to the appropriate slider would accomplish his goal.
     
  5. lexk thread starter macrumors newbie

    Joined:
    Aug 8, 2010
    #5
    Yep that is correct. Basically I'd want the values/output from the XY pad, which responds to the pan gesture recogniser, to be also shown by the sliders.

    How exactly would I go about this?

    Thanks
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    Add outlets for your 2 sliders if you don't have them already. Let's call the outlets pitchSlider and volumeSlider. Connect them in IB (Interface Builder). Then add the 2 lines in bold to your XY Pad's action method:



    Code:
    // XY Pad
    -(IBAction)sineXYPad:(UIPanGestureRecognizer *)trigger
    {
        float sinepadHeight = sinexyview.bounds.size.height;
        float sinepadWidth = sinexyview.bounds.size.width;
        CGPoint location = [trigger locationInView:sinexyview];
    
        if ((location.y >= 0) && (location.y < sinepadHeight) && (location.x >= 0) && (location.x < sinepadWidth))
        {
          float sineVolXY = ((location.y) / 250);
          [COLOR="Red"][B]volumeSlider.value = sineVolXY;[/B][/COLOR]
          [PdBase sendFloat:sineVolXY toReceiver:@"sine_vol"];
        }
    }
    
        if ((location.y >= 0) && (location.y < sinepadHeight) && (location.x >= 0) && (location.x < sinepadWidth))
        {
          float sinePitchXY = ((location.x) / 5);
          [COLOR="Red"][B]pitchSlider.value = sinePitchXY;[/B][/COLOR]
          [PdBase sendFloat:sinePitchXY toReceiver:@"sine_pitch"];
        }
    }
    
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    What do the highlighted lines do?
     
  8. lexk thread starter macrumors newbie

    Joined:
    Aug 8, 2010
    #8
    This worked perfectly, thank you!


    They send float values to a PureData patch that works as the audio engine for the app.
    Basically PdBase is part of libpd which in its simplest terms embeds the PureData patch within the app. :)
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Thanks for the clarification.
     

Share This Page