protected Switch(IntPtr handle) : base(handle) { TrackLayer = new CAShapeLayer(); Button = new FabButton(); prepareTrack(); prepareButton(); prepareSwitchSize(SwitchSize.Default); prepareSwitchStyle(SwitchStyle.LightContent); prepareSwitchState(SwitchState.Off); }
private void layoutSwitch() { if (TrackLayer == null) { TrackLayer = new CAShapeLayer(); } if (Button == null) { Button = new FabButton(); } nfloat w = 0; switch (SwitchSize) { case SwitchSize.Small: { w = 30; } break; case SwitchSize.Default: { w = 40; } break; case SwitchSize.Large: { w = 50; } break; } nfloat px = (this.Width() - w) / 2f; TrackLayer.Frame = new CGRect(px, (this.Height() - trackThickness) / 2f, w, trackThickness); TrackLayer.CornerRadius = NMath.Min(TrackLayer.Frame.Height, TrackLayer.Frame.Width) / 2f; Button.Frame = new CGRect(px, (this.Height() - buttonDiameter) / 2f, buttonDiameter, buttonDiameter); onPosition = this.Width() - px - buttonDiameter; offPosition = px; if (internalSwitchState == SwitchState.On) { Button.SetX(onPosition); } }
void handleTouchUpOutsideOrCanceled(FabButton sender, UIEvent e) { var touches = e.TouchesForView(sender).ToArray <UITouch>(); if (touches == null) { return; } var v = touches[0]; if (v != null) { nfloat q = sender.X() + v.LocationInView(sender).X - v.PreviousLocationInView(sender).X; SetSwitchState(q > (this.Width() - Button.Width()) / 2f ? SwitchState.On : SwitchState.Off, true); } }
void handleTouchDragInside(FabButton sender, UIEvent e) { var touches = e.TouchesForView(sender).ToArray <UITouch>(); if (touches == null) { return; } var v = touches[0]; if (v != null) { nfloat q = NMath.Max(NMath.Min(sender.X() + v.LocationInView(sender).X - v.PreviousLocationInView(sender).X, onPosition), offPosition); if (q != sender.X()) { sender.SetX(q); } } }