watchOS-并发症显示上一个条目 [英] watchOS - Complication shows previous entry

查看:130
本文介绍了watchOS-并发症显示上一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个watchOS 3并发症,它显示了公共交通服务的出发时间.我用数组创建了一个数据模型,该数组包含带有stationName(字符串)和departureTime(NSDate)的Train对象.

I'm creating a watchOS 3 complication that shows departure times from a public transit service. I've created a data model with an array that contains Train objects with a stationName (String) and departureTime (NSDate).

我已经实现了getCurrentTimelineEntry()方法,并且条目显示在手表上.问题在于手表只显示前一个条目.例如,我有以下出发时间:

I've implemented the getCurrentTimelineEntry() method and the entries are showing on the watch. The problem is that the watch shows only the previous entry. For example, I've the following departure times:

 Train(startStation: "Station name", endStation: "Station name", departureTime: stringToDate(dateString: "2016-06-20 14:00")),
 Train(startStation: "Station name", endStation: "Station name", departureTime: stringToDate(dateString: "2016-06-20 14:30")),
 Train(startStation: "Station name", endStation: "Station name", departureTime: stringToDate(dateString: "2016-06-20 14:45")),

如果当前时间是14:10,则手表上仍会显示第一个条目(时间为14:00).直到当前时间是14:30,该条目才会显示出来.如果当前时间是14:10,我想在手表上看到14:30的出发时间.

If the current time is 14:10, the first entry (with time 14:00) is still showing on the watch. Until the current time is 14:30, then that entry is showing up. If the current time is 14:10, I would want to see the 14:30 departure time on my watch.

有人可以帮我这个忙,还是可以向我指出正确的方向?

Can anybody help me with this or point me in the right direction?

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
    if let train = dataProvider.getTrains().nextTrain() {
        handler(timelineEntryForTrain(train: train))
    } else {
        handler(nil)
    }
}


extension Array where Element : OnRailable {
    func nextTrain() -> Element?{
        let now = NSDate()
        for d in self {
            if now.compare(d.departureTime) == .orderedAscending{
                return d
            }
        }

        return nil
    }
}

推荐答案

您需要将每个条目的时间轴日期设置为上一个出发日期之后的一分钟.例如:

You need to set the timeline date for each entry to be one minute after the previous departure's date. For example:

  • 14:30出发的时间表日期应为14:01,
  • 14:45出发的时间表日期应为14:31,依此类推.

通过将即将到来的出发点作为当前时间线条目,距上次出发的一分钟之后,这将产生您想要的期望效果:

This will produce the desired effect you want, by making the upcoming departure be the current timeline entry, one minute after the previous departure:

  • 在14:00时,将显示14:00的当前出发时间,
  • 在14:01到14:30之间的任何时间,都会显示14:30出发的时间,
  • 在14:31到14:45之间的任何时间,都会显示14:45出发的出发时间,依此类推.

在WWDC 2015 使用ClockKit创建并发症中对此方法进行了解释.会议.在指定事件的时间表日期方面,演示者提到了如何

This approach is explained in the WWDC 2015 Creating Complications with ClockKit session. In terms of specifying the timeline dates for events, the presenter mentions how

我们应该将模板放在上一个活动的末尾,以便您有足够的时间开始下一个活动.

We should put the templates at the end of the previous event so you have adequate time to get to your next event.

现在,保罗在日历复杂性方面提到的天真的解决方案将使用比赛开始日期作为我们时间线输入的日期,但这将带来日历也具有缺点.是您将无法看清并发症以了解即将开始的游戏.

Now, the naive solution which Paul mentioned in the context of a calendar complication would be to use the match start date to be the date of our timeline entry, but that would have the drawback that it has for the calendar as well which is you wouldn't be able to look at your complication to see what game is about to start.

您将只能看到已经开始玩的游戏.

You would only be able to see what game has already started.

因此,我们实际上想对保罗做同样的事情,并将所有这些条目都向前移动.

So we actually want to do the same thing Paul did with the calendar and move all of these entries farther forward.

我们将在上一场比赛结束时开始每个条目.

We will have each entry start at the time when the previous match ended.

在您的情况下,每条记录都将在前一列火车出发后立即开始.

In your case, each entry would start right after the time when the previous train departed.

如何实现并发症:

  • 将时间轴的结束日期指定为当天的最后一班火车出发后的一分钟.当前时间过去后,这将使最后一列火车的出发细节显示为灰色.

  • Specify a timeline end date to be one minute after the last train of the day has departed. This will grey out the last train's departure details, once the current time has passed its departure time.

指定您支持向前旅行.

getTimelineEntriesForComplication(_:afterDate:limit:withHandler:)中即将出发的航班提供将来的时间线条目.要确定条目的时间轴日期,请使用条目的previousTrain()方法的离开详细信息.

Supply future timeline entries for upcoming departures in getTimelineEntriesForComplication(_:afterDate:limit:withHandler:). To determine the timeline date for an entry, use the departure details of the entry's previousTrain() method.

如果您的实时出发时间表有所更改(例如,由于某些延误),您可以重新加载时间轴以更改任何即将到来的出发时间.

If your realtime departure schedule changes (e.g., due to some delay), you can reload the timeline to change any of the upcoming departure times.

这篇关于watchOS-并发症显示上一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆