MvvmCross iOS hamburger Menu without plugins iOS Native - c#

Okay,This is What I would like to do in MvvmCross without any plugins just native code. I did find a Tutorial on how to it, but I would like it in MvvmCross.iOS Have a look at what I would like to do in MvvmCross.iOS
Please advise or forward a better tutorial for MvvmCross.iOS
points to remember
The hamburger menu should have a draggable effect like the image I have linked
what I have tried
ViewDidLoad() -->
UIPanGestureRecognizer gesture = new UIPanGestureRecognizer();
gesture.AddTarget(() => HandleDrag(gesture));
this.View.AddGestureRecognizer(gesture);
panGestureRecognizer = new UIScreenEdgePanGestureRecognizer ( HandleSwipeRight);
panGestureRecognizer.Edges = UIRectEdge.Left;
this.View.AddGestureRecognizer(panGestureRecognizer);
HandleDrag() -->
protected void HandleDrag(UIPanGestureRecognizer recognizer)
{
PointF offset2 = (System.Drawing.PointF)recognizer.TranslationInView(View);
if (recognizer.State != (UIGestureRecognizerState.Cancelled | UIGestureRecognizerState.Failed
| UIGestureRecognizerState.Possible))
{
Console.WriteLine("Here");
// NEED TO LOAD ANOTHER VIEW HERE
openMenu();
}
}
openMenu() -->
public void openMenu()
{
viewBlack.Hidden = false;
this.view.Hidden = false;
UIView.Animate(
duration: 0.3,
delay: 0,
options: UIViewAnimationOptions.CurveEaseInOut |
UIViewAnimationOptions.Autoreverse,
animation: () =>
{
this.view.LayoutIfNeeded();
this.viewBlack.Alpha = this.maxBlackViewAlpha = 0.5f;
},
completion: () =>
{
panGestureRecognizer.Enabled = false;
}
);
}
hideMenu() -->
public void closeMenu(){
UIView.Animate(
duration: 0.3,
delay: 0,
options: UIViewAnimationOptions.CurveEaseInOut |
UIViewAnimationOptions.Autoreverse,
animation: () =>
{
this.view.LayoutIfNeeded();
this.viewBlack.Alpha = 0;
},
completion: () =>
{
panGestureRecognizer.Enabled = true;
viewBlack.Hidden = true;
view.Hidden = true;
}
);
}
My Custom Hamburger menu UIView -->
view = new UIView();
view.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width / 1.1, UIScreen.MainScreen.Bounds.Height);
var gradientLayer = new CAGradientLayer();
gradientLayer.Colors = new[] { UIColor.FromRGB(64, 0, 128).CGColor, UIColor.FromRGB(0, 0, 128).CGColor };
gradientLayer.Locations = new NSNumber[] { 0, 1 };
gradientLayer.Frame = view.Frame;
view.BackgroundColor = UIColor.Clear;
view.Layer.AddSublayer(gradientLayer);
var viewline = new UIView();
viewline.Frame = new CGRect(20, 60, 100, 1);
viewline.BackgroundColor = UIColor.White;
var bb = new UIBarButtonItem();
var Allbutton = new UIButton(new CGRect(0, 20, 135, 20));
Allbutton.SetTitleColor(UIColor.Black, UIControlState.Normal);
Allbutton.TitleLabel.BackgroundColor = UIColor.White;
Allbutton.SetTitle("Login", UIControlState.Normal);
var myPrefbutton = new UIButton(new CGRect(0, 120, 135, 20));
myPrefbutton.SetTitleColor(UIColor.Black, UIControlState.Normal);
myPrefbutton.SetTitle("Logout", UIControlState.Normal);
myPrefbutton.TitleLabel.BackgroundColor = UIColor.White;
view.BackgroundColor = UIColor.White;
view.Add(Allbutton);
view.Add(viewline);
view.Add(myPrefbutton);
view.Hidden = true;
this.View.AddSubviews(view);
this is the only code I was able to convert into MvvmCross.iOS from tutorial(SWIFT) and it's works but
I cannot drag the menu to show, what happens is that it load up normally and it's fast
Note!!! I am not using any Storyboards or nib files just using pure code for this hamburger menu
please has a good look at the .gif notice that the menu is draggable which makes it's animation slow and not fast.
if I have confused you please don't be sad I have just started coding in iOS and MvvmCross... I'm still a noob

Got it to work
first had to create a UIVew class -->
SideMenuView : MvxViewController
then set the X to minus... it will be zero if a user selects the navbaritem I also added a overlay UIView
viewBlack = new UIView();
viewBlack.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
viewBlack.BackgroundColor = UIColor.Black;
viewBlack.Alpha = 0.5f;
viewBlack.Hidden = true;
this.View.AddSubviews(viewBlack);

Related

How to disable animations on Highcharts Dotnet C# MVC?

So I have begun using Dotnet.Highcharts in Visual Studio with C# and MVC,
Its quite different from its javascript counterpart, I need to use ghostjs to take a picture from my web page to send as a report, as many must know, this means that animations interfere with this working properly as bars show up to half of the height they should be.
I'm using the recommended coding from Dotnet.highcharts which mostly uses Razor to configure the chart.
I have tried disabling animations in all these places:
PlotOptions = new PlotOptions
{
Column = new PlotOptionsColumn
{
PointPadding = 0.2,
BorderWidth = 0,
Animation = new Animation() { Enabled = false, Duration = 0 }
},
Series = new PlotOptionsSeries
{
Animation = new Animation() { Enabled = false, Duration = 0}
}
},
chartOptions.Drilldown.Animation = new Animation() { Enabled = false, Duration = 0};
chartOptions.Chart.Animation.Enabled = false;
and still the chart keeps animating, I just don't know what is going on
here is my full Razor code for the chart
var chartOptions = new Highcharts
{
Title = new Title { Text = "Transactions" },
Subtitle = new Subtitle { Text = "December" },
XAxis = new List<XAxis>
{
new XAxis
{
Categories = ViewBag.ListCategories
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Min = 0,
Title = new YAxisTitle
{
Text = "Tx"
}
}
},
Tooltip = new Tooltip
{
HeaderFormat = "<span style='font-size:10px'>{point.key}</span><table style='font-size:12px'>",
PointFormat = "<tr><td style='color:{series.color};padding:0'>{series.name}: </td><td style='padding:0'><b>{point.y:.0f}</b></td></tr>",
FooterFormat = "</table>",
Shared = true,
UseHTML = true
},
PlotOptions = new PlotOptions
{
Column = new PlotOptionsColumn
{
PointPadding = 0.2,
BorderWidth = 0,
Animation = new Animation() { Enabled = false, Duration = 0 }
},
Series = new PlotOptionsSeries
{
Animation = new Animation() { Enabled = false, Duration = 0}
}
},
Series = new List<Series>
{
new ColumnSeries
{
Name = "TX",
Data = ViewData["DataTX"] as List<ColumnSeriesData>
}
}
};
chartOptions.ID = "chart";
chartOptions.Drilldown.Animation = new Animation() { Enabled = false, Duration = 0};
chartOptions.Chart.Animation.Enabled = false;
var renderer = new HighchartsRenderer(chartOptions);
This one worked for me. Had to play with it a while, seems there's no clear documentation.
DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Areaspline})
.SetTitle(new Title
{
Text = "myLabel",
X = -20
})
.SetSeries(new[]
{
new Series { Name = "MyUsers", Data = new Data(myData), PlotOptionsSeries=new PlotOptionsSeries(){Animation=new Animation(false)} }
});
I found a way to disable the animation inside highcharts.js (From another question for the js version)
showCheckbox: !1,
animation: { duration: 1E3 }
, events: {},
I changed 1E3 to 0 and it worked, however, this is not the optimal solution, there must be a way to achieve this the right way. The whole point of the highcharts dll for visual studio is the ability to use highcharts without touching javascript
For now, I won't accept my own answer as the answer

Xamarin and ios Anchor Constraints not working

If I add this code to the ViewDidLoad of the basic startup project the red view is nowhere to be seen... I think it should show at the top center of the window... what am I doing wrong?
var redview = new UIView(new CGRect(0, 0, 100, 200));
redview.BackgroundColor = UIColor.Red;
redview.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddSubview(redview);
redview.TopAnchor.ConstraintEqualTo(this.View.TopAnchor).Active = true;
redview.CenterXAnchor.ConstraintEqualTo(this.View.CenterXAnchor).Active = true;
Because you have set the frame of the redView.which will be in conflict with Constraint.
Refer to the following code
var redview = new UIView();
redview.BackgroundColor = UIColor.Red;
redview.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddSubview(redview);
redview.TopAnchor.ConstraintEqualTo(this.View.TopAnchor).Active = true;
redview.CenterXAnchor.ConstraintEqualTo(this.View.CenterXAnchor).Active = true;
redview.WidthAnchor.ConstraintEqualTo(100).Active=true;
redview.HeightAnchor.ConstraintEqualTo(200).Active = true;

Xamarin.Forms loading large dataset with Android

I've been working on a Xamarin Forms project and I'm having trouble loading data into the Android application. It loads fine for iOS and UWP, but on Android it takes forever. This is happening on two different screens but I'm assuming the problem is the same. I'm sure there are better ways to load the data than what I am using.
This is the first screen I'm having trouble with on Android (screenshot from UWP):
It loads 15 products in each category into a horizontal scroller. The "See All" link loads all of the products in that category into a vertical scrolling grid. That's the second screen I'm having trouble with.
The code to load this view is:
var scroller = new TLScrollView
{
Orientation = ScrollOrientation.Horizontal,
ItemTemplate = new DataTemplate(() => ViewFactory.Merchandise.Invoke()),
HeightRequest = 320,
Padding = new Thickness(0, 0, 0, 10)
};
scroller.SetBinding(TLScrollView.ItemsSourceProperty, nameof(items));
scroller.ItemsSource = items.OrderByDescending(d => d.Cost).Take(15);
The ViewFactory code is:
public static Func<View> Merchandise { get; } = () =>
{
var mainGrid = new Grid() { WidthRequest = 250, Margin = 5 };
mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) });
mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) });
mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(25, GridUnitType.Absolute) });
mainGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(35, GridUnitType.Absolute) });
var nameLabel = new Label() { Text = "Casket Name", LineBreakMode = LineBreakMode.TailTruncation, HorizontalTextAlignment = TextAlignment.Center };
var materialLabel = new Label() { Text = "MaterialName", HorizontalTextAlignment = TextAlignment.Center };
var costLabel = new Label() { Text = "Cost", HorizontalTextAlignment = TextAlignment.Center };
nameLabel.SetBinding(Label.TextProperty, "ItemName");
materialLabel.SetBinding(Label.TextProperty, "Material");
costLabel.SetBinding(Label.TextProperty, "CostFormatted");
GlobalModel globalModel = App.Current.MainPage.BindingContext as GlobalModel;
var catalogImage = new CachedImage
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Aspect = Aspect.AspectFit,
LoadingPlaceholder = "MAFSLoader.gif",
CacheDuration = TimeSpan.FromDays(10),
RetryCount = 1,
RetryDelay = 100
};
catalogImage.BindingContextChanged += (s, o) => {
catalogImage.Source = null;
var item = catalogImage.BindingContext as CatalogItem;
if (item == null) return;
catalogImage.Source = item.SmallURL;
};
var buttonStyle = (Style)Application.Current.Resources["ButtonStyle"];
var addToCartButton = new Button { Text = "Add to Cart", BackgroundColor = Color.FromHex("#486E8E"), WidthRequest = 250, Style = buttonStyle };
addToCartButton.SetBinding(Button.CommandParameterProperty, "ItemID");
var imgTapGesture = new TapGestureRecognizer()
{
Command = new Command(() =>
{
if (addToCartButton.CommandParameter == null)
{
addToCartButton.SetBinding(Button.CommandParameterProperty, "ItemId");
}
var dictionary = new Dictionary<string, string>();
dictionary.Add(globalModel.CurrentFuneralHomeKey, addToCartButton.CommandParameter.ToString());
globalModel.ProductDetail.Execute(dictionary);
})
};
catalogImage.GestureRecognizers.Add(imgTapGesture);
addToCartButton.Clicked += new EventHandler(async delegate (Object o, EventArgs a)
{
var button = ((Button)o);
globalModel.AddToCart.Execute(button.BindingContext);
if (button.BindingContext.GetType() == typeof(Data.Corner))
{
await App.Current.MainPage.DisplayAlert("Shopping Cart", "You've added " + (button.BindingContext as Data.Corner).ItemName + " to your cart.", null, "OK");
}
});
mainGrid.Children.Add(catalogImage, 0, 0);
mainGrid.Children.Add(nameLabel, 0, 1);
mainGrid.Children.Add(materialLabel, 0, 2);
mainGrid.Children.Add(costLabel, 0, 3);
mainGrid.Children.Add(addToCartButton, 0, 4);
return mainGrid;
};
If anyone could point me in the right direction I'd be very grateful!
Interesting. I've just gone through the implementation of the TLSrollview. It looks like there is no recycling of elements and maybe lack some optimization (I can see that only if I debug). I see two options :
option 1 : Create a listview with recycling strategy on. For better performance, set also the row height. One item of your listview would be one row. The advantage of a listview is the performance gain from the recycling strategy (especially on Android). Note that on iOS and Android, it might be better to set it on or not.
option 2 : Create a custom layout. That might be a little overkill.

How can an animation be added properly to a CALayer when using AVAssetExportSession in Xamarin.ios?

I need your help regarding an animation I want to apply to an overlay image I add to a mp4 video. To add an overlay on a video, I followed Ray Wenderlich tutorials here: https://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos. At the end of that link, an animation is created on an overlay.
Ray Wenderlich is not coding with c# but I do since I use Xamarin to create my app.
Using the animation, the overlay is supposed to fade away during the very first seconds of the video. But nothing happens. I mean nothing animated happens except for the video: the overlay is well put on top of the video but it does not fade away.
Most of the posts I read about animations and CALayer create an animation within a UIView.
So I am asking myself: is the animation not working because I use c# or did I write something wrong?
Can somebody help me with this, please?
This is the function I wrote:
public static void addFadeOverlayAtTheBeginning (NSUrl source, String overlay, long secondsToFade, NSUrl destination)
{
AVUrlAsset videoAsset = new AVUrlAsset(source);
if (secondsToFade > videoAsset.Duration.Seconds)
secondsToFade = (long)videoAsset.Duration.Seconds;
CALayer overlayLayer = CALayer.Create();
UIImage image = new UIImage(overlay);
image.CreateResizableImage(new UIEdgeInsets(0, 0, videoAsset.NaturalSize.Width, videoAsset.NaturalSize.Height));
overlayLayer.Contents = image.CGImage;
overlayLayer.Frame = new CGRect(0, 0, videoAsset.NaturalSize.Width, videoAsset.NaturalSize.Height);
overlayLayer.MasksToBounds = true;
CABasicAnimation animation = CABasicAnimation.FromKeyPath(#"opacity");
animation.BeginTime = 0; // GetSeconds(image.CGImage.StartTime);
animation.Duration = secondsToFade;
animation.SetFrom (NSNumber.FromFloat (1));
animation.SetTo (NSNumber.FromFloat(0));
overlayLayer.AddAnimation(animation, #"opacity");
AVMutableComposition videoComposition = AVMutableComposition.Create();
AVMutableCompositionTrack track = videoComposition.AddMutableTrack(AVMediaType.Video, 0);
AVAssetTrack sourceVideoTrack = videoAsset.TracksWithMediaType(AVMediaType.Video)[0];
CMTimeRange timeRangeInAsset = new CMTimeRange();
timeRangeInAsset.Start = CMTime.Zero;
timeRangeInAsset.Duration = videoAsset.Duration;
NSError videoInsertError = null;
track.InsertTimeRange(timeRangeInAsset, sourceVideoTrack, CMTime.Zero, out videoInsertError);
track.PreferredTransform = sourceVideoTrack.PreferredTransform;
CALayer parentLayer = CALayer.Create();
CALayer videoLayer = CALayer.Create();
parentLayer.Frame = new CGRect(0, 0, videoAsset.NaturalSize.Width, videoAsset.NaturalSize.Height);
videoLayer.Frame = new CGRect(0, 0, videoAsset.NaturalSize.Width, videoAsset.NaturalSize.Height);
parentLayer.AddSublayer(videoLayer);
parentLayer.AddSublayer(overlayLayer);
//parentLayer.AddAnimation(animation, "opacity");
AVMutableVideoComposition animationComposition = AVMutableVideoComposition.Create(); //videoComposition
animationComposition.AnimationTool = AVVideoCompositionCoreAnimationTool.FromLayer(videoLayer, parentLayer);
animationComposition.RenderSize = videoAsset.NaturalSize;
animationComposition.FrameDuration = new CMTime(1, 30);
AVMutableVideoCompositionInstruction instruction = AVMutableVideoCompositionInstruction.Create() as AVMutableVideoCompositionInstruction;
CMTimeRange timeRangeInstruction = new CMTimeRange();
timeRangeInstruction.Start = CMTime.Zero;
timeRangeInstruction.Duration = videoComposition.Duration;
instruction.TimeRange = timeRangeInstruction;
AVMutableVideoCompositionLayerInstruction layerInstruction = AVMutableVideoCompositionLayerInstruction.FromAssetTrack(track);
CMTimeRange timeRangeFading = new CMTimeRange();
timeRangeFading.Start = CMTime.Zero;
timeRangeFading.Duration = new CMTime(secondsToFade, 1);
//layerInstruction.SetOpacityRamp(1, 0, timeRangeFading);
instruction.LayerInstructions = new AVVideoCompositionLayerInstruction[] { layerInstruction };
List<AVVideoCompositionInstruction> instructions = new List<AVVideoCompositionInstruction>();
instructions.Add(instruction);
animationComposition.Instructions = instructions.ToArray();
if (File.Exists(destination.Path))
File.Delete(destination.Path);
AVAssetExportSession exporter = new AVAssetExportSession(videoComposition, AVAssetExportSession.PresetHighestQuality);
exporter.OutputUrl = destination;
exporter.VideoComposition = animationComposition;
exporter.OutputFileType = AVFileType.Mpeg4;
exporter.ExportAsynchronously(() => {
VideoManagement.state ++;
AVAssetExportSessionStatus status = exporter.Status;
Console.WriteLine("addFadeOverlayAtTheBeginning Done. Status: " + status.ToString());
switch (status)
{
case AVAssetExportSessionStatus.Completed:
Console.WriteLine("Sucessfully Completed");
if (File.Exists(destination.Path))
{
Console.WriteLine(String.Format("Saved to {0}", destination.Path));
}
else
Console.WriteLine("Failed");
break;
case AVAssetExportSessionStatus.Cancelled:
break;
case AVAssetExportSessionStatus.Exporting:
break;
case AVAssetExportSessionStatus.Failed:
Console.WriteLine("Task failed => {0}", exporter.Error);
Console.WriteLine(exporter.Error.Description);
break;
case AVAssetExportSessionStatus.Unknown:
break;
case AVAssetExportSessionStatus.Waiting:
break;
default:
break;
}
});
}
OK. Found it.
Apparently, animation.BeginTime = 0; is not supported. I changed it to 0.01 and everything works fine.
double Epsilon = 0.01;
if (secondsToFade > videoAsset.Duration.Seconds - Epsilon)
secondsToFade = videoAsset.Duration.Seconds - Epsilon;
CALayer overlayLayer = CALayer.Create();
UIImage image = new UIImage(overlay);
image.CreateResizableImage(new UIEdgeInsets(0, 0, videoAsset.NaturalSize.Width, videoAsset.NaturalSize.Height));
overlayLayer.Contents = image.CGImage;
overlayLayer.Frame = new CGRect(0, 0, videoAsset.NaturalSize.Width, videoAsset.NaturalSize.Height);
overlayLayer.MasksToBounds = true;
//On crée l'animation pour le CAlayer
CABasicAnimation animation = CABasicAnimation.FromKeyPath(#"opacity");
animation.BeginTime = Epsilon;
animation.Duration = secondsToFade;
animation.SetFrom (NSNumber.FromFloat (1));
animation.SetTo (NSNumber.FromFloat(0));
animation.FillMode = CAFillMode.Forwards;
animation.RemovedOnCompletion = false;

xamarin.ios load empty uicontrolview before detecting data from webservice

i'm developing an ios app with xamarin.
i created a button in this way:
var myBtn = new UIButton(UIButtonType.System);
myBtn.Frame = new CGRect(xImg, 1180, 200, 100);
myBtn.SetTitleColor(UIColor.Yellow, UIControlState.Normal);
myBtn.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
myBtn.TouchUpInside += delegate
{
NavController.PushViewController(new WebViewController(), false);
};
the WebViewController contains a webservice call, so it spends some seconds to detect data.
When i Tap on the button, the app calls the webservice first, then it changes the UiViewController. In this way I can't implement the Loading Overlay class
So i need that, when i tap the button, first it shows the empty UiViewController (with the LoadingOverlay), and then, when the webservice call is complete, show the remaining components.
This is the WebViewController:
public override void ViewDidLoad()
{
base.ViewDidLoad();
Title = "MyTitle";
var logo = new UIImageView(new CGRect(20, 50, sizes.Width - 40, 130));
logo.Image = UIImage.FromBundle("logo.jpg");
View.BackgroundColor = UIColor.Black;
scrollView = new UIScrollView(new CGRect(20, 180, UIScreen.MainScreen.Bounds.Width - 40, UIScreen.MainScreen.Bounds.Height - 180));
scrollView.BackgroundColor = UIColor.Black;
.........
List<UIImage> imagesList = getImagesList(urlG);
int count = 0;
List<UIImageView> imageViewList = new List<UIImageView>();
List<UIButton> btnList = new List<UIButton>();
nfloat imgS = getImgSizes();
nfloat imgY = 0;
......
for (int i = 0; i < imageViewList.Count; i++)
{
scrollView.AddSubview(imageViewList[i]);
scrollView.AddSubview(btnList[i]);
}
scrollView.ContentSize = new CGSize(UIScreen.MainScreen.Bounds.Width - 40, ((imagesList.Count / 4) * getImgSizes()) + 150);
View.AddSubview(logo);
Add(scrollView);
View.Add(loadPop);
.....
}
}

Categories