問題描述
我是 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模板網!