SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
iOS:	
  Custom	
  Views	
  

     Jussi	
  Pohjolainen	
  
About	
  Views	
  
•  UIBu;on,	
  UILabel,	
  all	
  these	
  controls	
  are	
  views	
  
•  Views	
  is	
  an	
  
       –  Instance	
  of	
  UIView	
  or	
  one	
  of	
  its	
  subclasses	
  
       –  Can	
  be	
  drawn	
  on	
  applica-ons’	
  window	
  
       –  Exists	
  in	
  hierarchy	
  of	
  views,	
  root	
  is	
  app	
  window	
  
       –  Can	
  handle	
  events	
  
	
  
AppDelegate	
  




Creates	
  the	
  window,	
  puts	
  it	
  to	
  screen.	
  
You	
  can	
  add	
  other	
  views	
  to	
  window!	
  
CreaKng	
  Custom	
  View	
  
•    Subclass	
  UIView	
  
•    Allocate	
  and	
  iniKalize	
  the	
  UIView	
  
•    Pass	
  View	
  Frame	
  to	
  UIView	
  
•    Add	
  the	
  view	
  as	
  subview	
  of	
  Window	
  
Subview	
  inside	
  of	
  Subview	
  
 MyCustomView* view =
[[MyCustomView alloc]
initWithFrame: viewFrame];
 MyCustomView* view2 =
[[MyCustomView alloc]
initWithFrame: viewFrame2];
 [view addSubview:view2];
drawRect	
  
•  Override	
  drawRect	
  –	
  method	
  in	
  custom	
  view	
  
•  Default:	
  does	
  not	
  do	
  anything	
  
Redraw	
  and	
  Events	
  
•  To	
  redraw	
  a	
  view,	
  call	
  method	
  
   setNeedsDisplay	
  
    –  [view setNeedsDisplay]
•  To	
  receive	
  events	
  
    –  1)	
  Say	
  that	
  your	
  custom	
  view	
  can	
  do	
  that	
  
         •  Return	
  YES	
  in	
  method	
  - (BOOL)
            canBecomeFirstResponder
    –  2)	
  Say	
  to	
  the	
  view	
  that	
  you	
  are	
  the	
  one	
  to	
  receive	
  
       events	
  
         •  [view becomeFirstResponder];
MoKon	
  Event	
  to	
  Custom	
  View	
  
•  Override	
  moKonX	
  methods	
  in	
  custom	
  view	
  
   –  moKonBegan	
  
   –  moKonEnded	
  
   –  moKonCancelled	
  
Custom	
  View	
  Shake	
  
- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(motion == UIEventSubtypeMotionShake)
    {
        // New color
        [self setCircleColor:[UIColor redColor]];

        // Refresh!
        [self setNeedsDisplay];
    }
}
And	
  set	
  the	
  View	
  to	
  receive	
  events	
  
// Create the view with given size
MyCustomView* view = [[MyCustomView alloc] initWithFrame:
viewFrame];

// Set view to become the one who is receiving events!
[view becomeFirstResponder];
Scrolling	
  
•  Set	
  view	
  larger	
  than	
  screen	
  
•  Draw	
  a	
  porKon	
  of	
  that	
  view	
  
    –  Viewing	
  port	
  that	
  you	
  move	
  around	
  	
  
•  UIScrollView	
  
    –  contentSize	
  –	
  the	
  size	
  of	
  the	
  area	
  that	
  is	
  visible	
  
UIScrollView	
  Example	
  
UIScrollView	
  Example	
  
Panning	
  and	
  Paging	
  
Example	
  
Hide	
  Status	
  Bar	
  
•  To	
  hide	
  status	
  bar	
  either:	
  
    –  [[UIApplication sharedApplication]
      setStatusBarHidden:YES
      withAnimation:UIStatusBarAnimationFade];
•  Or	
  in	
  info	
  property	
  list	
  of	
  your	
  project	
  

Weitere ähnliche Inhalte

Ähnlich wie iOS: Implementing a Custom View

Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View ControllersBob McCune
 
Android Custom Views
Android Custom ViewsAndroid Custom Views
Android Custom ViewsBabar Sanah
 
Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨foxgem
 
December 2014 University iOS Meetup Talk
December 2014 University iOS Meetup TalkDecember 2014 University iOS Meetup Talk
December 2014 University iOS Meetup Talkjcgohlke
 
Desenvolvimento iOS - Aula 4
Desenvolvimento iOS - Aula 4Desenvolvimento iOS - Aula 4
Desenvolvimento iOS - Aula 4Saulo Arruda
 
Quick Start to iOS Development
Quick Start to iOS DevelopmentQuick Start to iOS Development
Quick Start to iOS DevelopmentJussi Pohjolainen
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkMiguel de Icaza
 
iOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI ComponentsiOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI ComponentsAsim Rais Siddiqui
 
Developing for Apple TV
Developing for Apple TVDeveloping for Apple TV
Developing for Apple TValekseyn
 
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III) Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III) Mobivery
 
iOS Contact List Application Tutorial
iOS Contact List Application TutorialiOS Contact List Application Tutorial
iOS Contact List Application TutorialIshara Amarasekera
 
Custom cell in objective c
Custom cell in objective cCustom cell in objective c
Custom cell in objective cVishal Verma
 
Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...
Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...
Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...Aaron Saunders
 
Converting Your Mobile App to the Mobile Cloud
Converting Your Mobile App to the Mobile CloudConverting Your Mobile App to the Mobile Cloud
Converting Your Mobile App to the Mobile CloudRoger Brinkley
 

Ähnlich wie iOS: Implementing a Custom View (20)

Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View Controllers
 
Android Custom Views
Android Custom ViewsAndroid Custom Views
Android Custom Views
 
iphonedevcon 2010: Cooking with iAd
iphonedevcon 2010:  Cooking with iAd iphonedevcon 2010:  Cooking with iAd
iphonedevcon 2010: Cooking with iAd
 
iOS
iOSiOS
iOS
 
iOS Training Session-3
iOS Training Session-3iOS Training Session-3
iOS Training Session-3
 
Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨
 
December 2014 University iOS Meetup Talk
December 2014 University iOS Meetup TalkDecember 2014 University iOS Meetup Talk
December 2014 University iOS Meetup Talk
 
IOS APPs Revision
IOS APPs RevisionIOS APPs Revision
IOS APPs Revision
 
Desenvolvimento iOS - Aula 4
Desenvolvimento iOS - Aula 4Desenvolvimento iOS - Aula 4
Desenvolvimento iOS - Aula 4
 
Quick Start to iOS Development
Quick Start to iOS DevelopmentQuick Start to iOS Development
Quick Start to iOS Development
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections Talk
 
iOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI ComponentsiOS Development (Part 3) - Additional GUI Components
iOS Development (Part 3) - Additional GUI Components
 
Koin
KoinKoin
Koin
 
Developing for Apple TV
Developing for Apple TVDeveloping for Apple TV
Developing for Apple TV
 
Diving Into Xamarin.Forms
Diving Into Xamarin.Forms Diving Into Xamarin.Forms
Diving Into Xamarin.Forms
 
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III) Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
 
iOS Contact List Application Tutorial
iOS Contact List Application TutorialiOS Contact List Application Tutorial
iOS Contact List Application Tutorial
 
Custom cell in objective c
Custom cell in objective cCustom cell in objective c
Custom cell in objective c
 
Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...
Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...
Getting Started with Appcelerator Alloy - Cross Platform Mobile Development -...
 
Converting Your Mobile App to the Mobile Cloud
Converting Your Mobile App to the Mobile CloudConverting Your Mobile App to the Mobile Cloud
Converting Your Mobile App to the Mobile Cloud
 

Mehr von Jussi Pohjolainen

libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferencesJussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDXJussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript DevelopmentJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDXJussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 

Mehr von Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 

iOS: Implementing a Custom View

  • 1. iOS:  Custom  Views   Jussi  Pohjolainen  
  • 2. About  Views   •  UIBu;on,  UILabel,  all  these  controls  are  views   •  Views  is  an   –  Instance  of  UIView  or  one  of  its  subclasses   –  Can  be  drawn  on  applica-ons’  window   –  Exists  in  hierarchy  of  views,  root  is  app  window   –  Can  handle  events    
  • 3. AppDelegate   Creates  the  window,  puts  it  to  screen.   You  can  add  other  views  to  window!  
  • 4. CreaKng  Custom  View   •  Subclass  UIView   •  Allocate  and  iniKalize  the  UIView   •  Pass  View  Frame  to  UIView   •  Add  the  view  as  subview  of  Window  
  • 5.
  • 6. Subview  inside  of  Subview   MyCustomView* view = [[MyCustomView alloc] initWithFrame: viewFrame]; MyCustomView* view2 = [[MyCustomView alloc] initWithFrame: viewFrame2]; [view addSubview:view2];
  • 7. drawRect   •  Override  drawRect  –  method  in  custom  view   •  Default:  does  not  do  anything  
  • 8.
  • 9.
  • 10.
  • 11. Redraw  and  Events   •  To  redraw  a  view,  call  method   setNeedsDisplay   –  [view setNeedsDisplay] •  To  receive  events   –  1)  Say  that  your  custom  view  can  do  that   •  Return  YES  in  method  - (BOOL) canBecomeFirstResponder –  2)  Say  to  the  view  that  you  are  the  one  to  receive   events   •  [view becomeFirstResponder];
  • 12. MoKon  Event  to  Custom  View   •  Override  moKonX  methods  in  custom  view   –  moKonBegan   –  moKonEnded   –  moKonCancelled  
  • 13. Custom  View  Shake   - (BOOL) canBecomeFirstResponder { return YES; } - (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(motion == UIEventSubtypeMotionShake) { // New color [self setCircleColor:[UIColor redColor]]; // Refresh! [self setNeedsDisplay]; } }
  • 14. And  set  the  View  to  receive  events   // Create the view with given size MyCustomView* view = [[MyCustomView alloc] initWithFrame: viewFrame]; // Set view to become the one who is receiving events! [view becomeFirstResponder];
  • 15. Scrolling   •  Set  view  larger  than  screen   •  Draw  a  porKon  of  that  view   –  Viewing  port  that  you  move  around     •  UIScrollView   –  contentSize  –  the  size  of  the  area  that  is  visible  
  • 20. Hide  Status  Bar   •  To  hide  status  bar  either:   –  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; •  Or  in  info  property  list  of  your  project