iOS Question Custom Switch - code examples

ivanomonti

Well-Known Member
Licensed User
Code Class

B4X:
Sub Class_Globals
    Private mValue As Boolean = False
    Private mPane As Panel
    Private mEventName As String
    Private mColor As Int = Colors.Black
    Private mRadius As Int = 8
    Private mPoint(4) As Int
    Dim cv As Canvas
End Sub

Public Sub Initialize(EventName As String, point() As Int) As Panel
   
    Dim mPane As Panel
    mPane.Initialize("pane")
    mPane.Color = Colors.White
    mPane.SetBorder(1,Colors.ARGB(200,0,0,0),mRadius)
   
    mPoint = point
    mEventName = EventName
   
    If draw = True Then
       
    End If
   
    Return mPane
   
End Sub

Private Sub draw As Boolean
   
    Dim cv As Canvas
    cv.Initialize(mPane)
   
    Dim rec As Rect
    rec.Initialize(0,0,mPoint(2),mPoint(3))
    cv.DrawRect(rec,Colors.White,True,1)
   
    Dim color(2) As Int
    color(0) = Colors.ARGB(245,255,255,255)
    color(1) = Colors.ARGB(245,180,180,180)
    color(1) = Colors.ARGB(245,200,200,200)
   
    cv.FillGradient(0,0,mPoint(2),mPoint(3),color)
   
    ' switch
    If mValue = False Then
        Dim rec1 As Rect
        rec1.Initialize(0,0,mPoint(3),mPoint(3))
        cv.DrawRect(rec1,Colors.Red,True,1)
        cv.DrawRectRotated(rec1,Colors.Red,True,3,mRadius,45)
        cv.DrawText("OFF",mPoint(2)/2,(mPoint(3)/2)+11,Font.CreateNew(22),Colors.ARGB(128,0,0,0),"CENTER")
    Else
        Dim rec2 As Rect
        Dim x,y As Int
        x = mPoint(2)-mPoint(3)
        y = 0
        rec2.Initialize(x,y,x+ mPoint(3),mPoint(3))
        cv.DrawRect(rec2,Colors.Green,True,1)
        cv.DrawRectRotated(rec2,Colors.Green,True,3,mRadius,45)
        cv.DrawText("ON",mPoint(2)/2,(mPoint(3)/2)+11,Font.CreateNew(22),Colors.ARGB(128,0,0,0),"CENTER")
    End If
   
    cv.Refresh
   
    Return True
   
End Sub

Sub SetValue(mbool As Boolean)
    mValue = mbool
    If draw = True Then
    End If
End Sub

Sub getValue() As Boolean
    Return mValue
End Sub

Sub pane_Touch(Action As Int, X As Float, Y As Float)
   
    If Action = 0 Then
       
        If mValue = True Then
            mValue = False
        Else
            mValue = True
        End If
   
        If draw = True Then
            CallSubDelayed3(Main, mEventName & "_Click",Sender,mValue)
        End If
       
    End If
   
End Sub
Code initialize


B4X:
    Dim point(4) As Int
    point(0) = 100
    point(1) = 100
    point(2) = 100
    point(3) = 50
   
   
    Dim p As Panel
    Dim myswitch As CustomSwitch
    p = myswitch.Initialize("CSwitch",point)
    Page1.RootPanel.AddView(p,point(0),point(1),point(2),point(3))
    myswitch.SetValue(True)
Code Event

B4X:
Sub CSwitch_Click(obj As Object,value As Boolean)
'   Dim p As Panel = obj
'   Msgbox(p.Tag,value)
End Sub
10175949_811926835538091_2403509818464837283_n.jpg
 
Top