I recently collaborated on a book on MonoTouch released by Wrox. You can purchase it over on Amazon. It contains a great introduction to
MonoTouch and how to use many common features of MonoTouch including building applications for the iPad.
var myString = "MyString";
var myFloat = 4.56f;
var myInt = 5;
Console.WriteLine(String.Format("log: {0}", myString));
Console.WriteLine(String.Format("log: {0}", myFloat));
Console.WriteLine(String.Format("log: {0}", myInt));
var imageRect = new RectangleF(0f, 0f, 320f, 109f);
using (var myImage = new UIImageView(imageRect))
{
myImage.Image = UIImage.FromFile("myImage.png");
myImage.Opaque = true;
view.AddSubview(myImage);
}
var webRect = new RectangleF(0f, 0f, 320f, 460f);
using (var webView = new UIWebView(webRect))
{
webView.BackgroundColor = UIColor.White;
var urlAddress = "http://www.google.com";
var url = new NSUrl(urlAddress);
var urlRequest = new NSUrlRequest(url);
webView.LoadRequest(urlRequest);
view.AddSubview(webView);
}
var app = UIApplication.SharedApplication; app.NetworkActivityIndicatorVisible = true; // Set True to display, False to hide.
List<UIImage> myImages = new List<UIImage>();
myImages.Add(UIImage.FromFile("myImage1.png"));
myImages.Add(UIImage.FromFile("myImage2.png"));
myImages.Add(UIImage.FromFile("myImage3.png"));
myImages.Add(UIImage.FromFile("myImage4.png"));
var myAnimatedView = new UIImageView(view.Bounds);
myAnimatedView.AnimationImages = myImages.ToArray();
myAnimatedView.AnimationDuration = 1.75; // Seconds
myAnimatedView.AnimationRepeatCount = 0; // 0 = Loops Forever
myAnimatedView.StartAnimating();
view.AddSubview(myAnimatedView);
var theAnimation = CABasicAnimation.FromKeyPath("transform.translation.x");
theAnimation.Duration = 1;
theAnimation.From = NSNumber.FromFloat(0f);
theAnimation.To = NSNumber.FromFloat(-60f);
exampleLabel.Layer.AddAnimation(theAnimation, "animateLabel");
var currentInt = 5;
exampleLabel.Text = String.Format("Int = {0}", currentInt);
var emailRegex = @"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
var realEmail = "my@email.com";
var fakeEmail = "notAEmailAddress.com";
var formatString = "{0} is a real e-mail = {1}";
Console.WriteLine ((String.Format(formatString, realEmail, Regex.IsMatch(realEmail, emailRegex))));
Console.WriteLine ((String.Format(formatString, fakeEmail, Regex.IsMatch(fakeEmail, emailRegex))));
public class MyDraggableImage : UIImageView
{
public MyDraggableImage(RectangleF frame) : base(frame)
{
}
public PointF StartLocation { get; set; }
}
2. In the implementation for this new class, add the 3 methods:
public override void TouchesBegan (NSSet touches, UIEvent evt)
{
var pt = (touches.AnyObject as UITouch).LocationInView(this);
StartLocation = pt;
this.Superview.BringSubviewToFront(this);
}
public override void TouchesMoved (NSSet touches, UIEvent evt)
{
// Move relative to the original touch point
var pt = (touches.AnyObject as UITouch).LocationInView(this);
var frame = this.Frame;
frame.X += pt.X - StartLocation.X;
frame.Y += pt.Y - StartLocation.Y;
this.Frame = frame;
}
public override void TouchesEnded (NSSet touches, UIEvent evt)
{
// Move relative to the original touch point
var pt = (touches.AnyObject as UITouch).LocationInView(this);
var frame = this.Frame;
frame.X += pt.X - StartLocation.X;
frame.Y += pt.Y - StartLocation.Y;
this.Frame = frame;
}
3. Now instantiate the new class as you would any other new image and add it to your view.
var image = UIImage.FromFile("myImage.png");
var draggableRect = new RectangleF(0f, 0f, image.Size.Width, image.Size.Height);
var dragger = new MyDraggableImage(draggableRect);
dragger.Image = image;
dragger.UserInteractionEnabled = true;
view.AddSubview(dragger);
SystemSound.Vibrate.PlaySystemSound();Sound will work in the Simulator, however some sound (such as looped) has been reported as not working in Simulator or even altogether depending on the audio format. Note there are specific filetypes that must be used (.caf in this example).
var sound = new SystemSound(new NSUrl("audioFile.caf"));
sound.PlaySystemSound();
var thread = new Thread(NewThreadMethod as ThreadStart); thread.Start();2. Create the method that is called by the new thread:
[Export("NewThreadMethod")]
void NewThreadMethod()
{
using(var pool = new NSAutoreleasePool())
{
// Run code in new thread
}
}
What if you need to do something to the main thread from inside your new thread (for example, show a loading symbol)? Use InvokeOnMainThread.
InvokeOnMainThread( delegate {
// Update UI Code (in Main thread) from the new thread.
});
var appDelegate = (AppDelegate) UIApplication.SharedApplication.Delegate; appDelegate.view.BackgroundColor = UIColor.Green;
var timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1), OneSecondTimer);What if you need to pass an object to OneSecondTimer? Use the "userInfo" property.
var userInfo = new NSString("MyUserInfo");
var timer = NSTimer.CreateScheduledTimer(1, this, new Selector("OneSecondTimer"), userInfo, true);
2. Then pass the NSTimer object to your method:
[Export("OneSecondTimer")]
void OneSecondTimer(NSTimer timer)
{
var userInfoString = timer.UserInfo.ToString();
}
To stop a timer, use "invalidate":
timer.Invalidate(); timer = null;
var currentTime = DateTime.UtcNow;
Console.WriteLine ("Date/Time: " + currentTime);
using(var alert = new UIAlertView("Title", "Message", null, "OK", null))
{
alert.Show();
}
var plist = NSUserDefaults.StandardUserDefaults;
var myKey = plist["myKey"];
// Or
var myKey2 = plist.StringForKey("myKey2");
var myBoolKey = plist.BoolForKey("myBoolKey");
var myIntKey = plist.IntForKey("myIntKey");
plist.SetString("Save this string", "myStringKey");
// Saves the new "myStringKey" string.
plist.Synchronize();
var f = infoButton.Frame; var newInfoButtonRectangle = new RectangleF(f.X - 25f, f.Y - 25f, f.Width + 50f, f.Height + 50f); infoButton.Frame = newInfoButtonRectangle;
foreach(var view in this.window.Subviews)
{
Console.WriteLine ("View" + view.ToString());
foreach(var subView in this.view.Subviews)
{
Console.WriteLine ("SubView: " + subView.ToString());
}
}