0

我有问题,当我打开应用程序时,我可以看到最新结果,当我向左滑动 uiview 时,我可以看到以前的结果,我面临的问题是我想向右滑动以再次获得最新结果。

这是向左滑动的代码

-(void)swipeLeft
{


    NSLog(@"Mag Date:%@",magCurDate);
    NSDate *tempDate;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    tempDate = [formatter dateFromString:magCurDate];

    NSLog(@"temp Date:%@",tempDate);

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:-1];
    [dateComponents setHour:8];
    NSDate *targetDate = [gregorian dateByAddingComponents:dateComponents toDate:tempDate  options:0];

    NSLog(@"target Date:%@",targetDate);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"ddMMYYYY"];
    NSString *sDate = [df stringFromDate:targetDate];

    NSLog(@"sDate Date:%@",sDate);

    [self LoadMagnumResult:sDate];

}

这是我向右滑动的代码,但我无法停止最新的结果,我希望最后一次滑动是最新的结果。

-(void)swipeRight
{


    NSLog(@"Mag Date:%@",magCurDate);
    NSDate *tempDate;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    tempDate = [formatter dateFromString:magCurDate];

    NSLog(@"temp Date:%@",tempDate);

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:+1];
    [dateComponents setHour:8];
    NSDate *targetDate = [gregorian dateByAddingComponents:dateComponents toDate:tempDate  options:0];

    NSLog(@"target Date:%@",targetDate);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"ddMMYYYY"];
    NSString *sDate = [df stringFromDate:targetDate];

    NSLog(@"sDate Date:%@",sDate);

    [self LoadMagnumResult:sDate];

}
4

1 回答 1

0

我假设最新的结果不可能在将来。换句话说,“最新结果”的最大日期是今天。我还假设向左滑动应该增加信息直到今天。如果你想左滑跳转到今天,那么代码就更简单了。让我知道这是否是你的意思。因为我没有你的应用程序,所以我只创建了一个内部变量来存储一个名为 testDate 的日期。我还在屏幕上放了一个标签,并将其命名为 outputLabel。您可以修改代码以满足您的需求。

最后,我更改了方法,使右滑及时回退,左滑及时前进。用户已经习惯于将向右滑动视为向后滑动(回到 Web 浏览器等)。请记住,向右滑动从左侧开始并在右侧结束。

首先,我将构建一个辅助方法来将日期转换为字符串:

- (NSString*)stringFromDate:(NSDate*)date
{
    //Helper method to return a string from a date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yy"];
    return [formatter stringFromDate:date];
}

这是我的右滑方法。请注意我如何使用时间间隔来增加日期。这可以从您的方法中节省几行代码。除非我错了,否则您似乎正在尝试将每次滑动的日期增加 24 小时。我还在这两种方法之前创建了一个常量,因此如果您想更改每次滑动递增的天数,则不必搜索代码。

#define kDaysPerSwipe 1

- (void)swipeRight:(UISwipeGestureRecognizer*)swipeRight
{
    NSLog(@"right swipe received");

    //Number of days to add
    int numberOfDaysToAdd = kDaysPerSwipe;

    //Generate a new date
    NSDate *newDate = [self.testDate dateByAddingTimeInterval:-(60*60*24*numberOfDaysToAdd)];

    //Store the new date
    self.testDate = newDate;

    //Output the new date
    self.outputLabel.text = [self stringFromDate:self.testDate];
}

这是我的左滑方法。请注意我如何将 newDate 与今天进行比较,然后在继续之前只存储两者的 EARLIER 日期。

- (void)swipeLeft:(UISwipeGestureRecognizer*)swipeLeft
{
    NSLog(@"left swipe received");

    //Number of days to add
    int numberOfDaysToAdd = kDaysPerSwipe;

    //Generate a new date
    NSDate *newDate = [self.testDate dateByAddingTimeInterval:(60*60*24*numberOfDaysToAdd)];

    NSLog(@"newDate: %@", [self stringFromDate:newDate]);

    //Compare newDate to today.  Return whichever is earlier.
    newDate = [[NSDate date] earlierDate:newDate];

    self.testDate = newDate;

    //Show the output on the screen
    self.outputLabel.text = [self stringFromDate:self.testDate];
}
于 2012-09-25T23:47:47.860 回答