iOS: Implementing a Custom View

7,831 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
7,831
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
38
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

iOS: Implementing a Custom View

  1. 1. iOS:  Custom  Views   Jussi  Pohjolainen  
  2. 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. 3. AppDelegate  Creates  the  window,  puts  it  to  screen.  You  can  add  other  views  to  window!  
  4. 4. CreaKng  Custom  View  •  Subclass  UIView  •  Allocate  and  iniKalize  the  UIView  •  Pass  View  Frame  to  UIView  •  Add  the  view  as  subview  of  Window  
  5. 5. Subview  inside  of  Subview   MyCustomView* view =[[MyCustomView alloc]initWithFrame: viewFrame]; MyCustomView* view2 =[[MyCustomView alloc]initWithFrame: viewFrame2]; [view addSubview:view2];
  6. 6. drawRect  •  Override  drawRect  –  method  in  custom  view  •  Default:  does  not  do  anything  
  7. 7. 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];
  8. 8. MoKon  Event  to  Custom  View  •  Override  moKonX  methods  in  custom  view   –  moKonBegan   –  moKonEnded   –  moKonCancelled  
  9. 9. 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]; }}
  10. 10. And  set  the  View  to  receive  events  // Create the view with given sizeMyCustomView* view = [[MyCustomView alloc] initWithFrame:viewFrame];// Set view to become the one who is receiving events![view becomeFirstResponder];
  11. 11. 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  
  12. 12. UIScrollView  Example  
  13. 13. UIScrollView  Example  
  14. 14. Panning  and  Paging  
  15. 15. Example  
  16. 16. Hide  Status  Bar  •  To  hide  status  bar  either:   –  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];•  Or  in  info  property  list  of  your  project  

×