iOS Question How to make a gradient TopLeft - BottomRight

kompiler

Member
Licensed User
How to make a gradient TopLeft-BottomRight?

Erel's code draws Top-Bottom.

B4X:
Sub SetGradient(v As View, color1 As Int, color2 As Int, Replace As Boolean)
  Dim NaObj As NativeObject = Me
  NaObj.RunMethod("SetGradient::::",Array(v,NaObj.ColorToUIColor(color1),NaObj.ColorToUIColor(color2), Replace))
End Sub

#If OBJC
- (void)SetGradient: (UIView*) View :(UIColor*) Color1 :(UIColor*) Color2 :(BOOL)replace{
   CAGradientLayer *gradient = [CAGradientLayer layer];
   gradient.colors = [NSArray arrayWithObjects:(id)Color1.CGColor, (id)Color2.CGColor, nil];
   gradient.frame = View.bounds;
   if (replace)
     [View.layer replaceSublayer:View.layer.sublayers[0] with:gradient];
   else
     [View.layer insertSublayer:gradient atIndex:0];
}
#end if
 

Erel

Administrator
Staff member
Licensed User
Use BitmapCreator to create the gradient:
B4X:
Sub Process_Globals
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private bc As BitmapCreator       
End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   bc.Initialize(200, 200)
   NavControl.ShowPage(Page1)
End Sub

Sub CreateGradient (pnl As B4XView, Color1 As Int, Color2 As Int)
   If pnl.NumberOfViews > 0 And "gradient" = pnl.GetView(pnl.NumberOfViews - 1).Tag Then
       pnl.GetView(pnl.NumberOfViews - 1).RemoveViewFromParent
   End If
   bc.FillGradient(Array As Int(Color1, Color2), bc.TargetRect, "TL_BR")
   Dim iv As ImageView
   iv.Initialize("")
   iv.Tag = "gradient"
   pnl.AddView(iv, 0, 0, pnl.Width, pnl.Height)
   bc.SetBitmapToImageView(bc.Bitmap, iv)
   iv.ContentMode = iv.MODE_FILL
End Sub
 
Top