我正在努力解决试图在滚动视图中平移的问题。我创建了一段测试代码(如下)来解决这个问题(这就是它看起来如此奇怪的原因——请原谅我)。
快速总结:
我在 scrollView 中有一系列 EasyTableView 水平滚动条。我已经设置了一个双指平移识别器来启动平移。垂直滚动工作得很好,滚动器内的水平滚动工作得很好,并且检测到两个手指的平移手势并调用委托方法。但是,滚动条不会在屏幕上平移。
我注意到原点 x 和原点 y 始终为零,即使滚动条的初始位置绝对不是 - (下面的输出)。
显然,我有一个参考框架问题,但我一生都无法弄清楚。
代码:
@synthesize panRecognizer, horizontalView;
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat yOffset = 0;
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
EasyTableView *ehorizontalView;
self.view=scrollView;
for(int i=0; i<5; i++){
UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
twoFingerPan.minimumNumberOfTouches = 2;
twoFingerPan.maximumNumberOfTouches = 2;
CGRect frameRect = CGRectMake(0, yOffset, PORTRAIT_WIDTH, HORIZONTAL_TABLEVIEW_HEIGHT); // do any further configuration to the scroll view
EasyTableView *view = [[EasyTableView alloc] initWithFrame:frameRect numberOfColumns:NUM_OF_CELLS ofWidth:VERTICAL_TABLEVIEW_WIDTH];
ehorizontalView = view;
// test code
if(i==1){
self.horizontalView=horizontalView;
self.panRecognizer=twoFingerPan;
}
ehorizontalView.delegate = self;
[ehorizontalView addGestureRecognizer:twoFingerPan];
ehorizontalView.tableView.backgroundColor = TABLE_BACKGROUND_COLOR;
ehorizontalView.tableView.allowsSelection = YES;
ehorizontalView.tableView.separatorColor = [UIColor colorWithRed: (CGFloat)0 green:(CGFloat)51/255 blue:(CGFloat)153/255 alpha:(CGFloat)0.3];
ehorizontalView.cellBackgroundColor = [UIColor colorWithRed:(CGFloat)0 green:(CGFloat)51/255 blue:(CGFloat)153/255 alpha:(CGFloat)0.3];
ehorizontalView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[ehorizontalView addGestureRecognizer:twoFingerPan];
[self.view addSubview:ehorizontalView];
yOffset+=180;
}
[scrollView setContentSize:CGSizeMake( 320, yOffset+460)];
}
- (void)panDetected:(UIPanGestureRecognizer *)pr
{
EasyTableView *v;
if(panRecognizer==pr){
NSLog(@"This works here - recognized: %f", [pr translationInView:self.view].y);
v=self.horizontalView;
CGPoint translation = [pr translationInView:self.view];
CGRect r=v.frame;
NSLog(@"Coordinates rx %f ry %f tx %f ty %f",r.origin.x,r.origin.y,translation.x,translation.y);
r.origin.y += translation.y;
r.origin.x=0;
[v setFrame:r];
[pr setTranslation:CGPointZero inView:self.view];
}
}
我也有一些 panDetected 的输出:
2012-05-04 08:43:48.317 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx 0.000000 ty 0.500000
2012-05-04 08:43:48.329 ScrollerTest[1920:707] This works here - recognized: 0.500000
2012-05-04 08:43:48.334 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx 0.000000 ty 0.500000
2012-05-04 08:43:48.358 ScrollerTest[1920:707] This works here - recognized: 0.500000
2012-05-04 08:43:48.365 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx 0.000000 ty 0.500000
2012-05-04 08:43:48.371 ScrollerTest[1920:707] This works here - recognized: 0.000000
2012-05-04 08:43:48.373 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx -0.500000 ty 0.000000
2012-05-04 08:43:48.550 ScrollerTest[1920:707] This works here - recognized: 0.000000
2012-05-04 08:43:48.554 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx -0.500000 ty 0.000000
2012-05-04 08:43:48.789 ScrollerTest[1920:707] This works here - recognized: 1.000000
2012-05-04 08:43:48.793 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx 0.000000 ty 1.000000
2012-05-04 08:43:48.805 ScrollerTest[1920:707] This works here - recognized: 0.000000
2012-05-04 08:43:48.809 ScrollerTest[1920:707] Coordinates rx 0.000000 ry 0.000000 tx 0.000000 ty 0.000000
非常感谢您提供的任何帮助!
谢谢你。