0305--iOS之阅读View Controller Programming Guide for iOS---(二(下))Preserving and Restoring State

 

Preserving and Restoring State                                                    --保存和恢复状态        

 

View controllers play an important role in the state preservation and restoration process. State preservation records the configuration of your app before it is suspended so that the configuration can be restored on a subsequent app launch. Returning an app to its previous configuration saves time for the user and offers a better user experience.
           --vc在状态的保持和恢复过程中起着重要的作用。状态保存机制记录了 “在app挂起之前” 的配置,以便在随后的app启动时还原该配置。将app返回到以前的配置可以为用户节省时间,并提供更好的用户体验。

The preservation and restoration process is mostly automatic, but you need to tell iOS which parts of your app to preserve. The steps for preserving your app’s view controllers are as follows:
            --保存和恢复的过程基本上是自动的,但你需要告诉iOS你的app有哪些部分需要保存。保存应用程序vc的步骤如下:

  • (Required) Assign restoration identifiers to the view controllers whose configuration you want to preserve; see Tagging View Controllers for Preservation.
          --(必选项)将“恢复标识符”分配给vc,那个你要保留它配置的vc; 参见超链接

  • (Required) Tell iOS how to create or locate new view controller objects at launch time; see Restoring View Controllers at Launch Time.
            --(必选项)告诉iOS在启动时 如何创建或定位新的vc对象; 请参阅超链接: “在启动时还原视图控制器”。

  • (Optional) For each view controller, store any specific configuration data needed to return that view controller to its original configuration; see Encoding and Decoding Your View Controller’s State.
             --(可选项)对于每个vc,存储该vc返回原状态所需的具体配置数据;  参考链接: “编码和解码您的视图控制器的状态  ”  

For an overview of the preservation and restoration process, see App Programming Guide for iOS.
          -- 有关保存和修复过程的概述,请参阅“iOS应用程序编程指南”。

 

Tagging View Controllers for Preservation                               --标记vc用于保存

 

UIKit preserves only the view controllers you tell it to preserve. Each view controller has a restorationIdentifier property, whose value is nil by default. Setting that property to a valid string tells UIKit that the view controller and its views should be preserved. You can assign restoration identifiers programmatically or in your storyboard file.
         --UIKit只保留你让它保留的vc。每个视图控制器都有一个restorationIdentifier属性,默认值为nil。将这个属性设置为一个有效的字符串来告诉UIKit 去保留vc和vc的views。您可以以编程方式或在storyboard文件中赋值恢复标识符。

When assigning restoration identifiers, remember that all parent view controllers in the view controller hierarchy must have restoration identifiers too. During the preservation process, UIKit starts at the window’s root view controller and walks the view controller hierarchy. If a view controller in that hierarchy does not have a restoration identifier, the view controller and all of its child view controllers and presented view controllers are ignored.
            --在分配“还原标识符”时,请记住vc层次结构中 的“所有父vc“ 都必须具有 ”还原标识符“。在保存过程中,UIKit从窗口的根vc开始并遍历vc层次结构。如果该层次结构中的vc没有还原标识符,则将忽略该vc及其所有子vc和显示的vc。

 

Choosing Effective Restoration Identifiers                                   --选择有效的恢复标识符

 

UIKit uses your restoration identifier strings to recreate view controllers later, so choose strings that are easily identifiable to your code. If UIKit cannot automatically create one of your view controllers, it asks you to create it, providing you with the restoration identifiers of the view controller and all of its parent view controllers. This chain of identifiers represents the restoration path for the view controller and is how you determine which view controller is being requested. The restoration path starts at the root view controller and includes every view controller up to and including the one that was requested.

           --UIKit是使用你的“恢复标识符字符串”来重建vc的,所以你要选择那些容易被你的代码识别的字符串。如果UIKit不能自动创建你的某个vc时,UIKit会要求你来创建它、但会提供vc的恢复标识符和vc所有的父vc给你。这个“标识符链”表示了vc的恢复路径,并且是您获知是哪个视图控制器需要重建的途径。恢复路径从根视图控制器开始,直到被请求重建的vc为止(途间的所有vc)。

Restoration identifiers are often just the class name of the view controller. If you use the same class in many places, you might want to assign more meaningful values. For example, you might assign a string based on the data being managed by the view controller.

        --恢复标识符通常只是vc的类名。如果你在许多地方都使用同一个类,则需要设置更有意义的值。例如,您可以基于vc管理的数据来分配字符串。

The restoration path for every view controller must be unique. If a container view controller has two children, the container must assign each child a unique restoration identifier. Some container view controllers in UIKit automatically disambiguate their child view controllers, allowing you to use the same restoration identifiers for each child. For example, the UINavigationController class adds information to each child based on its position in the navigation stack. For more information about the behavior of a given view controller, see the corresponding class reference.

           --每个vc的还原路径必须是唯一的。如果容器vc有两个子级,则容器必须为每个子级分配唯一的还原标识符。UIKit中的某些vc会自动消除其子vc的歧义,从而允许您为每个子视图使用相同的还原标识符。例如,UINavigationController类会根据子vc在导航堆栈中的位置添加额外的信息。有关给定vc行为的详细信息,请参阅超链接: “相应的类引用”。

For more information on how you use restoration identifiers and restoration paths to create view controllers, see Restoring View Controllers at Launch Time.

              --有关如何使用恢复标识符和恢复路径创建视图控制器的更多信息,请参见超链接: “在启动时还原视图控制器”。

 

Excluding Groups of View Controllers                                                 --排除一组vc

 

To exclude an entire group of view controllers from the restoration process, set the restoration identifier of the parent view controller to nil. Figure 7-1 shows the impact that setting the restoration identifier to nil has on the view controller hierarchy. The lack of preservation data prevents that view controller from being restored later.

                --若要从还原过程中排除整个vc组,请将父vc的还原标识符设置为nil。图7-1 显示了将恢复标识符设置为nil时, 对视图控制器层次结构的影响。这会导致由于缺少保存数据,无法在以后还原该vc。

Figure 7-1 Excluding view controllers from the automatic preservation process   --从自动保存vc的进程中排除一组vc

image: ../Art/state_vc_caveats.jpg

Excluding one or more view controllers does not remove them altogether during a subsequent restore. At launch time, any view controllers that are part of your app’s default setup are still created, as shown in Figure 7-2. Such view controllers are recreated in their default configuration, but they are still created.

           --排除一个或多个vc时 , 这些vc不一定全部都在随后的还原过程中被剔除。在启动时,那些作为app默认配置的vc,还是会被创建的.  如图7-2所示。这些的vc是在其默认配置被重新创建的,所以它们仍然是会被创建的。

Figure 7-2 Loading the default set of view controllers                                              --加载默认的vc集

image: ../Art/state_vc_caveats_2.jpg

Excluding a view controller from the automatic preservation process does not prevent you from preserving it manually. Saving a reference to the view controller in the restoration archive preserves the view controller and its state information. For example, if the app delegate in Figure 7-1 saved the three children of the navigation controller, their state would be preserved. During a restore, the app delegate could then recreate those view controllers and push them onto the navigation controller’s stack.

            --在自动保存过程排除掉的vc并不妨碍您手动保存它们。在恢复存档中保存对vc的引用将会保存vc及其状态信息。例如,如果图7-1 中的app委托保存了导航控制器的三个子控件,它们的状态将被保留。在恢复期间,app的委托可以重新创建那些视图控制器,并将它们压到导航vc的堆栈上。

 

Preserving a View Controller’s Views                             --保存vc的视图

 

            --Some views have additional state information that is relevant to the view but not to the parent view controller. For example, a scroll view has a scroll position that you might want to preserve. While the view controller is responsible for providing the content of the scroll view, the scroll view itself is responsible for preserving its visual state.

             --有view具有与view相关但与它的父vc无关的附加状态信息。例如,滚动视图具有您可能希望保留的滚动位置。这里是vc负责提供滚动视图的内容,而滚动视图本身则负责保存其可视状态。

To save a view’s state, do the following:
         --要保存视图的状态,请执行以下操作:

  • Assign a valid string to the view’s restorationIdentifier property.
       --1、为“视图”的restorationIdentifier属性分配一个有效的字符串

  • Use the view from a view controller that also has a valid restoration identifier.
        --2、使用具有有效恢复标识符的vc中的视图。

  • For table views and collection views, assign a data source that adopts the UIDataSourceModelAssociation protocol.
       --3、对于列表视图和集合视图,分配一个采用UIDataSourceModelAssociation协议的数据源。

Assigning a restoration identifier to a view tells UIKit that it should write that view’s state to the preservation archive. When the view controller is restored later, UIKit also restores the state of any views that had restoration identifiers.

       --给”视图“分配一个恢复标识符,用来告诉UIKit, 它应该把哪个视图的状态写到保存存档。当vc稍后被恢复时,UIKit也会恢复任何有恢复标识符的视图的状态。

 

Restoring View Controllers at Launch Time                          --在启动期恢复视图控制器

 

At launch time, UIKit tries to restore your app to its previous state. At that time, UIKit asks your app to create (or locate) the view controller objects that comprise your preserved user interface. UIKit searches in the following order when trying to locate view controllers:

        --在启动期,UIKit会尝试将您的app还原到以前的状态。此时,UIKit会要求应用程序创建(或定位)组成被保留的用户界面的vc对象。UIKit查找vc时按以下顺序搜索:

  1. If the view controller had a restoration class, UIKit asks that class to provide the view controller. UIKit calls the viewControllerWithRestorationIdentifierPath:coder: method of the associated restoration class to retrieve the view controller. If that method returns nil, it is assumed that the app does not want to recreate the view controller and UIKit stops looking for it.
          --1、如果vc有一个还原类,UIKit会要求还原类提供该vc。UIKit调用该还原类的 viewControllerWithRestorationIdentifierPath: coder:方法来检索vc。如果该方法返回nil,则假定应用程序不希望重新创建vc,并且UIKit停止查找它。

  2. If the view controller did not have a restoration class, UIKit asks the app delegate to provide the view controller. UIKit calls the application:viewControllerWithRestorationIdentifierPath:coder: method of your app delegate to look for view controllers without a restoration class. If that method returns nil, UIKit tries to find the view controller implicitly.
           --2、如果vc没有还原类,UIKit会要求qpp委托提供视图控制器。UIKit调用app委托的 viewControllerWithRestorationIdentifierPath: coder: 方法来查找没有还原类的vc。如果该方法返回nil,则UIKit会尝试隐式查找vc。

  3. If a view controller with the correct restoration path already exists, UIKit uses that object. If your app creates view controllers at launch time (either programmatically or by loading them from a storyboard) and those view controllers have restoration identifiers, UIKit finds them implicitly based on their restoration paths.
              --3、如果已存在具有正确还原路径的vc,则UIKit将使用该vc。如果应用程序在启动时创建vc(以编程方式或storyboard的方式加载vc),并且这些vc具有还原标识符,则UIKit会根据它们的还原路径“隐式”查找它们。

  4. If the view controller was originally loaded from a storyboard file, UIKit uses the saved storyboard information to locate and create it. UIKit saves information about a view controller’s storyboard inside the restoration archive. At restore time, UIKit uses that information to locate the same storyboard file and instantiate the corresponding view controller if the view controller was not found by any other means.
                --4、如果vc最初是从storyboard加载的,则UIKit使用保存的storyboard信息来定位和创建它。UIKit将有关vc的storyboard信息保存在“还原存档”中。在还原时,UIKit使用该信息来定位相同的stroyboard文件,并在其他方法都找不到vc的情况下才实例化相应的vc。

Assigning a restoration class to a view controller prevents UIKit from searching for that view controller implicitly. Using a restoration class gives you more control over whether you really want to create a view controller. For example, your viewControllerWithRestorationIdentifierPath:coder: method can return nil if your class determines that the view controller should not be recreated. When no restoration class is present, UIKit does everything it can to find or create the view controller and restore it.

            --为vc分配还原类可防止UIKit隐式搜索该vc。使用还原类可以更好地控制是否确实要创建vc。例如,如果你的类确定不应重新创建该vc,则可以设置viewControllerWithRestorationIdentifierPath:coder:method方法返回nil。当不存在还原类时,UIKit会尽其所能查找或创建vc并还原它。

When using a restoration class, your viewControllerWithRestorationIdentifierPath:coder: method should create a new instance of the class, perform minimal initialization, and return the resulting object. Listing 7-1 shows an example of how you might use this method to load a view controller from a storyboard. Because the view controller was originally loaded from a storyboard, this method uses the UIStateRestorationViewControllerStoryboardKey key to get the storyboard from the archive. Note that this method does not try to configure the view controller’s data fields. That step occurs later when the view controller’s state is decoded.

         --使用还原类时,viewControllerWithRestorationIdentifierPath:coder:方法应创建类的新实例,执行最小初始化并返回结果对象。清单7-1 代码显示了一个示例,说明如何使用该方法从storyboard中加载vc。由于vc最初是从storyboard中加载的,因此该方法使用UIStateRestorationViewControllerStoryboardKey 键从存档中获取storyboard。请注意,该方法不会尝试配置vc的数据字段, 因为这一步在vc的状态被解码后发生。

Listing 7-1 Creating a new view controller during restoration                              --在恢复期间创建一个新的vc

  1. + (UIViewController*) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents
  2. coder:(NSCoder *)coder {
  3. MyViewController* vc;
  4. UIStoryboard* sb = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
  5. if (sb) {
  6. vc = (PushViewController*)[sb instantiateViewControllerWithIdentifier:@"MyViewController"];
  7. vc.restorationIdentifier = [identifierComponents lastObject];
  8. vc.restorationClass = [MyViewController class];
  9. }
  10. return vc;
  11. }

Reassigning the restoration identifier and restoration class is a good habit to adopt when recreating view controllers manually. The simplest way to restore the restoration identifier is to grab the last item in the identifierComponents array and assign it to your view controller.

         --手动重新创建vc时,记得重新分配“还原标识符” 和“还原类”。重新分配“恢复标识符”的最简单方法是获取identifierComponents数组中的最后一项并将其分配给vc。

For objects that were created from your app’s main storyboard file at launch time, do not create new instances of each object. Let UIKit find those objects implicitly or use the application:viewControllerWithRestorationIdentifierPath:coder: method of your app delegate to find the existing objects.

       --对于在启动时从app的storyboard文件中创建的对象,不要为每个对象创建新实例。让UIKit隐式查找这些对象,或使用app委托的application:viewControllerWithRestorationIdentifierPath:coder 方法查找现有对象。

 

Encoding and Decoding Your View Controller’s State          --编码和解码你的vc的状态

 

For each object slated for preservation, UIKit calls the object’s encodeRestorableStateWithCoder: method to give it a chance to save its state. During the restoration process, UIKit calls the matching decodeRestorableStateWithCoder: method to decode that state and apply it to the object. The implementation of these methods is optional, but recommended, for your view controllers. You might use them to save and restore the following types of information:

         --对于每个预定要保存的对象,UIKit会去调用该对象的encodeRestorableStateWithCoder:方法,使其有机会保存其状态。在还原过程中,UIKit调用相对应的decodeRestorableStateWithCoder:方法来解码该状态并将其应用于该对象。这些方法的实现是可选的,但建议用于vc。您可以使用这些方法保存和还原以下类型的信息:

  • References to any data being displayed (not the data itself)
      --对正在显示的任何数据的引用(不是数据本身)

  • For a container view controller, references to its child view controllers
       --容器vc的子vc的引用

  • Information about the current selection
        --有关当前所选内容的信息

  • For view controllers with a user-configurable view, information about the current configuration of that view.
       --若是具有用户可配置视图的vc,则保存有关该视图的当前配置的信息。

In your encode and decode methods, you can encode objects and any data types supported by the coder. For all objects except views and view controllers, the object must adopt the NSCoding protocol and use the methods of that protocol to write its state. For views and view controllers, the coder does not use the NSCoding protocol to save the object’s state. Instead, the coder saves the restoration identifier of the object and adds it to the list of preservable objects, which causes that object’s encodeRestorableStateWithCoder: method to be called.
      --在编码和解码方法中,可以对对象或编码器支持的任何数据类型进行编码。对于除view和vc之外的所有对象,该对象必须采用NSCoding协议并使用该协议的方法来写入其状态。对于view和vc,编码器不使用NSCoding协议来保存对象的状态, 而是,编码器保存对象的“还原标识符”并将其添加到可保存对象列表中,这将导致UIKit调用该对象的encodeRestorableStateWithCoder:方法。

The encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: methods of your view controllers must call super at some point in their implementation. Calling super gives the parent class a chance to save and restore any additional information. Listing 7-2 shows a sample implementation of these methods that save a numerical value used to identify the specified view controller.

         --你的vc的encodeRestorableStateWithCoder:和decdeRestorableStateWithCoder:方法必须在实现中的某个点调用super关键字。调用super使父类有机会保存和还原任何附加信息。清单7-2显示了这些方法实现的示例,这些方法保存了“用于标识给定vc的”数字的值。

Listing 7-2 Encoding and decoding a view controller’s state.                 --对vc的状态进行编码和解码

  1. - (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
  2. [super encodeRestorableStateWithCoder:coder];
  3.  
  4. [coder encodeInt:self.number forKey:MyViewControllerNumber];
  5. }
  6.  
  7. - (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
  8. [super decodeRestorableStateWithCoder:coder];
  9.  
  10. self.number = [coder decodeIntForKey:MyViewControllerNumber];
  11. }

Coder objects are not shared during the encode and decode process. Each object with preservable state receives its own coder object. The use of unique coders means that you do not have to worry about namespace collisions among your keys. However, do not use the UIApplicationStateRestorationBundleVersionKeyUIApplicationStateRestorationUserInterfaceIdiomKey , and UIStateRestorationViewControllerStoryboardKey key names yourself. Those keys are used by UIKit to store additional information about the state of your view controller.

         --编码器对象 在编码和解码过程中 并不共享。具有可保存状态的每个对象都接收其自己的编码器对象。使用唯一的编码器意味着您不必担心密钥之间的命名空间冲突。但是,不要自己使用uiapplicationstateretorationbundleverionkey、uiapplicationstateretorationuserinterfaceidiomkey和uistateretorationviewcontrollerstoryboardkey密钥名称, 因为UIKit使用这些键来存储有关vc状态的附加信息。

For more information about implementing the encode and decode methods for your view controllers, see UIViewController Class Reference.

           --有关实现视图控制器的编码和解码方法的更多信息,请参见超链接: UIViewController类参考。

Tips for Saving and Restoring Your View Controllers         --保存和还原vc的一些提示

As you add support for state preservation and restoration in your view controllers, consider the following guidelines:

         --在你添加 对vc状态的保存和恢复 的支持时,请考虑以下指导原则:

  • Remember that you might not want to preserve all view controllers. In some cases, it might not make sense to preserve a view controller. For example, if the app was displaying a change, you might want to cancel the operation and restore the app to the previous screen. In such a case, you would not preserve the view controller that asks for the new password information.
            --请记住,您可能并不希望保留所有的vc。在某些情况下,保留vc可能没有意义。例如,如果app正在显示“更改”,而你可能想取消操作并将app还原到上一个屏幕。在这种情况下,您该不会保存请求新的密码信息的vc吧。

  • Avoid swapping view controller classes during the restoration process. The state preservation system encodes the class of the view controllers it preserves. During restoration, if your app returns an object whose class does not match (or is not a subclass of) the original object, the system does not ask the view controller to decode any state information. Thus, swapping out the old view controller for a completely different one does not restore the full state of the object.
                --避免在还原过程中交换vc类。“状态保持系统”对其保持的vc的类进行编码。在还原过程中,如果app返回的对象的类与原始对象不匹配(或不是其子类),则系统不会要求vc解码任何状态信息。因此,将旧的vc替换完全不同的vc之后, 系统恢复不了对象的完整状态。

  • The state preservation system expects you to use view controllers as they were intended. The restoration process relies on the containment relationships of your view controllers to rebuild your interface. If you do not use container view controllers properly, the preservation system cannot find your view controllers. For example, never embed a view controller’s view inside a different view unless there is a containment relationship between the corresponding view controllers.
            --”状态保持系统“希望您按照预期使用vc。恢复过程依赖于vc的包含关系来重建接口。如果未正确使用容器vc,则保存系统找不到vc。例如,除非相应的vc之间存在包含关系,否则永远不要将vc的视图嵌入到其他视图中。

 

 

Supporting Accessibility                                          --可访问性的支持

 

An accessible app is one that can be used by everyone—including those with a disability or physical impairment—while retaining its functionality and usability as a helpful tool. To be accessible, an iOS app must supply information about its user interface elements to VoiceOver, so that vision-impaired users can interact with those elements. UIKit objects are accessible by default, but there are still things you can do from the view controller’s perspective to address accessibility, including the following:

  • Ensure that every user element in your interface is accessible, including controls and static elements such as labels.

  • Ensure that accessible elements supply accurate and helpful information.

You can enhance the VoiceOver user’s experience in your app by setting the position of the VoiceOver focus ring programmatically, by responding to special VoiceOver gestures, and by observing accessibility notifications.

Moving the VoiceOver Cursor to a Specific Element

When your app presents new views onscreen, consider setting the position of the VoiceOver cursor. When the layout of a screen changes, the VoiceOver focus ring, also known as the VoiceOver cursor, resets its position to the first element displayed on the screen from left to right and top to bottom. Placing the cursor over a more appropriate element can speed the user’s navigation of your interface. For example, when pushing a new view controller onto a navigation controller’s stack, the VoiceOver cursor falls on the Back button of the navigation bar. You might want to move that cursor to the heading of the navigation bar or to an element on the newly pushed page.

To change the position of the cursor, post a UIAccessibilityScreenChangedNotification notification using the UIAccessibilityPostNotification function. The notification tells VoiceOver that the contents of the screen changed. When posting the notification, specify the element to which you want to assign the focus, as shown in Listing 6-1.

Listing 6-1Posting an accessibility notification can change the first element read aloud

  1. @implementation MyViewController
  2. - (void)viewDidAppear:(BOOL)animated {
  3. [super viewDidAppear:animated];
  4.  
  5. // The second parameter is the new focus element.
  6. UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification,
  7. self.myFirstElement);
  8. }
  9. @end

Layout changes, including those caused by rotations, reset the position of the VoiceOver cursor. When the layout of your view controller changes, post the notification UIAccessibilityLayoutChangedNotification. Like the UIAccessibilityScreenChangedNotification notification, you can specify the object that you want to become the new first element for VoiceOver.

Responding to Special VoiceOver Gestures

VoiceOver defines five special gestures for triggering app-specific actions.

  • Escape. A two-finger Z-shaped gesture that dismisses a modal dialog, or goes back one level in a navigation hierarchy.

  • Magic Tap. A two-finger double-tap that performs the most-intended action.

  • Three-Finger Scroll. A three-finger swipe that scrolls content vertically or horizontally.

  • Increment. A one-finger swipe up that increments a value in an element.

  • Decrement. A one-finger swipe down that decrements a value in an element.

Use these gestures to perform tasks that are specific to your views and view controllers. UIKit looks for a method that implements the method corresponding to the gesture. It searches for the method using the responder chain, starting with the element that has the VoiceOver focus. If no object implements the appropriate method, UIKit performs the default system action for that gesture. For example, the Magic Tap gesture plays and pauses music playback from the Music app if no Magic Tap implementation is found from the current view to the app delegate.

Although you can take any actions you want in your handlers, VoiceOver users expect your app’s actions to follow certain guidelines. Like any gesture, your implementation of a VoiceOver gesture should follow a pattern so that interaction with an accessible app remains intuitive.

Note

All special VoiceOver gesture methods return a Boolean value that determine whether to propagate through the responder chain. To halt propagation, return YES; otherwise, return NO.

Escape

Use the accessibilityPerformEscape method to handle the Escape gesture. For a view that overlays content—such as a modal dialog or an alert—use the method to dismiss the overlay. The function of the Escape gesture is like the function of the Esc key on a computer keyboard; it cancels a temporary dialog or sheet to reveal the main content. You might also use the Escape gesture to navigate back one level in a custom navigation hierarchy. You do not need to implement this gesture if you are already using a UINavigationController object, which already handles this gesture.

Magic Tap

Use the accessibilityPerformMagicTap method to handle the Magic Tap gesture. The Magic Tap gesture performs an often-used or most-intended action quickly. For example, in the Phone app, a Magic Tap picks up or hangs up a phone call, and in the Clock app, a Magic Tap starts and stops the stopwatch. You might use this gesture to trigger actions that are not necessarily relevant to the element that the VoiceOver cursor is highlighting. To handle Magic Tap gestures from anywhere in your app, implement the accessibilityPerformMagicTap method in your app delegate.

Three-Finger Scroll

Use the accessibilityScroll: method to scroll the content of a custom view when a VoiceOver user performs a three-finger scroll gesture. A custom view that displays the pages of a book might use this gesture to turn the page. The parameter passed to the method indicates the direction in which to scroll.

Increment and Decrement

Use the accessibilityIncrement and accessibilityDecrement methods to increment or decrement a value in the element. Elements with the UIAccessibilityTraitAdjustable trait must implement this method.

Observing Accessibility Notifications

UIKit sends accessibility notifications to inform your app about relevant events. The objects of your app can observe any relevant notifications and use them to perform appropriate tasks. For example, the iBooks app uses the UIAccessibilityAnnouncementDidFinishNotification notification to turn the page and continue reading when VoiceOver finishes speaking the last line on a page. This behavior provides a seamless, uninterrupted reading experience.

Use the default notification center to register as an observer for accessibility notifications. Listing 6-2 shows an example of a view that records whether the reading of an announcement was successful or was interrupted by the user.

Listing 6-2Registering as an observer for accessibility notifications

  1. @implementation MyViewController
  2. - (void)viewDidLoad
  3. {
  4. [super viewDidLoad];
  5.  
  6. [[NSNotificationCenter defaultCenter]
  7. addObserver:self
  8. selector:@selector(didFinishAnnouncement:)
  9. name:UIAccessibilityAnnouncementDidFinishNotification
  10. object:nil];
  11. }
  12.  
  13. - (void)didFinishAnnouncement:(NSNotification *)dict
  14. {
  15. NSString *valueSpoken = [[dict userInfo] objectForKey:UIAccessibilityAnnouncementKeyStringValue];
  16. NSString *wasSuccessful = [[dict userInfo] objectForKey:UIAccessibilityAnnouncementKeyWasSuccessful];
  17. // ...
  18. }
  19. @end

Another helpful notification to subscribe to is the UIAccessibilityVoiceOverStatusChanged notification. You can use that notification to detect when VoiceOver is toggled on or off. If this notification occurs while your app is suspended, you receive the notification when your app returns to the foreground.

For a list of accessibility notifications you can observe, see UIAccessibility Protocol Reference.

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值