2014-07-06 57 views
3

我是新的objective-c。我創建UIScrollView對象,在我看來,添加此代碼:滾動時如何在UIScrollView中移動一個方向

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; 
     } 
    } 
在我的代碼

我加4觀點在我的滾動型有不同的顏色,現在我的滾動型滾動時,我想檢測DX & DY(DX:行駛距離on Axis.x & dy:在Axis.y上的行駛距離),並檢查這兩個變量和時間:

Notic:我希望當任何人觸摸ScrollView並在Axis(x或y)上移動觸摸或觸摸Both軸(x和y)檢查此:

if(dx> dy)disable水平滾動和垂直移動! 否則在水平方向移動並禁用垂直滾動!

- (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"); 
    } 
} 

請指導我的傢伙。我不能禁用一邊和移動另一邊!謝謝

回答

1

你可以達到你想要添加一些代碼到你的委託結果。這是我的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 

當您開始拖移,委託將重置標誌direction爲「未知」的狀態,並存儲當前內容偏移。每拖動一次,你的-scrollViewDidScroll:將被調用。在那裏,你決定哪個是主要方向(如果這還沒有完成),並通過去除x(或y)偏移來相應地校正當前的滾動偏移量。

我測試了這個與你提供的相同設置,只有我使用在UIScrollView裏面,我通過InterfaceBuilder設置了所有的東西,但它應該可以正常工作。從理論上講,用這種方法可以取代directionLock,但是請記住,在動作期間多次調用-scrollViewDidScroll,並且每次重寫內容偏移(如果滾動發生在兩個方向上)。因此,如果您啓用directionLock,則會將代表執行的很多呼叫保存到setContentOffset:

+0

謝謝我的老兄,這是我的回答! – user3797431

6

如果我理解你是正確的,你想禁用滾動方向已被用戶拖動較少。如果是這樣,UIScrollView的已經提供了一個選項,可以這樣做:

scrollView.directionalLockEnabled = YES; 

當此選項設置爲true的UIScrollView會自行處理的正確方向鎖。

欲瞭解更多信息,請查看文檔:link

+0

我的朋友你是對的,但是當拖拽Axis(x&y)兩次滾動工作,我不想要它。 – user3797431