1.UI Construction
Direct development, you will find Apple Watch does not support AutoLayout,WatchKit There is a class called Wkinterfacegroup, at first glance like UIView, but the goods are actually used for layout. Pull a wkinterfacegroup out of storyboard and have a layout property in the property Inspector (Attributes Inspector), by setting it to horizontal or vertical, You can have all the sub-views in the group arranged horizontally or vertically. Combined with autolayout knowledge, you can easily meet the requirements.
First of all, watch's screen is small, currently only 38mm and 42mm two size, we can not do in this limited space to do very complex interface effect, therefore, in the interface development, should follow the principle of easy to use and at a glance. Watch on the layout of the way is a flat stacking, there is no frame, no longer have constraints, the layout of the control is just a plane stacked next to each other, also can not overlap. But in watch, we provide a way for the group to be able to embody freedom and individuality in the layout. How to use it?
(1) First Simple layout introduction
Is that when we do not use Group, watch layout is the most basic stacking method, from top to bottom row, for example, we add four labels, the effect is as follows:
By changing the order in which the labels are added, you can change their upper and lower positions:
There is no limit to the height of the layout in this way, we can keep the line down, and on watch, there will be a sliding effect:
(2) Use Group for complex interface layout
With the above layout, we can only arrange the layout vertically, this does not meet our needs, WatchKit provides the layout of the model: Group.
As you can see, group is dividing the screen into partitions, we can set the arrangement of each partition, such as horizontal or vertical, through this way, to complete the complex watch interface layout, such as the following effect:
the effect of an interface, is to add three group on the screen, the top Gorup is set to the horizontal arrangement mode, in which two buttons and a split line is added, the middle group is a vertical arrangement mode, put a selector and a button, The bottom group is also the horizontal arrangement mode, with a button and a time bar. Group in the interface layout, not only can play the role of the partition screen, it can also set some properties to make the layout more beautiful. In the Settings menu on the right side of storyboard, we can manipulate these properties:
Layout: Set up layouts mode, divided into horizontal layout and vertical layout two kinds
Insert: You can set the content area offset, and with this property, we can leave the controls that are populated around white
Spacing: The spacing of the controls that are populated
BackGround: Set background pattern for group
Mode: Sets the fill mode of the background pattern
Animate: Animate when present
Color: Sets the background color of group
Radius: Sets the circle angle of the group
(3) Position and size settings for controls in layouts
In the iphone, we use frame or constraints to control the position and size of the control, it is much simpler in watch, the size and position are fixed patterns, we just need to make some settings.
1 control of control size
For the size of the control, there are three modes, and the width and height of the control are set by these three modes:
Relative to Container: its size is set according to the size of the container. For example, if set to 0.5, the current control's size is half of its group.
Size to Fit content: the size of itself is related to its contents, for example, the number of words in a label determines the size of the label.
Fixed: Manually set a constant value.
2. Control of Control position
Because the watch interface is very concise, the position setting for the control is set by the horizontal and vertical two dimensions, by setting the properties of each dimension to control its position in the group that holds it:
Horizontal:left (left), Center (centre), right
Vertical:top (upper), Center (center), Bottom (bottom)
Attention:
As for the picture footage, you can see that there is a assets.xcasssets group in the extension and Apps folder, which can only be used if the footage is in the group watch under the app folder.
use of 2.UI controls
(1) Table
The wkinterfacetable in WatchKit is different from UITableView, without DataSource and delegate, and can only be populated and presented in the active way. For each of the different types of cells, it is necessary to construct a Rowcontroller (inherited from NSObject), which Rowcontroller is responsible for populating this type of row with what needs to be presented.
Here's how:
- -(ID) Rowcontrolleratindex: (Nsinteger) index;
Call this method to get the rowcontroller of the specified row, and then call the Rowcontroller custom method to render. For example:
- -(void) Configurerowwithdatamodel: (ID) Datamodel;
In addition, if you want to insert rows and delete rows, you should call them before the preceding operations:
- -(void) Insertrowsatindexes: (Nsindexset *) rows Withrowtype: (NSString *) RowType;
- -(void) Deleterowsatindexes: (Nsindexset *) rows;
Note: If you want to refresh UITableView in iOS development, you can just update the data source and then call the Reloaddata method. But as said before, Wkinterfacetable does not actively refresh the data, only by calling the [table Setrowtypes:nil] method to clear all the data, and then re-populated with the new data source. just let it go, assuming that you are now in view controller A and then push to view Controller B, it is useless if you refresh the wkinterfacetable in a in B. But not completely useless, after many tests, I found that the data source was actually updated, but the content on the interface was not refreshed. In other words, the data source and interface are not consistent. A temporary workaround is to record a marker bit when it needs to be refreshed, and then refresh it at the time of the willactivate based on the token bit. One obvious side effect is that the refresh will produce a bounce, and the later version of Apple should fix the problem.
There is a big difference between tableview in watchOS and TableView in iOS, and before development, we should first understand the limitations and features of table in watchOS. Here are some of the special things I've summed up in table watchOS:
1, table only the concept of the row, there is no concept of partitioning, there is no concept of the tail view.
2, you can create multiple table, to achieve the effect of partitioning.
3, because watch on the layout through the GRUOP to adapt, so there is no high-line setting.
4, table does not have a proxy, all rows of data are used statically configured way.
5. Clicking on a row trigger in table is done by rewriting the method in interface.
(2) Create a table
Drag into your table in storyboard (you don't want plain code), as follows:
Pull two labels on table:
Each table contains a tablerowcontroller, and actually the controls on our table are managed by this tablerowcontroller, so if we need to control the content on the TableRow in the code, We need to create a file as a table Tablerowcontroller:
Modify the Tablerowcontroller class in storyboard to the class we created and specify a identifier:
We then associate two labels to the Tablerowcontroller:
| 123456 |
import WatchKitclassTableRowController: NSObject { @IBOutlet var numberLabel: WKInterfaceLabel! @IBOutlet var titleLabel: WKInterfaceLabel!} |
To associate a table to the Interfacecontroller:
| 12345 |
classInterfaceControllerMain: WKInterfaceController { @IBOutlet var Table: WKInterfaceTable!} |
Here we begin to configure the table in interface, first we can look at the methods and properties in Wkinterfacetable:
| 1234567891011121314151617181920 |
publicclass WKInterfaceTable : WKInterfaceObject { //设置行的类型,数组中对应存放行的类型,数组元素的个数,就是行数 /* 通过这个方法,我们可以创建每一行样式都不同的table,行的类型 实际上就是我们刚才用到的TableRowController,我们可以进行自定义 */ public func setRowTypes(rowTypes: [String]) //设置行数和类型 用于创建单一行类型的table publicfunc setNumberOfRows(numberOfRows: Int, withRowType rowType: String) // repeating row name //这个get方法获取行数,用于我们遍历table中的行,进行内容设置 public var numberOfRows: Int { get } //这个方法会返回某一行,我们可以获取到后进行内容设置 publicfunc rowControllerAtIndex(index: Int) -> AnyObject? //插入一行 publicfunc insertRowsAtIndexes(rows: NSIndexSet, withRowType rowType: String) //删除一行 publicfunc removeRowsAtIndexes(rows: NSIndexSet) //滑动到某一行 publicfunc scrollToRowAtIndex(index: Int)} |
Knowing the above method, we can see that the table configuration of watchOS is very simple and easy to use, for example, we configure the following:
| 1234567891011121314151617 |
@IBOutlet var Table: WKInterfaceTable! override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) let dic:Dictionary<String,String> = ["中国建设银行":"¥1000","中国农业银行":"¥5000","中国银行":"20000","招商银行":"¥401","中国邮政储蓄":"1100"] //设置行数与类型 Table.setNumberOfRows(dic.count, withRowType: "TableRowController") //遍历进行设置 let titleArray:Array<String> = Array(dic.keys) forvar i=0 ; i < dic.count ; i++ { let row:TableRowController = Table.rowControllerAtIndex(i) as! TableRowController row.titleLabel.setText(titleArray[i]) row.numberLabel.setText(dic[titleArray[i]]) row.numberLabel.setTextColor(UIColor.grayColor()) } // Configure interface objects here. } |
Such a display of the bank card balance of the interface we created, the effect is as follows:
(3) Click events on table
As we mentioned above, table does not have the so-called proxy method, click on the row, we are also in two ways to carry out the logical jump, one is in the storyboard, we jump through the pull line, when the need to pass value, we need to implement the following methods in interface:
| 1 |
publicfunc contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? |
In another way, we can override the following methods in implementing Interfacecontroller to handle table click events:
| 1 |
publicfunc table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) |
Either way, we can use the Parameters table and rowindex to confirm that the click is exactly the table and which row, to pass the value and to handle our logic.
APPLEWATCH___ Learning Notes (ii) UI building and logic processing