Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Custom Range Slider for iOS

$
0
0

For those who search to make a custom range slider in iOS, this is mine.

OneView.xaml in MyApp.Shared.Views :

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:c="clr-namespace:MyApp.Shared.Forms;assembly=MyApp.Shared" 
    x:Class="Whymee.Shared.Views.OneView"
    Padding="0, 20, 0, 0">
            <c:RangeSlider Minimum="0" Maximum="100" RangeValue="{Binding Minimum, Mode=TwoWay}" Value="{Binding Maximum, Mode=TwoWay}"/>
</ContentPage>

OneViewModel.cs in MyApp.Shared.ViewModels :

`namespace MyApp.Shared.ViewModels
{
public class OneViewModel : ViewModelBase
{
#region Fields
private double _minimum;
private double _maximum;
#endregion

    #region Constructor
    public WantViewModel()
    {
        this._minimum = 0;
        this._maximum = 30;
    }
    #endregion

    #region Properties
    public double Minimum
    {
        get { return this._minimum; }
        set
        {
            this._minimum = value;
            this.RaisePropertyChanged("Minimum");
        }
    }

    public double Maximum
    {
        get { return this._maximum; }
        set
        {
            this._maximum = value;
            this.RaisePropertyChanged("Maximum");
        }
    }
    #endregion
}

}`

RangeSlider.cs in MyApp.Shared.Forms :

`namespace MyApp.Shared.Forms
{
public class RangeSlider : Slider
{
public static readonly BindableProperty RangeValueProperty = BindableProperty.Create<RangeSlider, double>(r => r.RangeValue, 0, BindingMode.TwoWay, null, null, null, null);

    public RangeSlider()
    {
    }

    public double RangeValue
    {
        get { return (double)GetValue(RangeValueProperty); }
        set { SetValue(RangeValueProperty, value); }
    }
}

}`

RangeSliderRenderer.cs in MyApp.iOS.Renderers :

`[assembly: ExportRenderer (typeof(RangeSlider), typeof(RangeSliderRenderer))]

namespace MyApp.iOS.Renderers
{
public class RangeSliderRenderer : SliderRenderer
{
private UISlider _rangeSlider;

    public RangeSliderRenderer()
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement != null || this.Element == null)
            return;

        this.InitSlider();
    }

    private void Element_SizeChanged(object sender, EventArgs e)
    {
        this.InitRangeSlider();
    }

    private void RangeSlider_PanGestureRecognized(UIPanGestureRecognizer recognizer)
    {
        this.SetThumbValue(recognizer.LocationInView (this.NativeView).X);
    }

    /**
    *** Custom functions
    **/

    private void InitSlider()
    {
        this.Control.MinimumTrackTintColor = UIColor.Blue;
        this.Control.MaximumTrackTintColor = UIColor.LightGray;

        this.Element.SizeChanged += Element_SizeChanged;
    }

    private void InitRangeSlider()
    {
        this._rangeSlider = new UISlider();
        this._rangeSlider.Value = (float)(this.Element as RangeSlider).RangeValue;
        this._rangeSlider.MinValue = (float)this.Element.Minimum;
        this._rangeSlider.MaxValue = (float)this.Element.Maximum;
        this._rangeSlider.MinimumTrackTintColor = UIColor.LightGray;
        this._rangeSlider.MaximumTrackTintColor = UIColor.Clear;
        this._rangeSlider.Frame = new System.Drawing.RectangleF(0, 0, (float)this.Element.Width, (float)this.Element.Height);

        UIPanGestureRecognizer panGestureRecognizer = new UIPanGestureRecognizer();
        panGestureRecognizer.AddTarget(() => RangeSlider_PanGestureRecognized(panGestureRecognizer));
        this._rangeSlider.AddGestureRecognizer(panGestureRecognizer);

        this.AddSubview(this._rangeSlider);
    }

    private void SetThumbValue(float x)
    {
        if (x <= this.Element.Width / this.Element.Maximum * (this.Element.Value + 10) && x >= this.Element.Width / this.Element.Maximum * (this.Element.Value - 10) && this._rangeSlider.Value < (float)(this.Element.Maximum / this.Element.Width * x))
            this.Element.Value = (float)(this.Element.Maximum / this.Element.Width * x);
        else if (x <= this.Element.Width / this.Element.Maximum * (this._rangeSlider.Value + 10) && x >= this.Element.Width / this.Element.Maximum * (this._rangeSlider.Value - 10) && this.Element.Value > (float)(this.Element.Maximum / this.Element.Width * x))
        {
            this._rangeSlider.Value = (float)(this.Element.Maximum / this.Element.Width * x);               
            (this.Element as RangeSlider).RangeValue = this._rangeSlider.Value;
        }
    }
}

}`

See u ;-)


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>