我是 Objective-c的新手.我创建UIScrollView对象并使用以下代码添加到我的视图中: height = self.view.frame.size.height;width = self.view.frame.size.width;scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
height = self.view.frame.size.height; width = self.view.frame.size.width; scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; scrollbar.delegate = self; scrollbar.backgroundColor = [UIColor whiteColor]; scrollbar.maximumZoomScale = 1.0; scrollbar.minimumZoomScale = 1.0; scrollbar.clipsToBounds = YES; scrollbar.showsHorizontalScrollIndicator = YES; scrollbar.pagingEnabled = YES; [scrollbar setContentSize:CGSizeMake(width*4, height*4)]; [self.view addSubview:scrollbar]; for (int i = 1; i <= 4; i++) { first = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; first.view.frame = CGRectMake((i-1)*width, 0, width, height*4); [scrollbar addSubview:first.view]; switch (i) { ase 1: first.view.backgroundColor = [UIColor blueColor]; break; case 2: first.view.backgroundColor = [UIColor redColor]; break; case 3: first.view.backgroundColor = [UIColor greenColor]; break; case 4: first.view.backgroundColor = [UIColor grayColor]; break; default: break; } }
在我的代码中,我在我的ScrollView中添加了4个不同颜色的视图,现在我想在滚动我的ScrollView上检测dx& dy(dx:Axis.x上的行驶距离& dy:Axis.y上的行驶距离)并检查这两个变量以及何时:
Notic:我希望当任何人触摸ScrollView并在Axis上移动触摸(x或y)或触摸Both Axis(x和y)时检查:
if(dx> dy)禁用水平滚动并在垂直方向移动!!!
否则在水平方向移动并禁用垂直滚动!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGRect visibleRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.contentOffset.x + scrollView.bounds.size.width, scrollView.contentOffset.y + scrollView.bounds.size.height); NSLog(@"%f,%f",visibleRect.origin.x,visibleRect.origin.y); /*NSLog(@"x : %f",scrollView.contentOffset.x); NSLog(@"y : %f",scrollView.contentOffset.y);*/ if (fabsf(scrollView.contentOffset.x) > fabsf(scrollView.contentOffset.y)) { NSLog(@"Vertical Side"); } else { NSLog(@"Horizontal Side"); } }
请指导我们.我不能禁用一方并移动另一侧!谢谢
您可以实现希望向代理添加一些代码的结果.这是我的ViewController.m文件. -viewDidLoad,#import语句和其他方法被省略.@interface ViewController () { CGPoint initialOffset; NSInteger direction; // 0 undefined, 1 horizontal, 2 vertical } @end @implementation ViewController - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // retrieve current offset CGPoint currentOffset = scrollView.contentOffset; // do we know which is the predominant direction? if (direction == 0) { // no. CGFloat dx = currentOffset.x - initialOffset.x; CGFloat dy = currentOffset.y - initialOffset.y; // we need to decide in which direction we are moving if (fabs(dx) >= fabs(dy)) { direction = 1; // horizontal } else if (fabs(dy) > fabs(dx)) { direction = 2; } } // ok now we have the direction. update the offset if necessary if (direction == 1 && currentOffset.y != initialOffset.y) { // remove y offset currentOffset.y = initialOffset.y; // update [scrollView setContentOffset:currentOffset]; } else if (direction == 2 && currentOffset.x != initialOffset.x) { currentOffset.x = initialOffset.x; [scrollView setContentOffset:currentOffset]; } } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // store the current offset initialOffset = scrollView.contentOffset; // reset flag direction = 0; // AKA undefined } @end
当您开始拖动时,委托会将标志方向重置为“未知”状态,并存储当前内容偏移量.每次拖动移动后,都会调用-scrollViewDidScroll :.在那里,您决定哪个是主要方向(如果尚未完成)并通过删除x(或y)偏移来相应地校正当前滚动偏移.
我用你提供的相同设置测试了这个,只是我在UIScrollView中使用了UIImageView,我通过InterfaceBuilder设置了一切,但它应该可以正常工作.从理论上讲,使用此方法可以替换directionLock,但请记住-scrollViewDidScroll在操作期间多次调用,并且每次重写内容偏移时(如果滚动在两个方向上进行).因此,如果启用了directionLock,则会保存对委托执行的setContentOffset的许多调用.