美文网首页
四、iWatch常用控件基本用法之Timer

四、iWatch常用控件基本用法之Timer

作者: Dosun | 来源:发表于2017-03-07 17:49 被阅读1348次

    本文将讲WKInterfaceTimer和WKInterfaceDate。从其属性和常见的方法入手,鄙人不才,只好从这此入手。

    1.WKInterfaceTimer

    1.1WKInterfaceTimer方法
    @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
    
    1.2WKInterfaceTimer用法
    @interface InterfaceController()
    
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceTimer *timer;
    
    @end
    
    
    @implementation InterfaceController
    
    - (void)awakeWithContext:(id)context {
        [super awakeWithContext:context];
        // Configure interface objects here.
        
        //设置计时器的颜色
        [self.timer setTextColor:[UIColor redColor]];
        
        //设置计时的时间
        [self.timer setDate:[NSDate dateWithTimeIntervalSinceNow:60]];
        
        //开始计时,如果不开始的话,计时器不会有反应
        [self.timer start];
    }
    

    下图是上面代码生成的UI界面。


    Snip20170307_27.png

    2. WKInterfaceDate

    2.1 WKInterfaceDate方法
    @interface WKInterfaceDate : WKInterfaceObject
    
    //日期的字体颜色
    - (void)setTextColor:(nullable UIColor *)color;
    
    //设置时区
    - (void)setTimeZone:(nullable NSTimeZone *)timeZone;
    
    //生成日期样式
    - (void)setCalendar:(nullable NSCalendar *)calendar;
    
    @end
    
    2.2 WKInterfaceDate 简单用法
    - (void)awakeWithContext:(id)context {
        [super awakeWithContext:context];
        
        //设置字体颜色
        [self.date setTextColor: [UIColor blueColor]];
        
        //设置时区,我们是东八区 ,此处感觉不怎么会设置
        NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:12];
        [self.date setTimeZone:gmt];
    
        //设置日期
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        // Configure interface objects here.
        //暂时不能指定日期的格式
        [self.date setCalendar:calendar];
    }
    

    下图是代码加载的UI界面


    Snip20170307_28.png
    2.3 WKInterfaceDate storyBoard 设置其日期格式

    如下面箭头标记,在Date -> Format 中设置 Custom,然后手动修改日期样式。


    Snip20170307_29.png

    结果如下图所示

    Snip20170307_30.png

    Over!
    May maker help us all!!

    相关文章

      网友评论

          本文标题:四、iWatch常用控件基本用法之Timer

          本文链接:https://www.haomeiwen.com/subject/tmmmgttx.html