iOS for C# Developers
Miguel de Icaza – Xamarin Inc
@migueldeicaza
iOS Adoption
.NET Event Idiom
Objects raising a number
of events
MyControl
F.Clicked += EventHandler

OnEnter
OnLeave

Storage for
prop...
Objective-C Delegate Idiom
Objects use a peer object
to post notifications

MyControl

Delegate
Background

f.Delegate = n...
Objective-C Action/Target
• Poor man’s C# delegate.
• In .NET this is mapped to a C# delegate
– Use methods
– Anonymous me...
C# on iOS
• All the features you come to expect from C#
• IDEs:
– Visual Studio on Windows
– Xamarin Studio on Mac

• Thin...
APIs for C# Developers on iOS
• .NET’s Base Class Libraries
– mscorlib, System, System.Core, System.Data
– System.Web.Serv...
iOS APIs Surfaced to .NET
• Objective-C to C# bridge
– Special runtime support for these
– Integrates the Object Systems

...
iOS APIs for .NET
• iOS APIs are weakly typed
– Similar to .NET 1.0 code

• C# bindings are strongly typed
– Helps explore...
THE BASICS
Starting Up
• C# Main () method
– Call UIApplication.Main
– Pass the name of your application delegate class

• System cre...
Complete app
UIApplicationDelegate
• How the operating system talks to your app
• Mostly deals with state:
–
–
–
–
–

Starting up (fres...
Hierarchy
Screens
Windows
ViewControllers

Views or ViewControllers
UIScreen
• Represents a screen available in your device
• UIScreen.MainScreen is the main screen
– Same as UIScreen.Screen...
UIWindows
• Developers use one (system does others)
– Routes events
– Sets the Root View Controller

• UIWindow.RootViewCo...
UIView
• Base class for all UI
Elements

BackgroundColor = UIColor.Red
UIView Subclasses
UIButton

Simple UIViews

UIPicker

Controls
UISlider
UIControl
UISwitch
UIImageView
UIStepper
UILabel

...
UIViews are General Containers
Can be arbitrarily nested
UIView methods:
AddSubview (UISubview)
RemoveFromSuperview ()
Unl...
Can be Arbitrarily Nested
UIView’s
• Frame
– Superview coordinates
– RectangleF

• Bounds
– Size in UIView’s
coordinates

• Transform
– 2D Affine tr...
2D Affine Transforms

Rotation

Translation

Shearing

Scaling
UIView’s
• Frame
– Superview coordinates
– RectangleF

• Bounds
– Size in UIView’s
coordinates

• Transform
– 2D Affine tr...
UIView Center+Affine Transforms
• Frame
– Superview coordinates
– RectangleF

• Bounds
– Size in UIView’s
coordinates

• T...
Custom UIView - Rendering
• Override Draw (RectangleF region) method
– Must paint the entire requested region

• Obtain th...
Sample
public override void Draw (RectangleF rect)
{
using (CGContext context = UIGraphics.GetCurrentContext ()) {
// turn...
Custom UIViews – Touch Handling
• Configure properties:
– UserInteractionEnabled, MultipleTouchEnabled

• Override:
Touche...
Animation
• UIKit is powered by an animation framework
– Details, beyond the scope of today’s talk

• Certain properties c...
Animation + Async
• UIView.Animate methods
• Use AnimateNotifyAsync family of methods
– Duration
– Lambda to update visual...
Sample
Dynamics – new in iOS 7
• Introduces a physics engine into the UI
– Gravity
– Collision
– Attachment
– Snap
– Forces/Pushi...
Gravity + Collision Detection
UIViewController
• Typically a full screen of content
• Host for your views
– In charge of layout
– Orientation changes
– ...
Manually Creating your UI
Using a UI Designer
UIViewControllers in UIKit
UIActivityViewController

Built-in UI

UICollectionViewController

Building Blocks
UINavigation...
Built-in UI
UIActivityViewController

UIReferenceLibraryViewController
Other UIViewControllers
•

AddressBookUI

•

– ABNewPersonViewController
– ABPersonViewController
– ABUnknownPersonViewCon...
Some Examples
ABNewPersonViewController

EKEventViewController
Some Examples
TWTweetComposeViewController

MPMoviewPlayerViewController
UIViewControllers and Storyboards
• Name your class in the designer
– Will be reflected in your code

• Only after ViewDid...
Presenting View Controllers
• Given a current UIViewController, call:
– PresentViewController
– PresentViewControllerAsync...
UIControl
Common Controls
Strongly Typed Notifications
• NSNotificationCenter
– Application Message Bus
– Hub for posting messages
• “Keyboard will ...
Strongly Typed
THREE VIEWS TO MASTER
UIScrollView
• Where the magic originates
• Powerful control that handles scrolling
– Pagination, scrolling, smooth motion...
UITableView
• Most UI in iOS is a table
• Variable height
• External Data Source
– Request Section/Row
– Return UITableVie...
MonoTouch.Dialog – UITableView
made easy
• Switches the model
– From callback to fetch data (very scalable)
– To dump all ...
MonoTouch.Dialog sample
• Sample program
• Mimics Sounds Settings
UICollectionView
• Arbitrary collections
• In any form you want
–
–
–
–

Table, Grid
Baseball diamond
Circle
Or anything y...
More Resources
Wallace McClure’s talk
Mechanics of it - Right after this one
Islander IE
• http://developer.apple.com/ios
...
iOS for C# Developers - DevConnections Talk
Upcoming SlideShare
Loading in …5
×

iOS for C# Developers - DevConnections Talk

1,947 views

Published on

Crash-course for C# developers on getting started with C#/iOS development.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,947
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
12
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • Two state of the art platformsiOS only mainstream UI toolkit designed for the GPUCover the principles of iOS DevelopmentThe UIKit Framework
  • iOS for C# Developers - DevConnections Talk

    1. 1. iOS for C# Developers Miguel de Icaza – Xamarin Inc @migueldeicaza
    2. 2. iOS Adoption
    3. 3. .NET Event Idiom Objects raising a number of events MyControl F.Clicked += EventHandler OnEnter OnLeave Storage for properties/events: • either in object • Or uses bags, like DependencyProperties Clicked Background Font
    4. 4. Objective-C Delegate Idiom Objects use a peer object to post notifications MyControl Delegate Background f.Delegate = new myDelegate (); Must implement methods in MyDelegate Font MyControlDelegate OnEnter OnLeave Clicked
    5. 5. Objective-C Action/Target • Poor man’s C# delegate. • In .NET this is mapped to a C# delegate – Use methods – Anonymous methods – Lambdas
    6. 6. C# on iOS • All the features you come to expect from C# • IDEs: – Visual Studio on Windows – Xamarin Studio on Mac • Think of iOS as another platform to target – Just like you ASP.NET or WPF are – Same level of code sharing
    7. 7. APIs for C# Developers on iOS • .NET’s Base Class Libraries – mscorlib, System, System.Core, System.Data – System.Web.Services – etc • Native iOS APIs surfaced as C# classes – Mapped with some artistic liberties: – Follow .NET’s Framework Design Guidelines
    8. 8. iOS APIs Surfaced to .NET • Objective-C to C# bridge – Special runtime support for these – Integrates the Object Systems • Object Oriented C Code – Manually mapped to C# classes • Mostly CoreFoundation derived types • Regular C code – Mapped to C# classes as well
    9. 9. iOS APIs for .NET • iOS APIs are weakly typed – Similar to .NET 1.0 code • C# bindings are strongly typed – Helps explore the API – Let the IDE help you write your code • Async-ified (same rules as .NET async)
    10. 10. THE BASICS
    11. 11. Starting Up • C# Main () method – Call UIApplication.Main – Pass the name of your application delegate class • System creates UIApplication class – Instantiates your UIApplicationDelegate • UIApplicationDelegate methods invoked – FinishedLaunching performs UI setup
    12. 12. Complete app
    13. 13. UIApplicationDelegate • How the operating system talks to your app • Mostly deals with state: – – – – – Starting up (fresh, openUrl request) Suspending Resuming Respond to notifications Background downloads • Also: data security, UI orientation
    14. 14. Hierarchy Screens Windows ViewControllers Views or ViewControllers
    15. 15. UIScreen • Represents a screen available in your device • UIScreen.MainScreen is the main screen – Same as UIScreen.Screens [0] • Other screens used for external connectors
    16. 16. UIWindows • Developers use one (system does others) – Routes events – Sets the Root View Controller • UIWindow.RootViewController – Must be set by the end of running your FinishedLaunching method.
    17. 17. UIView • Base class for all UI Elements BackgroundColor = UIColor.Red
    18. 18. UIView Subclasses UIButton Simple UIViews UIPicker Controls UISlider UIControl UISwitch UIImageView UIStepper UILabel UIView UITextField UIProgressView UITableView UIScrollView UITextView UICollectionView 10,000 foot view, not comprehensive ScrollViews
    19. 19. UIViews are General Containers Can be arbitrarily nested UIView methods: AddSubview (UISubview) RemoveFromSuperview () Unlike Gtk/Winforms: Everything is a container
    20. 20. Can be Arbitrarily Nested
    21. 21. UIView’s • Frame – Superview coordinates – RectangleF • Bounds – Size in UIView’s coordinates • Transform – 2D Affine transformation • Center – Superview coordinates X, Y = (20,10) Center X, Y = (35,20) W,H=(50,30)
    22. 22. 2D Affine Transforms Rotation Translation Shearing Scaling
    23. 23. UIView’s • Frame – Superview coordinates – RectangleF • Bounds – Size in UIView’s coordinates • Transform – 2D Affine transformation • Center – Superview coordinates 45’ rotation over the center
    24. 24. UIView Center+Affine Transforms • Frame – Superview coordinates – RectangleF • Bounds – Size in UIView’s coordinates • Transform – 2D Affine transformation • Center – Superview coordinates
    25. 25. Custom UIView - Rendering • Override Draw (RectangleF region) method – Must paint the entire requested region • Obtain the UIGraphics’ current draw context • CGContext: Immediate graphics API – Similar to System.Drawing on Windows.Forms – Not retained, like Silverlight
    26. 26. Sample public override void Draw (RectangleF rect) { using (CGContext context = UIGraphics.GetCurrentContext ()) { // turn on anti-aliasing context.SetAllowsAntialiasing (true); // loop through each spot and draw it foreach (Spot s in touchSpots) { context.SetFillColor (s.Red, s.Green, s.Blue, s.Alpha); context.FillEllipseInRect (new RectangleF (s.Point,size)); } } }
    27. 27. Custom UIViews – Touch Handling • Configure properties: – UserInteractionEnabled, MultipleTouchEnabled • Override: TouchesBegan: – User touched the UI TouchesMoved – Updated locations TouchesEnded – User lifted fingers TouchesCancelled – System cancelled (for example, incoming call)
    28. 28. Animation • UIKit is powered by an animation framework – Details, beyond the scope of today’s talk • Certain properties can be animated: – Frame, Bounds, Center, Transform – Alpha, BackgroundColor, ContentStretch • Very little setup needed
    29. 29. Animation + Async • UIView.Animate methods • Use AnimateNotifyAsync family of methods – Duration – Lambda to update visual properties – Options controlling animation • Await on the call – Will resume execution after animation completes
    30. 30. Sample
    31. 31. Dynamics – new in iOS 7 • Introduces a physics engine into the UI – Gravity – Collision – Attachment – Snap – Forces/Pushing
    32. 32. Gravity + Collision Detection
    33. 33. UIViewController • Typically a full screen of content • Host for your views – In charge of layout – Orientation changes – Provides event routing for your views • Some can host other UIViewControllers – “UIViewController Containment” • UIViewController.View property is the root UIView
    34. 34. Manually Creating your UI
    35. 35. Using a UI Designer
    36. 36. UIViewControllers in UIKit UIActivityViewController Built-in UI UICollectionViewController Building Blocks UINavigationViewController UIPageViewController UIViewController UIReferenceLibraryViewController UITabBarViewController UITableViewController UISplitVuewController UIViewControllers in UIKit.
    37. 37. Built-in UI UIActivityViewController UIReferenceLibraryViewController
    38. 38. Other UIViewControllers • AddressBookUI • – ABNewPersonViewController – ABPersonViewController – ABUnknownPersonViewController • – MCBrowserViewController – MCPeerPickerViewController • EventKitUI – EKCalendarChooser – EKEventViewController MultipeerConnectivity PassKit – PKAddPassesViewController • QuickLook – QLPreviewController • GLKit – GLKViewController • • Social/Twitter – SLComposeViewController – TWTweetComposeViewController GameKit – GKMatchmakerViewController • • MediaPlayer – MPMediaPickerController – MPMoviePlayerViewController. StoreKit – SKStoreProductViewController
    39. 39. Some Examples ABNewPersonViewController EKEventViewController
    40. 40. Some Examples TWTweetComposeViewController MPMoviewPlayerViewController
    41. 41. UIViewControllers and Storyboards • Name your class in the designer – Will be reflected in your code • Only after ViewDidLoad () are objects created – Any references to other views or controllers – Wont be valid until after this method is called
    42. 42. Presenting View Controllers • Given a current UIViewController, call: – PresentViewController – PresentViewControllerAsync • Modality of controller: – bool ModalViewController {get;set} • You can build your own visual transition – And control every step of it
    43. 43. UIControl
    44. 44. Common Controls
    45. 45. Strongly Typed Notifications • NSNotificationCenter – Application Message Bus – Hub for posting messages • “Keyboard will appear” • “Font size changed” • Strongly typed in C# – Class.Notification.ObserveXXXX ()
    46. 46. Strongly Typed
    47. 47. THREE VIEWS TO MASTER
    48. 48. UIScrollView • Where the magic originates • Powerful control that handles scrolling – Pagination, scrolling, smooth motion • Must see: Series of WWDC talks on it – Every possible trick and hack
    49. 49. UITableView • Most UI in iOS is a table • Variable height • External Data Source – Request Section/Row – Return UITableViewCell • External Delegate • Powerful • And Cumbersome
    50. 50. MonoTouch.Dialog – UITableView made easy • Switches the model – From callback to fetch data (very scalable) – To dump all data into view (easy, not scalable) • Elements: – Provide cell-specific style renderers
    51. 51. MonoTouch.Dialog sample • Sample program • Mimics Sounds Settings
    52. 52. UICollectionView • Arbitrary collections • In any form you want – – – – Table, Grid Baseball diamond Circle Or anything you can provide rules for
    53. 53. More Resources Wallace McClure’s talk Mechanics of it - Right after this one Islander IE • http://developer.apple.com/ios • Apple’s site • http://docs.xamarin.com/ios – Docs, tutorials on C# and iOS • http://planet.xamarin.com – Technical Blogs from the community

    ×