久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

滾動時如何在 UIScrollView 中移動一個方向

how to move one direction in UIScrollView when scrolling(滾動時如何在 UIScrollView 中移動一個方向)
本文介紹了滾動時如何在 UIScrollView 中移動一個方向的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 Objective-c 的新手.我創建 UIScrollView 對象并使用以下代碼添加到我的視圖中:

I'm new in objective-c. I create UIScrollView object and add in my view with this code:

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 上的行駛距離)并檢查這兩個變量以及何時:

in my code I add 4 view in my ScrollView with different color now I want when scrolling on my ScrollView detect dx & dy (dx: driving distance on Axis.x & dy:driving distance on Axis.y) and check these two variable and when :

注意:當任何一次觸摸 ScrollView 并移動觸摸軸(x 或 y)或觸摸兩個軸(x 和 y)時,我想檢查一下:

Notic: I want when any one touch on ScrollView and moving touch on Axis (x or y) or touch on Both Axis (x and y) check this :

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

請指導我.我不能禁用一側并移動另一側!!!謝謝

please guide me guys. I can't disable one side and move another side!!! thanks

推薦答案

你可以實現你想要的結果添加一些代碼到你的委托.這是我的 ViewController.m 文件.-viewDidLoad#import語句等方法省略.

You can achieve the result you want adding some code to your delegate. This is my ViewController.m file. -viewDidLoad, #import statements and the other methods are omitted.

@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)偏移量來相應地更正當前滾動偏移量.

When you start dragging, the delegate will reset the flag direction to "unknown" state, and store the current content offset. After every dragging move, your -scrollViewDidScroll: will be called. There, you decide which is the predominant direction (if this hasn't been done yet) and correct the current scrolling offset accordingly, by removing the x (or y) offset.

我使用您提供的相同設置對此進行了測試,只是我在 UIScrollView 中使用了一個 UIImageView 并通過 InterfaceBuilder 設置了所有內容,但它應該可以正常工作.理論上,用這個方法你可以替換directionLock,但是記住-scrollViewDidScroll在一個動作中被調用了很多次,并且每次它都會重寫內容偏移量(如果滾動是雙向發生).因此,如果您讓 directionLock 處于啟用狀態,您可以保存委托執行的許多對 setContentOffset: 的調用.

I tested this with the same settings you provided, only I used a UIImageView inside UIScrollView and I set up everything via InterfaceBuilder, but it should work fine. Theoretically, with this method you could replace directionLock, but remember that -scrollViewDidScroll is called many times during an action, and every time it rewrites the content offset (if the scrolling is happening in both directions). So if you leave directionLock enabled, you save many of the calls to setContentOffset: that the delegate performs.

這篇關于滾動時如何在 UIScrollView 中移動一個方向的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
主站蜘蛛池模板: 在线观看视频91 | www.夜夜骑 | 国产精品揄拍一区二区久久国内亚洲精 | 日韩欧美成人精品 | 国产亚洲一区二区三区在线观看 | 一级毛片在线视频 | 一级看片免费视频囗交动图 | 欧美一区二区久久 | 成人影院午夜 | 一级a性色生活片久久毛片 午夜精品在线观看 | 日韩精品在线一区 | 亚洲综合色视频在线观看 | 国产成人免费视频网站高清观看视频 | 欧美激情亚洲 | 区一区二区三在线观看 | 高清欧美性猛交xxxx黑人猛交 | 中文字幕高清 | 91精品国产一区二区三区蜜臀 | 91爱爱·com| 91视频在线观看 | 在线一级片 | 欧美日韩一区二区三区不卡视频 | 国产欧美日韩久久久 | 天天天堂| 中文字幕亚洲无线 | 中文字幕在线精品 | 亚洲人成人一区二区在线观看 | 日韩免费视频一区二区 | 久久人人爽人人爽人人片av免费 | 精品欧美一区二区三区久久久 | 欧美精品一区在线发布 | 日本亚洲欧美 | 国产视频久久久 | 国产精品久久久久久福利一牛影视 | 午夜一区| 成人中文字幕av | 久久久久国产一区二区三区 | 久久一区二区精品 | 午夜日韩精品 | 国产中文字幕网 | 久久久精品日本 |