Creating Your First Mac AppAdding Polish 进一步优化

Presentation is a critical component of a successful Mac OS X app. Although the app behaves “correctly” now, there are a couple more changes you can make to improve the overall polish.

演示是一个成功的Mac OS X应用程序的重要组成部分。尽管应用程序现在能很正确地(correctly)运行,但是我们还能做更多改变来使应用更完美。

Add a Number Formatter

添加数字格式化

If you move the slider, the text field displays all the digits of the corresponding float value—for example, 3.141592653589793. Typically, all these digits are unnecessary—3.14 would probably suffice. You can use a formatter object to display a rounded representation of the number. Using a formatter has two additional benefits:

如果你拖动滑动条,文本框里显示的float数值显示所有数字-- 比如,3.141592653589793. 通常情况下,所有的这些数字都不需要--3.14可能就够了。你可以使用一个格式化(formatter)对象来显示一个舍入的数字。 使用格式化有2个额外的好处:

  • A formatter can impose maximum and minimum values on the number entered.

    格式化能对输入数字进行最大化和最小化的处理。

  • A formatter presents the number appropriately for the user’s locale. For example, if the locale is French, users see 3,14 instead of 3.14.

    格式化根据用户语言环境显示合适的数字形式。举个例子,本地语言环境是法语,用户将看到3,14,而不是3.14 。

一、To add a number formatter
  添加数字格式化
  1. In the project navigator, select the MainMenu.xib file to display the TrackMix window.

    在项目导航(project navigator),选择MainMenu.xib文件,显示TrackMix窗口。

  2. Drag a number formatter from the Object Library into the text field.

    从对象库(Object Library)拖动 数字格式化(number formatter)对象到文本框内。

    image: ../Art/30a_dragnumberformatter.png
  3. To configure the formatter, click the disclosure button at the bottom left of the canvas to show the outline view of the nib file.

    为了设置格式化(formatter),点击画布(canvas)底部左边的展开按钮(disclosure button),显示nib文件的大纲视图(outline view)。

    image: ../Art/30b_outlinedisclosurebutton.png
  4. If necessary, select the number formatter (use the disclosure triangles to drill down to it under the text field).

    如有需要,选择数字格式化(number formatter)---在文本字段(text field)向下展开三角形(disclosure trangles)。

  5. Show the Attributes inspector to configure the number formatter.

    打开属性检查器(Attributes inspector),设置数字格式化(number formatter)

    image: ../Art/30_numberformatter_attrinspector.png

    Normally, you should choose one of the standard formats—short, medium, or long—because these standard formats take the user’s preferences into account. In this case, you want a custom formatter that shows just two decimal places.

    通常情况下,你应该选择一个标准格式(standard formats)-- short , medium, 或 long -- 因为这些标准格式考虑用户的喜好。 在这里,你想要一个自定义的格式化,只显示小数点后2位。

  6. Use the defaults, except for the following settings:

    除了以下设置,其他都使用默认设置:

    • Behavior: OS X 10.4+ Custom

    • Minimum: 0

    • Maximum: 11

    • Allows Floats: Yes

    • Localize: Yes

    • Fraction Digits: Minimum 0, Maximum 2

  7. Click the Run button.

    点击“Run“按钮

    You should find that that the number displayed in the text field is correctly formatted and that the text field does not accept numbers outside of the given range.

    你应该能发现,现在文本框里的数字按正确格式显示,文本框不接受超出给定值范围的数字。

Change the Window’s Resize Behavior

更改窗口的Resize行为

Currently, if you resize the window, all the UI elements stay centered, the text field and button remain the same size, and the slider resizes proportionately to the window. Unfortunately, you can resize the window so small that you can’t use the slider. This behavior leads to poor layout, as illustrated in this image.

现在,如果你调整窗口大小,所有的UI元素保持居中,文本字段和按钮保持相同的大小,滑动条按窗口比例调整大小。不幸的是,你可以把窗口调整到这么小,但是你不能使用滑动条。 这种行为会导致不良布局,正如下图所示:

image: ../Art/31_badwindowresize.png

Note: The UI elements resize and reposition themselves appropriately when the window size changes. That’s because they use the default Cocoa Auto Layout settings. Cocoa Auto Layout is a rule-based system that defines spacial relationships between UI elements. In this tutorial, it is not necessary to change these settings.

 注意: 当窗口大小改变时,UI元素自动调整大小并重新布局。那是因为它们使用了默认的Cocoa 自动图层设置(Cocoa Auto Layout settings)。Cocoa Auto Layout 是一个定义UI元素之间空间关系(spacial relationships)的基于规则的系统。本教程中,无需改变这些设置。

You can correct this problem by changing the window’s resize behavior. One option is to prevent the window from being resized in order to keep the sliders a constant size. Or you can just set the minimum size of the window.

你可以通过更改窗口大小调整行为,改正这个问题。 一个选择是为了保持滑动条的恒定尺寸,阻止窗口大小的调整。或者你可以只设置窗口的最小值。

一、To change the window’s resize behavior
  1. Select the MainMenu.xib file to display the TrackMix window.

    选择MainMenu.xib文件,显示TrackMix 窗口。

  2. Select the window.

    选择窗口

  3. In the utility area, show the Size inspector by clicking the icon that looks like this: image: ../Art/size_inspector_icon.jpg

    在实用区(utility area), 点击下图像梯子的一样的按钮,显示 尺寸检查器(Size inspector)。

  4. Select the Minimum Size checkbox in Constraints.

    在Constrainsts处,选中Minimum Size

    image: ../Art/32_windowsizeinspector.png

Test the App Using the Cocoa Simulator

用Cocoa 模拟器测试应用

To save time, you can test the changes you’ve made directly without having to build and run the app. Using Cocoa Simulator, you can test whether the UI elements behave as you expect. Note, though, that only the UI elements are recreated. Cocoa Simulator doesn’t create an app delegate for you or simulate any of your app logic. So although you can test the resizing behavior and click (for example) the button, the slider and text field do not set the volume to zero.

为了节省时间,你可以不用构建和运行应用,直接测试你你做的改变。 使用Cocoa模拟器,你可以测试是否UI元素如你预期的那样工作。不过,请注意,只有UI元素被重新创建。Cocoa模拟器不会为你创建一个app delegate ,也不会模拟任何你的应用程序逻辑(app logic)。 因此尽管你能测试尺寸调整行为,也能点击按钮(举个例子),但是滑动条和文本框不会把音量设置为0。

一、To test the user interface using Cocoa Simulator
  在Cocoa模拟器设置用户界面
  1. Choose Editor > Simulate Document.

    选择 Editor > Simulate Document。

    Xcode launches Cocoa Simulator. This loads the nib file and allows you to interact with it directly.

    Xcode 启动 Cocoa 模拟器(Cocoa Simulator)。这样就加载了nib文件,并允许你跟它直接交互。

  2. To quit the simulator, choose Cocoa Simulator > Quit Cocoa Simulator.

    退出(quit) 模拟器,选择Cocoa Simulator > Quit Cocoa Simulator。

If the interface didn’t behave as you expected, review the Autosizing settings you made and try again. Otherwise, test the actual app.

如果界面不像你预期的那样工作,查看你做的Autosizing settings(自动尺寸调整设置),重新试一次。否则,测试真实应用。

二、To test the app
  • Click the Run button.

    点"Run"按钮

    Make sure that the user interface resizing behavior is the same as it was in Cocoa Simulator.

    确保用户界面尺寸调整行为(the user interface resizing behavior)跟在Cocoa模拟器(Cocoa Simulator)里表现的一样。

Recap(概括)

You added a number formatter to the text field, fixed a problem when resizing the window, and learned how to use Cocoa Simulator to test the user interface without having to compile any code.

你给文本框添加了一个数字格式化(number formatter),解决了一个当调整窗口尺寸时出现的问题,并学习了如何使用Cocoa模拟器来测试用户界面而不需要编译任何代码。

原文地址:https://www.cnblogs.com/patientAndPersist/p/3114097.html