我创建了2个单独的图,现在我想检测用户何时更改其中一个(滚动)的xRange并更改第二个图的xRange。
我一直在尝试使用plotSpace:willChangePlotRangeTo:forCoordinate:
并在userInfo中发布带有新范围和plotID的通知。
父viewController侦听通知并更改第二个绘图的xRange,但是:
我收到滞后,第二个情节是"摇摇欲坠"而且它经常以不同的范围结束。
当我这么快的时候,没有观察到颤抖(只有滞后)。
我该如何解决这个问题? 你可以在这里看到它: https://www.youtube.com/watch?v=f6VR7rRBuCU&feature=youtu.be
在parentViewController中:
-(void)receivedNewRangeNotif:(NSNotification*)notification {
NSDictionary* userInfo = notification.userInfo;
NSString* identifier = [userInfo objectForKey:@"identifierKey"];
CPTPlotRange* newRange = [userInfo objectForKey:@"newRangeKey"];
NSLog(@"receivedNewRangeNotif: %@",identifier);
if ([identifier isEqualToString:@"firstItemID"]) {
_secondItem.postNotifications = NO;
CPTXYPlotSpace *secondPlotSpace = (CPTXYPlotSpace *)_secondItem.graph.defaultPlotSpace;
secondPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:newRange.location length:newRange.length];
_secondItem.postNotifications = YES;
}
else if ([identifier isEqualToString:@"secondItemID"]) {
_firstItem.postNotifications = NO;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)_firstItem.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:newRange.location length:newRange.length];
_firstItem.postNotifications = YES;
}
在plotItem中:
- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate {
if (_postNotifications) {
NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:newRange, @"newRangeKey",
self.identifier, @"identifierKey", nil];
NSLog(@"sendingNewRangeNotif");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TODOnotifRangeChanged" object:nil userInfo:userInfo];
}
//DLog(@"changing plotRange");
//disables possibility to change y size
if (CPTCoordinateY == coordinate) {
return [self automaticYRangeWithCustomMinY:_customMinY CustomMaxY:_customMaxX customLengthY:_customYLength];
}
//length of range is specified, one can change only the startpoint
else if (CPTCoordinateX == coordinate) {
CPTPlotRange* plotRange = [CPTPlotRange plotRangeWithLocation:newRange.location length:_customXLength];
return plotRange;
}
//returning nil is never reached :D
return nil;
}
编辑: 我所观察到的是,当仅在x中非常准确地滚动时,这些图表正在很好地移动。只有当一个人也在Y(甚至是单个像素)中滚动时,它才开始摇晃。
答案 0 :(得分:0)
好的,我已经通过更改发送通知的时刻解决了这个问题:
- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate {
NSLog(@"changing plotRange: %ld",(long)coordinate);
if (CPTCoordinateY == coordinate) {
return [self automaticYRangeWithCustomMinY:_customMinY CustomMaxY:_customMaxY customLengthY:_customYLength];
}
else if (CPTCoordinateX == coordinate) {
if (_postNotifications) {
NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:newRange, @"newRangeKey",
self.identifier, @"identifierKey", nil];
NSLog(@"sendingNewRangeNotif");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TODOnotifRangeChanged" object:nil userInfo:userInfo];
}
CPTPlotRange* plotRange = [CPTPlotRange plotRangeWithLocation:newRange.location length:_customXLength];
return plotRange;
}
return nil;
}