1. 程式人生 > >iWatch開發:UI 組件說明

iWatch開發:UI 組件說明

ctf jsb span fill 關聯 clas ring imageview 它的

技術分享圖片


WKInterfaceLabel使用

WKInterfaceLabel 相似iOS 組件中的UILabel, 可通過使用 setText 的方式來設置詳細的值。這裏就不做多闡述。

WKInterfaceImage 使用

WKInterfaceImage 相似於 UIImageView, 使用時,可用setImage 來設置圖片。

它的接口例如以下:

@class UIImage;

@protocol WKImageAnimatable <NSObject>

// Play all images repeatedly using duration specified in interface description.
- (void)startAnimating;

// Play a subset of images for a certain number of times. 0 means repeat until stop.
- (void)startAnimatingWithImagesInRange:(NSRange)imageRange duration:(NSTimeInterval)duration repeatCount:(NSInteger)repeatCount; - (void)stopAnimating; @end WK_CLASS_AVAILABLE_IOS(8_2) @interface WKInterfaceImage : WKInterfaceObject <WKImageAnimatable> - (void)setImage:(nullable UIImage *)image; - (void
)setImageData:(nullable NSData *)imageData; - (void)setImageNamed:(nullable NSString *)imageName; - (void)setTintColor:(nullable UIColor *)tintColor; @end NS_ASSUME_NONNULL_END

WKInterfaceTable

相比於iOS 中的UITableViewController來說,iwatch中的WKInterfaceTable功能就簡單多了。它沒有delegate 也無需設置數據源。

在組件庫中選中WKInterfaceTable 拖入Interface.storyboard中,並在代碼中形成相應的關聯,這裏有一點要註意一下,那就是這個必需要設置 Row Controller 的identifier, 不然數據就無法載入出來。

在這裏就使用靜態的數據讓這個Table 控件來載入出來。 代碼例如以下:

NSMutableDictionary *phoneContact = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"13776054770", @"約翰",
                                         @"13776054770", @"約翰1",
                                         @"13776054771", @"約翰2",
                                         @"13776054772", @"約翰3",
                                         @"13776054773", @"約翰4", nil];

    [_contactTableV setNumberOfRows:phoneContact.count withRowType:@"MyTableRowControl"];
    NSArray *namesArray = phoneContact.allKeys;

    for(int i = 0; i < phoneContact.count; i++){
        NSString *name = [namesArray objectAtIndex:i];
        NSString *phone = [phoneContact objectForKey:name];

        MyTableRowControl *row = [_contactTableV rowControllerAtIndex:i];
        [row.contactName setText:name];
        [row.phoneNo setText:phone];
    }

table點擊事件,通過重寫實現InterfaceController 來處理:

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex{
    NSLog(@"我點擊了 %ld 行", (long)rowIndex);
}

技術分享圖片

WKInterfaceButton

iWatch button控件。可用的API 例如以下:

NS_ASSUME_NONNULL_BEGIN

@class UIImage, UIColor;

WK_CLASS_AVAILABLE_IOS(8_2)
@interface WKInterfaceButton : WKInterfaceObject

- (void)setTitle:(nullable NSString *)title;
- (void)setAttributedTitle:(nullable NSAttributedString *)attributedTitle;

- (void)setBackgroundColor:(nullable UIColor *)color;
- (void)setBackgroundImage:(nullable UIImage *)image;
- (void)setBackgroundImageData:(nullable NSData *)imageData;
- (void)setBackgroundImageNamed:(nullable NSString *)imageName;

- (void)setEnabled:(BOOL)enabled;

@end

NS_ASSUME_NONNULL_END

button點擊事件,能夠通過storyboard 拖拽的方式來實現,也可通過代碼來實現。

WKInterfaceDate

日期控件,可用API 例如以下:

NS_ASSUME_NONNULL_BEGIN

@class UIColor;

WK_CLASS_AVAILABLE_IOS(8_2)
@interface WKInterfaceDate : WKInterfaceObject

- (void)setTextColor:(nullable UIColor *)color;

- (void)setTimeZone:(nullable NSTimeZone *)timeZone;
- (void)setCalendar:(nullable NSCalendar *)calendar;

@end

NS_ASSUME_NONNULL_END

WKInterfaceTimer

時間控件, 可用 API 例如以下:

NS_ASSUME_NONNULL_BEGIN

@class UIColor;

WK_CLASS_AVAILABLE_IOS(8_2)
@interface WKInterfaceTimer : WKInterfaceObject

- (void)setTextColor:(nullable UIColor *)color;

- (void)setDate:(NSDate *)date; // count up/down from current date to this date
- (void)start;
- (void)stop;

@end

NS_ASSUME_NONNULL_END

好了。祝大家生活愉快。

多多收獲友誼和愛情。

假設想獲取很多其它的訊息。請掃描下方二維碼關註我的微信公眾號:

技術分享圖片

iWatch開發:UI 組件說明