قالب وردپرس درنا توس
Home / IOS Development / # 281: Touch Bar Basics 🚥 – Little Bites of Cocoa

# 281: Touch Bar Basics 🚥 – Little Bites of Cocoa



Subjects

Topics

In yesterday's MacBook Pro event Apple announced a fantastic new piece of hardware, called Touch Bar . It is a 1085 x 30 bulletproof Retina display that is located above the keyboard of the new MacBook Pros . It can dynamically change their controls and content as we work in each app on the main screen.

Today, we see the basics of adding Touch Bar support for a macOS app ]. Let's go diving.

First, some prerequisites. You need the latest version of Xcode (8.1

on publishing time), and the latest version of macOS . Even for those running 10.12.1, this updated 10.12.1 building must still be installed to test Touch Bars using Xcode .

After we've made sure We have the latest versions of everything we can launch Xcode 8.1 and select Window > View Touch Bar to view a simulated Touch Bar on screen while we work on our Mac .

It even flows over all other windows to make it feel a bit more realistic. Neat!

Puh! With that out of the way, we can fire up Xcode and create a new macOS app . [19659000] We create a new WindowController for our app window and set its class in Interface Builder .

  // WindowController.swift 
  class    WindowController .    NSWindowController    {
    ride    FUNC    windowDidLoad  ()    {
      super    windowDidLoad  () 

  ] 
} 

We add an extension to WindowController to adopt the protocol NSTouchBarDelegate . We also add a @available statement to ensure that it is only available on the last macOS :

  @available (OSX 10.12.1, *) 
  extension [19659013] WindowController  :    NSTouchBarDelegate    {
    ride    FUNC    makeTouchBar  ()    ->    NSTouchBar     {[19659043] ] 
   :    

              makeItemForIdentifier    identifies  19659014]:    
     // TODO 
  } 
} 

Below we must create our touch line . We do this with an instance of NSTouchBar .

  
    
                      -?>    NSTouchBar     
       NSTouchBar 
    
     .    delegate    =    even 
    return    touchBar 
} 

We use another extension to add some identifiers for touch line items we will look forward to NSTouchBarItemIdentifier :

  extension    NSTouchBarItemIdentifier    {
   ] static    La    launch    =       NSTouchBarItemIdentifier  (  "com.magnus.spaceships.launch" ) 
    static    la    hyper    =    NSTouchBarItemIdentifier  (  ] "com.magnus.spaceships.hyperspace" ) 
} 

Then we put them in the order we want in our touchpad s d efaultItemIdentifiers property .: ..

  touchpad    defaultItemIdentifiers    =    [[19659191]   launch       hyper [19659014]] 

Then we will implement the function by NSTouchBarDelegate (The "Cell for Objects on Index Course" Touching Children So-To-Talk).
We will swap on our identifiers from previously and return a NSTouchBarItem for each one:

  func    touchBar  19659061] _    touchBar :    NSTouchBar     makeItemForIdentifier    identifier :   ] NSTouchBarItemIdentifier )    -?.>    NSTouchBarItem     {
    switch ~~ POS = HEADCOMP    identifier    {

    case    NSTouchBarItemIdentifier    launch [19659014]: 
      la    subject    ] =    NSCustomTouchBarItem  (  identifier :    identifier ). 
       see    =    NSButton  (  Title :    "Launch"     Target :    ] ] ] action .    #   Selects  (  Launch ) 
      Return    Subject 

    Case    NSTouchBarItemIdentifier    Hyperrom : 
      la    topic    =    NSCustomTouchBarItem  (  identifier :    identifier ). 
     
    =    NSButton  (  title :    "Hyperspace"     goal :    self     action :    #   Selects  (  Hyperrom )) 
      ] ]     ]:    return    nil 
  } 
} 

Success!

Last but not least, we can let our Touch Bar items be customized by assigning it a customizationIdentifier :

  touchBar .   customizationIdentifier    =    NSTouchBarCustomizationIdentifier    "com.magnus.spaceships" )  if    # Available (OSX 10.12.1, *)    { 
   ]    (  NSClassFromString ] (  "NSTouchBar" ))  ! =    nil )    {
      NSA application .   ]    [196590191]   isAutomaticCustomizeTouchBarMenuItemEnabled    =    true 
  } 
} 

We also need to define which items can be customized:

  touchBar [19659191].   customizationAllowedItemIdentifiers [19659039] = [1965        .   hyperspace ] 

We can test your customization by selecting See > Customize Touch Bar ... from App Menu . Pro Tip: Hold Alternate to change the menu item to Customize the control strip ... instead, which allows us to customize the Persistent NSTouchBarItems as always available on the right side of Touch Bar ). Neat!


Source link