Skip to content

Instantly share code, notes, and snippets.

@e23z
e23z / playPause.cs
Created July 26, 2017 14:37
[Play/Pause Core Animations] How to play and pause core animations in iOS. #xamarin #mobile #animation #ios
void PauseAnimation()
{
var pausedTime = Layer.ConvertTimeFromLayer(CAAnimation.CurrentMediaTime(), null);
Layer.Speed = 0.0f;
Layer.TimeOffset = pausedTime;
}
void ResumeAnimation()
{
var pausedTime = Layer.TimeOffset;
@e23z
e23z / animation.swift
Created July 26, 2017 14:35
[Reversing Animations] Example on how to reverse animations. #swift #mobile #ios #animation
// first, separate your drawing code from your animation code.
// this way you can call animations without instantiating new objects
func drawLine() {
let path = UIBezierPath()
// draw your path with no position translation.. move the layer
path.moveToPoint(CGPointMake(view.bounds.width, 0))
path.addLineToPoint(CGPointMake(0, 0))
pathLayer.frame = path.bounds
// this line sets the position of the layer appropriately
pathLayer.position = view.bounds.width - pathLayer.bounds.width / 2
@e23z
e23z / nsdate.cs
Created July 26, 2017 14:33
[NSDate Convertions] How to convert NSDate do DateTime and vice-versa. #xamarin #ios #utils #function
public DateTime ConvertNsDateToDateTime(NSDate date)
{
DateTime newDate = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(2001, 1, 1, 0, 0, 0));
return newDate.AddSeconds(date.SecondsSinceReferenceDate);
}
public NSDate ConvertDateTimeToNSDate(DateTime date)
{
DateTime newDate = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(2001, 1, 1, 0, 0, 0));
return NSDate.FromTimeIntervalSinceReferenceDate((date - newDate).TotalSeconds);
@e23z
e23z / androidId.cs
Created July 26, 2017 14:30
[Unique Device ID] How to get an unique device id to use for some purpose in a mobile app. #xamarin #ios #android #wp #mobile
// --------------------------------------
// ANDROID
// --------------------------------------
var id = Android.OS.Build.Serial;
@e23z
e23z / connection.cs
Created July 26, 2017 14:27
[Check Internet Connection] How to make sure the device has internet connection before executing some action or to display alerts. #swift #xamarin #mobile #ios #ux #internet
CrossConnectivity.Current.ConnectivityChanged += async (sender, args) =>
{
try
{
GlobalVariables.Instance.IsNetworkReachable = args.IsConnected;
if (GlobalVariables.Instance.IsNetworkReachable)
{
// stuff here that needs to be done when connectivity restored
// (e.g. updating list of purchases for in-app purchases/billing
}
@e23z
e23z / vibrate.cs
Last active July 26, 2017 14:30
[Device Vibration] How to make the mobile device to vibrate for some action. #xamarin #ios #android #mobile #wp #ux
// Android
Vibrator vibrator = (Vibrator) this.ApplicationContext.GetSystemService(Context.VibratorService);
// iOS
SystemSound.Vibrate.PlayAlertSound();
// Windows Phone
VibrateController testVibrateController = VibrateController.Default;
vibrate.Start(TimeSpan.FromMilliseconds(1000));
@e23z
e23z / moveKeyboard.cs
Created July 26, 2017 14:22
[Move Screen on Keyboard Show] How to move the screen to keep the input visible when the keyboard opens. #xamarin #ui #ux #ios
// listen to keyboards events
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
// reposition the view
void OnKeyboardNotification(NSNotification notification) {
var visible = notification.Name == UIKeyboard.WillShowNotification;
var location = _origin.Location;
if (visible) {
var keyboardFrame = UIKeyboard.FrameEndFromNotification(notification);
@e23z
e23z / share.cs
Created July 26, 2017 14:18
[Whatsapp Sharing] How to share content from a mobile app with whatsapp. #swift #mobile #ios #xamarin #sharing
var text = new NSString(_view.Text.Value).CreateStringByAddingPercentEncoding(NSUrlUtilities_NSCharacterSet.UrlHostAllowedCharacterSet);
var url = new NSUrl($"whatsapp://send?text={text}");
if (UIApplication.SharedApplication.CanOpenUrl(url)) {
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) {
UIApplication.SharedApplication.OpenUrl(url, new UIApplicationOpenUrlOptions(), null);
} else {
UIApplication.SharedApplication.OpenUrl(url);
}
} else {
// Error
@e23z
e23z / pullToRefresh.swift
Created July 26, 2017 13:04
[Pull to Refresh (UIRefreshControl)] How to add "pull to refresh" to a table view in iOS. #mobile #swift #ios #ui #ux
var refreshControl: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl) // not required when using UITableViewController
}
@e23z
e23z / customAttr.cs
Created July 26, 2017 12:50
[Custom Attribute Value] How to get a custom attribute value. #csharp #reflection
var typeInfo = GetType ().GetTypeInfo ();
var myAttr = typeInfo.CustomAttributes.FirstOrDefault (_ => _.AttributeType == typeof (MyAttribute));
Value = myAttr?.ConstructorArguments.First ().Value as string;