我有问题,当我打开应用程序时,我可以看到最新结果,当我向左侧滑动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];
}
答案 0 :(得分: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];
}