archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • A watchOS 2 WatchKit Dynamic Layout and Animation Tutorial - Techotopia
    following line of code sets the width of an interface object to 60 of the width of the container myObject setRelativeWidth 0 6 withAdjustment 0 The following code on the other hand sets the height of an interface object to 30 of the container s width with an adjustment of 20 points myObject setRelativeHeight 0 3 withAdjustment 20 Finally an interface object may be configured such that it sizes to fit its content using the sizeToFitWidth and sizeToFitHeight methods myObject sizeToFitHeight myObject sizeToFitWidth Setting the Visibility of an Interface Object The visibility of an interface object can be changed from within code via a call to the object s setHidden method myObject setHidden true myObject setHidden false The transparency of an interface object is adjusted using the setAlpha method passing through a value between 0 0 and 1 0 with 1 0 representing a fully opaque appearance myObject setAlpha 0 5 eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Animating Interface Changes The concept of animation within the scene of a WatchKit app involves the use of so called animation block methods Animation block methods are used to mark the beginning and end of a sequence of changes to the appearance of interface objects within a scene Once the end of the block is reached the animation is performed over a specified duration For the sake of example consider a Button object connected to an outlet named myButton The application requires that the button gradually fade from view over a period of 3 seconds This can be achieved by making the button transparent through the use of the alpha property myButton setAlpha 0 Simply setting the alpha property to 0 however causes the button to immediately become transparent In order to make it fade out of sight gradually we need to place this line of code in a call to the animateWithDuration animation block method as follows self animateWithDuration 3 0 animations self myButton setAlpha 0 0 When the above code is executed the object will gradually fade from view over a period of 3 seconds An Animation Example Start Xcode and create a new iOS project On the template screen choose the Application option located under watchOS in the left hand panel and select iOS App with WatchKit App Click Next set the product name to AnimateInterfaceApp enter your organization identifier and make sure that the Devices menu is set to Universal Before clicking Next change the Language menu to Swift and switch off all of the Include options On the final screen choose a location in which to store the project files and click on Create to proceed to the main Xcode project window Designing the User Interface Select the Interface storyboard file located under AnimateInterfaceApp WatchKit App so that it loads into Interface Builder Locate the Group

    Original URL path: http://www.techotopia.com/index.php/A_watchOS_2_WatchKit_Dynamic_Layout_and_Animation_Tutorial (2016-02-13)
    Open archived version from archive


  • Working with Fonts and Attributed Strings in watchOS 2 - Techotopia
    the user s current preferred content size category setting Having obtained the preferred font the next step is to use the font to render the text displayed on a Label or Button interface object As will be outlined in the next section this involves the use of attributed strings Understanding Attributed Strings To display text within a WatchKit app using a particular font it is necessary to use an attributed string Attributed strings are represented using the NSAttributedString and NSMutableAttributedString classes and allow text to be combined with attributes such as fonts and colors Having obtained a font object as outlined in the previous section the next task is to create a Dictionary object with a key set to NSFontAttributeName and the font object as the value This dictionary is then used to specify the attributes for an NSAttributedString instance The following code for example obtains a font object for the headline text style and uses it to create an attributed string that reads Apple Watch let headlineFont UIFont preferredFontForTextStyle UIFontTextStyleHeadline let fontAttribute NSFontAttributeName headlineFont let attributedString NSAttributedString string Apple Watch attributes fontAttribute Once an attributed string has been created it can be applied to Button and Label interface objects using the setAttributedTitle and setAttributedText methods respectively The following code for example displays the attributedString text from the above example on a Label object myLabel setAttributedText attributedString When executed text which reads Apple Watch will be rendered on the label referenced by the myLabel outlet using the headline font style sized according to the user s preferred content size setting as shown in Figure 20 3 Figure 20 3 eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters The above example uses an attributed string to set one attribute for the entire text string In practice attributed strings can contain multiple attributes covering different ranges of characters within the string Multiple attributes within an attributed string require the use of the NSMutableAttributedString class Once an instance of this class has been created and initialized with a string the addAttribute method may be called to add attributes for ranges of characters in the string For the purposes of the example project this approach will be used to change the colors used to render the words Apple and Watch in the label in Figure 20 3 The code to achieve this reads as follows let headlineFont UIFont preferredFontForTextStyle UIFontTextStyleHeadline let fontAttribute NSFontAttributeName headlineFont let attributedText NSMutableAttributedString string Apple Watch attributes fontAttribute let firstRange NSRange location 0 length 5 let secondRange NSRange location 6 length 5 attributedText addAttribute NSForegroundColorAttributeName value UIColor greenColor range firstRange attributedText addAttribute NSForegroundColorAttributeName value UIColor redColor range secondRange myLabel setAttributedText attributedText The above code creates a mutable attributed string using the headline text style font Two range objects are then created and initialized to encompass the first and second words

    Original URL path: http://www.techotopia.com/index.php/Working_with_Fonts_and_Attributed_Strings_in_watchOS_2 (2016-02-13)
    Open archived version from archive

  • A watchOS 2 App Custom Font Tutorial - Techotopia
    the Group object in the scene layout Double click on the upper label and change the text so it reads Hello Shift click on each Label object so that both are selected and within the Attributes Inspector panel change the Horizontal Alignment property to Center On completion of these steps verify that the scene matches the layout shown in Figure 21 4 Figure 21 4 The last task to perform within Interface Builder is to establish an outlet connection on the second Label object Display the Assistant Editor panel verify that it is displaying the content of the InterfaceController swift file and Ctrl click and drag from the lower Label to a position immediately beneath the class declaration line in the editor panel On releasing the line use the connection dialog to establish an outlet named labelObject Adding the Custom Font to the Project There are two steps to integrating a custom font into a WatchKit app The first step is to add the font file to the project For the purposes of this example the Sofia Regular font will be used Locate this font in the Finder window and drag and drop it beneath the CustomFont WatchKit App entry in the Xcode project navigator Figure 21 5 Since the font will need to be accessible to both the WatchKit App and the extension make sure that both targets are selected in the options panel Figure 21 6 before clicking on the Finish button Figure 21 6 In addition to adding the font file to the project and configuring the targets an entry for the font needs to be added to the Info plist files for both the WatchKit app and extension targets Begin by selecting the Info plist file located under the CustomFont WatchKit App folder in the Project Navigator panel Once the file has loaded into the property list editor select the last row in the list so that it highlights in blue Click on the button that appears in the selected row to add a new entry to the list From the dropdown menu that appears scroll down to and select the Fonts provided by application option as illustrated in Figure 21 7 Figure 21 7 Click on the arrow to the left of the newly added row to list the first item in the array of fonts Double click in the Value column of this row to enter into editing mode type in the full file name of the custom font file including the otf filename extension and press the keyboard Enter key Figure 21 8 The same entry also needs to be added to the Info plist file of the extension target Select the Info plist file listed under the CustomFont WatchKit Extension folder and repeat the above steps to add an entry for the same font file With these steps completed it should now be possible to begin using the custom font within the WatchKit app eBookFrenzy com Purchase the full edition of this

    Original URL path: http://www.techotopia.com/index.php/A_watchOS_2_App_Custom_Font_Tutorial (2016-02-13)
    Open archived version from archive

  • An Introduction to the watchOS 2 WatchKit WKInterfacePicker Object - Techotopia
    Digital Crown Options available for this attribute are for an outline to appear around the picker for an outline combined with a caption to be visible or for no outline to appear Figure 25 1 for example shows a picker object configured with both outline and caption Figure 25 1 Indicator This attribute controls whether or not an indicator is displayed in the top right hand corner of the picker object to provide a visual indication of the user s progress through the list of items presented by the picker Enabled This attribute indicates whether the picker object is able to be selected by the user within the scene Understanding Picker Object Styles A WKInterfacePicker instance can be configured to present content using one of three different styles List The list style presents the picker items in a vertical stack through which the user scrolls using the Digital Crown Each item within the list can contain a title a caption an accessory image and a content image The title takes the form of the text that is displayed for the item in the list If the picker object is configured to display an outline with a caption the caption property defines the text that is displayed within the outline of the picker The optional accessory image appears to the left of the title text The content image if provided appears in the background of the list item Stack When configured to use the stack style the picker items are presented in a form resembling a stack of cards displaying images At any one time only one item is visible As the user turns the Digital Crown the current item is animated off the screen and the new one transitioned on screen Image Sequence The image sequence style is ideal for presenting an animated sequence of images whereby the user moves from frame to frame within the animation sequence using the Digital Crown As outlined in the chapter entitled A WatchKit WKInterfacePicker Coordinated Animation Example multiple animated sequences may be coordinated with the Digital Crown within a single interface controller scene eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters Creating Picker Item Objects Each item displayed by a picker object is contained within an instance of the WKPickerItem class When working in List mode each picker item may be configured with a caption title content image and accessory image For stacked and image sequence modes only the contentImage property of the WKPickerItem object needs to be set Images must be provided as WKImage objects The WKPickerItem objects are packaged into an array which is passed through to the setItems method of the picker object The following code fragment demonstrates the initialization of a picker object configured to display both title and caption information var itemList String String Caption 1 Red Caption

    Original URL path: http://www.techotopia.com/index.php/An_Introduction_to_the_watchOS_2_WatchKit_WKInterfacePicker_Object (2016-02-13)
    Open archived version from archive

  • A watchOS 2 WatchKit Picker Tutorial - Techotopia
    and click on Create to proceed to the main Xcode project window Designing the WatchKit App Scene Locate and select the Interface storyboard file listed under PickerListApp WatchKit App in the Project Navigator panel Once loaded into Interface Builder drag and drop a Picker object from the Object Library panel onto the main storyboard scene Next drag a Label object and position it immediately beneath the Picker object as illustrated in Figure 26 1 Once the Label has been added use the Attributes Inspector panel to set the Alignment and Horizontal Position properties to Center Figure 26 1 Display the Assistant Editor panel and create outlet connections from the Picker and Label objects named myPicker and itemLabel respectively Finally select the Picker object in the scene and in the Attributes Inspector panel set the Style property to List and the Focus Style to Outline with Caption as shown in Figure 26 2 Figure 26 2 Implementing the Picker List Items Each picker item will need to have a caption and a title property The values for these items will be stored in an array of string pairs within the interface controller class Select the InterfaceController swift file and add this array as follows import WatchKit import Foundation class InterfaceController WKInterfaceController IBOutlet var myPicker WKInterfacePicker IBOutlet var itemLabel WKInterfaceLabel var itemList String String Item 1 Red Item 2 Green Item 3 Blue Item 4 Yellow Item 5 Indigo Item 6 Violet override func awakeWithContext context AnyObject super awakeWithContext context Configure interface objects here The code to populate the picker object with the items now needs to be added to the awakeWithContext method as follows override func awakeWithContext context AnyObject super awakeWithContext context let pickerItems WKPickerItem itemList map let pickerItem WKPickerItem pickerItem caption 0 0 pickerItem title 0 1 return pickerItem myPicker setItems pickerItems The code added to the method works through the items in the array and creates a WKPickerItem object for each one assigning the string values in each array element to the picker item caption and title properties After each WKPickerItem object has been created and initialized it is added to the array of picker items Once the array is complete it is passed through to the myPicker object via the setItems method Compile and run the PickerListApp WatchKit App target and once running scroll through the list items using the Digital Crown or in the case of the Watch Simulator the mouse scroll wheel Figure 26 3 Note that the picker has a green outline as configured and that the caption changes along with the item as the list scrolls Implementing the Action Method In order to be able to update the Label object with the current selection in the picker list an action outlet needs to be added to the project With the Assistant Editor panel displayed establish an action connection from the picker object to a method named pickerChanged Once the action method has been established modify the code so that it reads as follows IBAction

    Original URL path: http://www.techotopia.com/index.php/A_watchOS_2_WatchKit_Picker_Tutorial (2016-02-13)
    Open archived version from archive

  • A WatchKit WKInterfacePicker Coordinated Animation Example - Techotopia
    establish an outlet connection from the Group object named myGroup Display the Attributes Inspector panel and change both the Horizontal and Vertical Alignment settings to Center With the Group object still selected change the Height and Width Size properties on the Group object to be Fixed at 100 Figure 24 2 Locate the Picker object in the Object Library panel and drag and drop an instance onto the Group object within the storyboard scene Once again using the Assistant Editor panel establish an outlet connection from the Picker object named myPicker With the Picker object still selected in the storyboard scene change the Style property in the Attributes Inspector panel to Sequence and both the Horizontal and Vertical Alignment properties to Center Finally change both the Height and Width Size properties for the Picker object to be Fixed setting the Width property to 65 and the Height property to 32 On completion of these steps the layout for the main scene should match that of Figure 24 3 Figure 24 3 Adding the Picker Image Sequences to the Project The animation will require two sets of animation sequences one representing the percentage text and the other the progress circle The images for the picker object can be found in the sample code download for the book which is available from the following URL http www ebookfrenzy com code watchOS2BookSamples zip Within the Project Navigator panel select the Assets xcassets entry listed under PickerImagesApp WatchKit App so that the asset catalog loads into the main Xcode panel Ctrl click in the center panel beneath the AppIcon entry and from the resulting menu select the Import menu option eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters In the file selection dialog navigate to and select the picker images folder in the sample code archive folder and click on the Open button All of the images needed for the percentage animation sequence will now be added to the asset catalog of the WatchKit app bundle Figure 24 4 Adding the Group Background Image Sequences to the Project The images for the Group object background are contained within the group images folder of the sample code download and must be added to the asset catalog of the WatchKit app extension Within the Project Navigator panel select the Assets xcassets entry listed under PickerImagesApp WatchKit Extension so that the asset catalog loads into the main Xcode panel Ctrl click in the center panel and from the resulting menu select the Import menu option In the file selection dialog navigate to and select the group images folder in the sample code folder and click on the Open button All of the images needed for the radial animation sequence will now be added to the asset catalog of the WatchKit extension Implementing the Picker Animation Sequence With

    Original URL path: http://www.techotopia.com/index.php/A_WatchKit_WKInterfacePicker_Coordinated_Animation_Example (2016-02-13)
    Open archived version from archive

  • Sharing watchOS 2 Media Files Using App Groups - Techotopia
    and the corresponding WatchKit app Sharing Data Using App Groups The sharing of files between a WatchKit extension and the WatchKit app can be achieved through the use of shared app groups When the WatchKit app and WatchKit extension are enrolled in the same app group they are given access to a shared container This allows files to be shared between the WatchKit app and the extension The diagram in Figure 25 1 illustrates this concept Figure 25 1 Adding the WatchKit App and Extension to an App Group App Group settings are contained in entitlement files using the com apple security application groups key Both the WatchKit extension and the WatchKit app must include this entitlement The value assigned to the com apple security application groups key indicates the app group to which membership is required This value is typically set to the package name of the app prefixed with group and must match in both the WatchKit app and extension entitlement files While it is possible to manually create the necessary entitlement files by far the easiest way to configure app group membership is to do so through the Xcode Capabilities panel To configure app group support for the WatchKit app select the target located at the top of the Project Navigator panel and click on the Capabilities tab in the main panel Within the capabilities panel locate the App Groups section and switch it to the On position Figure 25 2 When app groups have been enabled in the Capabilities screen any existing app groups associated with your Apple developer account will be listed To make the current app a member of any of those groups simply enable the checkbox next to the group name Figure 25 3 eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters To add a new app group to your account simply click on the button and enter the new app group name Add the current app to the newly added app group by enabling the checkbox next to the group name The app group will subsequently appear as an option in all other project targets within the Xcode Capabilities panel With the WatchKit app added to the app group the WatchKit extension must also be added as a member of the same group in order to gain access to the shared container To access the capability settings for the WatchKit Extension use the menu located in the top left hand corner of the Capabilities panel as indicated in Figure 25 4 Figure 25 4 When clicked this menu will present a list of targets contained within the current project as shown in Figure 25 5 one of which will be the WatchKit extension Select this option and repeat the steps followed for the WatchKit app to enable and configure app group support

    Original URL path: http://www.techotopia.com/index.php/Sharing_watchOS_2_Media_Files_Using_App_Groups (2016-02-13)
    Open archived version from archive

  • Playing Movies and Audio on watchOS 2 using the WKInterfaceMovie Class - Techotopia
    setLoops method Directly Playing Content Obviously the WKInterfaceMovie class involves the placement of a user interface object in a storyboard scene which the user must tap in order to initiate audio or video playback Situations may arise however where playback needs to be triggered automatically in response to some other form of event such as a button being tapped or the user selecting a movie clip from a table list Playback of this kind can be initiated via a call to the presentMediaPlayerControllerWithURL method of the WKInterfaceController class This method displays the same playback interface as that used by the WKInterfaceMovie class but allows playback to auto play and does not require the placement of a user interface object into the storyboard scene The presentMediaPlayerControllerWithURL method takes as parameters the URL of the content to be played a dictionary containing options such as whether playback should start automatically and a completion handler closure containing any necessary code to be executed once playback completes On completion of playback the completion handler is passed a Boolean value indicating whether or not playback ran to the end of the content the time into the content at which playback stopped and an NSError object containing details of any errors encountered let options WKMediaPlayerControllerOptionsAutoplayKey true presentMediaPlayerControllerWithURL saveUrl options options completion didPlayToEnd endTime error in Code here is called after playback ends or fails eBookFrenzy com Purchase the full edition of this watchOS 2 App Development Essentials book in eBook 12 99 or Print 27 99 format watchOS 2 App Development Essentials Print and eBook ePub PDF Kindle editions contain 35 chapters The above example code configures the playback to automatically start The full range of configuration options when calling this method are as follows WKMediaPlayerControllerOptionsAutoplayKey When set to true playback is initiated automatically once the specified media has loaded WKMediaPlayerControllerOptionsStartTimeKey An NSTimeInterval value indicating the number of seconds into the content at which playback is to start WKMediaPlayerControllerOptionsVideoGravityKey The video gravity setting to be used when playing video content These values are the same as those previously described for the WKInterfaceMovie class WKMediaPlayerControllerOptionsLoopsKey When set to true this option causes playback to loop continuously Creating the WKInterfaceMovie Example Start Xcode and create a new iOS project On the template screen choose the Application option located under watchOS in the left hand panel and select iOS App with WatchKit App Click Next set the product name to MoviePlayerApp enter your organization identifier and make sure that the Devices menu is set to Universal Before clicking Next change the Language menu to Swift and switch off all of the Include options On the final screen choose a location in which to store the project files and click on Create to proceed to the main Xcode project window Designing the WatchKit App Scene Select the Interface storyboard file located under MoviePlayerApp WatchKit App and drag and drop a Movie object from the Object Library onto the scene within the storyboard so that the scene matches that shown in

    Original URL path: http://www.techotopia.com/index.php/Playing_Movies_and_Audio_on_watchOS_2_using_the_WKInterfaceMovie_Class (2016-02-13)
    Open archived version from archive



  •