IOS9-by-Tutorials-study Note 5: Multitasking

Source: Internet
Author: User

IOS9-by-Tutorials-study Note 5: Multitasking
IOS9-by-Tutorials-study Note 5: Multitasking

On WWDC 2015, Apple launched multitasking, which allows us to run two apps at the same time. For example, we can simultaneously refresh Weibo when watching videos. Because we run two apps at the same time, the hardware requirements are high. Currently, Apple has not enabled these features on all devices. Here is a brief introduction to multitasking.

Getting started

In the book, there is a project Travelog supporting this chapter. Open this project and run it on the iPad Air 2 simulator. You can see a page similar to the following:

The Travelog App uses UISpiltViewController to display the list on the left. Click the list on the left to display the corresponding details on the right. We will introduce three modes of multitasking in this App:
* Slide Over
* Split View
* Picture in Picture, or PIP

Slide Over

We Slide along the right edge of the screen of the iPad to the left, and a list of apps will pop up. clicking an App will display the following status: Slide Over Mode:

In this status, Travelog cannot be used. Only the calendar App that we pop up can be used.

Split View

Note that there is a small icon on the left side of the calendar App. After clicking that icon, Travelog will be displayed on the iPad screen with the calendar App at the same time and can be used at the same time, this status is in the Split View mode:

Picture in Picture, or PIP

This mode is translated into Chinese, which is similar to the function of painting on TV. One example is that we can use other apps when using FaceTime. FaceTime will be scaled to a small interface and floating on your App.

Support for multitasking

Make your App support multitasking

If your App is slow as follows, your App can support multitasking.
* Is a universal app
* Use SDK 9.x to compile
* Supports all directions
* Use launch storyboard

The APP that meets the preceding four conditions only supports multitasking and does not indicate perfect adaptation.
Travelog meets the above four features, but it is not perfectly adapted.

Orientation and size changes

Run Travelog in Split View mode, and rotate the iPad to the vertical screen direction, as shown below:

This is a list interface that shows a lot of white space on the left. In the following content, we can make better use of these blank areas.

Rotate the screen to the landscape status, as shown below:

The above interface is also the list interface, but the master column is too narrow.

Open the SplitViewController. swift file. SplitViewController is a subclass of UISplitViewController and overwrites viewDidlayoutSubviews () to update the maximum width of the main column. This method may not work, because in the Split view mode of the horizontal screen, there may be narrow dead wn.

UIKit provides the following methods to capture layout changes:
1. willTransitionToTraitCollection (_:, withTransitionCoordinator :)
2. viewWillTransitionToSize (_:, withTransitionCoordinator :)
3. traitCollectionDidChange (_:):

The following shows the status of Size Classes:

In, R represents Regular, and C Represents Compact.

It can be seen that not all multitasking and direction changes will trigger the size class change, and you cannot rely solely on the size classes to provide the best user experience. We can use the viewWillTransitionToSize method to perform the correct operation when the size classes is changed.

The following describes how to delete the viewDidLayoutSubviews () and updateMaximumPrimaryColumnWidth () methods in SplitViewController. swift, and add some methods as follows (see comments ):

Import UIKitclass SplitViewController: UISplitViewController {// need to delete // override func viewDidLayoutSubviews () {// super. viewDidLayoutSubviews () // updateMaximumPrimaryColumnWidth () //} override func preferredStatusBarStyle ()-> UIStatusBarStyle {return. lightContent} // Delete // MARK: Helper // func updateMaximumPrimaryColumnWidth () {// if UIInterfaceOrientationIsPortrait (UIApplication. sharedApplication (). statusBarOrientation) {// maximumPrimaryColumnWidth = 170.0 //} else {// maximumPrimaryColumnWidth = bytes // override func viewDidLoad () {super. viewDidLoad () updateMaximumPrimaryColumnWidthBasedOnSize (view. bounds. size)} // Add // This is an auxiliary method used to set the maximum width of the main column func updateMaximumPrimaryColumnWidthBasedOnSize (size: CGSize) {if size. width <UIScreen. mainScreen (). bounds. width | size. width <size. height {maximumPrimaryColumnWidth = 170.0} else {maximumPrimaryColumnWidth = margin} // added override func viewWillTransitionToSize (size: CGSize, withTransitionCoordinator coordinator: Upper. viewWillTransitionToSize (size, withTransitionCoordinator: coordinator) updateMaximumPrimaryColumnWidthBasedOnSize (size )}}

The final result of running the app is as follows. It can be seen that the width of the main column of the app is better.

Let's take a closer look at the content of the line. We can see the content on the line, but there is still a problem. It should be that our cell does not respond to the size change. Open LogCell. swift and find the implementation of layoutSubviews (). You can find that the code checks UIScreen. mainScreen (). bounds. width, and then decide whether the cell uses Compact view or regular view. UIScreen represents the entire screen, ignoring the status of multitasking. You can no longer rely on the screen size to determine. Update the following code:

// Modify static let widthThreshold: CGFloat = 180. 0 ...... override func layoutSubviews () {super. layoutSubviews () // modify // let isTooNarrow = UIScreen. mainScreen (). bounds. width <LogCell. widthThreshold let isTooNarrow = bounds. width <= LogCell. widthThreshold compactView. hidden =! IsTooNarrow regularView. hidden = isTooNarrow}

After modification, the running effect is as follows:

Adaptive presentation

Click the Photo Library bar button in the following status to see the following results:

Drag the sign in the middle of the status to set the APP to 5: 5. The effect is as follows:

Note that we didn't make any changes, but when we changed from 33% to 50%, the pop-up modal menu changed to the entire page. This effect is controlled by UIPopoverPresentationController. The effect we want is that only the APP uses pover in Slide Over mode or as the second APP and 33%. We can set the delegate of UIPopoverPresentationController to implement our behavior.

Open LogsViewController. swift and add the following code:

Extension LogsViewController: Container {func adaptivePresentationStyleForPresentationController (controller: UIPresentationController, traitCollection: UITraitCollection)-> UIModalPresentationStyle {/1 determines that it is iPad guard traitCollection. userInterfaceIdiom =. pad else {return. fullScreen} // use popover if splitViewController?. The width is greater than 320 ?. View. bounds. width> 320 {return. None} else {return. FullScreen }}}

Set the proxy as follows:

Func presentImagePickerControllerWithSourceType (sourceType: UIImagePickerControllerSourceType) {let controller = UIImagePickerController () controller. delegate = self controller. sourceType = sourceType controller. mediaTypes = [String (kUTTypeImage), String (kUTTypeMovie)] controller. view. tintColor = UIColor. themeTineColor () if sourceType = UIImagePickerControllerSourceType. photoLibrary {controller. ModalPresentationStyle =. Popover let presenter = controller. popoverPresentationController presenter ?. SourceView = view presenter ?. BarButtonItem = photoLibraryButton presenter ?. Delegate = self // Add} presentViewController (controller, animated: true, completion: nil )}

The final running result is as follows:

I have completed this note, but it may not be exactly the same as in the book. I feel that this article is just a brief introduction. For more information, see the apple documentation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.