iOS for C# Developers - DevConnections Talk

54
iOS for C# Developers Miguel de Icaza – Xamarin Inc @migueldeicaza

description

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

Transcript of iOS for C# Developers - DevConnections Talk

Page 1: iOS for C# Developers - DevConnections Talk

iOS for C# Developers

Miguel de Icaza – Xamarin Inc@migueldeicaza

Page 2: iOS for C# Developers - DevConnections Talk

iOS Adoption

Page 3: iOS for C# Developers - DevConnections Talk

.NET Event Idiom

Objects raising a number of events

F.Clicked += EventHandler

Storage for properties/events:

• either in object• Or uses bags, like

DependencyProperties

MyControl

OnEnter

OnLeave

Clicked

Background

Font

Page 4: iOS for C# Developers - DevConnections Talk

MyControlDelegate

Objective-C Delegate Idiom

Objects use a peer object to post notifications

f.Delegate = new myDelegate ();

Must implement methods in MyDelegate

MyControl

OnEnter

OnLeave

Clicked

Background

Font

Delegate

Page 5: iOS for C# Developers - DevConnections Talk

Objective-C Action/Target

• Poor man’s C# delegate.

• In .NET this is mapped to a C# delegate– Use methods– Anonymous methods– Lambdas

Page 6: iOS for C# Developers - DevConnections Talk

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

Page 7: iOS for C# Developers - DevConnections Talk

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

Page 8: iOS for C# Developers - DevConnections Talk

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

Page 9: iOS for C# Developers - DevConnections Talk

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)

Page 10: iOS for C# Developers - DevConnections Talk

THE BASICS

Page 11: iOS for C# Developers - DevConnections Talk

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

Page 12: iOS for C# Developers - DevConnections Talk

Complete app

Page 13: iOS for C# Developers - DevConnections Talk

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

Page 14: iOS for C# Developers - DevConnections Talk

Hierarchy

Screens

Windows

ViewControllers

Views or ViewControllers

Page 15: iOS for C# Developers - DevConnections Talk

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

Page 16: iOS for C# Developers - DevConnections Talk

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.

Page 17: iOS for C# Developers - DevConnections Talk

UIView

• Base class for all UI Elements

BackgroundColor = UIColor.Red

Page 18: iOS for C# Developers - DevConnections Talk

UIView Subclasses

UIView

UIControl

UIButton

UIPicker

UISlider

UISwitch

UIStepper

UITextField

UIImageView

UILabel

UIProgressView

UIScrollView

UITableView

UITextView

UICollectionView10,000 foot view, not comprehensive

Simple UIViews

Controls

ScrollViews

Page 19: iOS for C# Developers - DevConnections Talk

UIViews are General Containers

Can be arbitrarily nested

UIView methods:AddSubview (UISubview)RemoveFromSuperview ()

Unlike Gtk/Winforms: Everything is a container

Page 20: iOS for C# Developers - DevConnections Talk

Can be Arbitrarily Nested

Page 21: iOS for C# Developers - DevConnections Talk

UIView’s

• Frame– Superview coordinates– RectangleF

• Bounds– Size in UIView’s

coordinates

• Transform– 2D Affine transformation

• Center– Superview coordinates

X, Y = (20,10)

W,H=(50,30)

Center X, Y = (35,20)

Page 22: iOS for C# Developers - DevConnections Talk

2D Affine Transforms

Rotation Translation Shearing Scaling

Page 23: iOS for C# Developers - DevConnections Talk

UIView’s

• Frame– Superview coordinates– RectangleF

• Bounds– Size in UIView’s

coordinates

• Transform– 2D Affine transformation

• Center– Superview coordinates

45’ rotation over the center

Page 24: iOS for C# Developers - DevConnections Talk

UIView Center+Affine Transforms

• Frame– Superview coordinates– RectangleF

• Bounds– Size in UIView’s

coordinates

• Transform– 2D Affine transformation

• Center– Superview coordinates

Page 25: iOS for C# Developers - DevConnections Talk

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

Page 26: iOS for C# Developers - DevConnections Talk

Samplepublic 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)); } }}

Page 27: iOS for C# Developers - DevConnections Talk

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)

Page 28: iOS for C# Developers - DevConnections Talk

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

Page 29: iOS for C# Developers - DevConnections Talk

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

Page 30: iOS for C# Developers - DevConnections Talk

Sample

Page 31: iOS for C# Developers - DevConnections Talk

Dynamics – new in iOS 7

• Introduces a physics engine into the UI – Gravity– Collision– Attachment– Snap– Forces/Pushing

Page 32: iOS for C# Developers - DevConnections Talk

Gravity + Collision Detection

Page 33: iOS for C# Developers - DevConnections Talk

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

Page 34: iOS for C# Developers - DevConnections Talk

Manually Creating your UI

Page 35: iOS for C# Developers - DevConnections Talk

Using a UI Designer

Page 36: iOS for C# Developers - DevConnections Talk

UIViewControllers in UIKit

UIViewControllers in UIKit.

UIViewController

UIActivityViewController

UICollectionViewController

UINavigationViewController

UIPageViewController

UIReferenceLibraryViewController

UITabBarViewController

UITableViewController

UISplitVuewController

Built-in UI

Building Blocks

Page 37: iOS for C# Developers - DevConnections Talk

Built-in UI

UIActivityViewController UIReferenceLibraryViewController

Page 38: iOS for C# Developers - DevConnections Talk

Other UIViewControllers• AddressBookUI

– ABNewPersonViewController– ABPersonViewController– ABUnknownPersonViewController

• EventKitUI– EKCalendarChooser– EKEventViewController

• GLKit– GLKViewController

• GameKit– GKMatchmakerViewController

• MediaPlayer– MPMediaPickerController– MPMoviePlayerViewController.

• MultipeerConnectivity– MCBrowserViewController– MCPeerPickerViewController

• PassKit– PKAddPassesViewController

• QuickLook– QLPreviewController

• Social/Twitter– SLComposeViewController– TWTweetComposeViewController

• StoreKit– SKStoreProductViewController

Page 39: iOS for C# Developers - DevConnections Talk

Some Examples

ABNewPersonViewController EKEventViewController

Page 40: iOS for C# Developers - DevConnections Talk

Some Examples

TWTweetComposeViewController MPMoviewPlayerViewController

Page 41: iOS for C# Developers - DevConnections Talk

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

Page 42: iOS for C# Developers - DevConnections Talk

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

Page 43: iOS for C# Developers - DevConnections Talk

UIControl

Page 44: iOS for C# Developers - DevConnections Talk

Common Controls

Page 45: iOS for C# Developers - DevConnections Talk

Strongly Typed Notifications

• NSNotificationCenter– Application Message Bus– Hub for posting messages• “Keyboard will appear”• “Font size changed”

• Strongly typed in C#– Class.Notification.ObserveXXXX ()

Page 46: iOS for C# Developers - DevConnections Talk

Strongly Typed

Page 47: iOS for C# Developers - DevConnections Talk

THREE VIEWS TO MASTER

Page 48: iOS for C# Developers - DevConnections Talk

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

Page 49: iOS for C# Developers - DevConnections Talk

UITableView

• Most UI in iOS is a table• Variable height• External Data Source– Request Section/Row– Return UITableViewCell

• External Delegate

• Powerful• And Cumbersome

Page 50: iOS for C# Developers - DevConnections Talk

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

Page 51: iOS for C# Developers - DevConnections Talk

MonoTouch.Dialog sample

• Sample program• Mimics Sounds Settings

Page 52: iOS for C# Developers - DevConnections Talk
Page 53: iOS for C# Developers - DevConnections Talk

UICollectionView

• Arbitrary collections• In any form you want– Table, Grid– Baseball diamond– Circle– Or anything you can

provide rules for

Page 54: iOS for C# Developers - DevConnections Talk

More Resources

Wallace McClure’s talkMechanics of it - Right after this oneIslander 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