問題描述
我有一個內容大小為 1200x480 的 UIScrollView.我有一些圖像視圖,其寬度加起來為 600.當向右滾動時,我只需增加內容大小并設置偏移量以使一切順利(然后我想添加其他圖像,但這并不重要現在).所以基本上,當前在視口中的圖像保留在左側某處,最終會從超級視圖中刪除.
I have a UIScrollView whose content size is 1200x480. I have some image views on it, whose width adds up to 600. When scrolling towards the right, I simply increase the content size and set the offset so as to make everything smooth (I then want to add other images, but that's not important right now). So basically, the images currently in the viewport remain somewhere on the left, eventually to be removed from the superview.
現在,我在向左滾動時發生的問題.我所做的是將圖像移動到內容大小的末尾(因此將 600 添加到每個圖像視圖的 origin.x),然后相應地設置內容偏移量.當手指在屏幕上并且用戶拖動時它起作用(scrollView.isTracking = YES).當用戶向左滾動并松開時(scrollView.isTracking = NO),圖像視圖最終向右移動得太快并且幾乎立即消失.有誰知道我怎樣才能讓圖像很好地移動并且即使用戶沒有手動拖動視圖并且已經放手也不會消失?
Now, the problem that I have happens when scrolling towards the left. What I do is I move the images to the end of the content size (so add 600 to each image view's origin.x), and then set the content offset accordingly. It works when the finger is on the screen and the user drags (scrollView.isTracking = YES). When the user scrolls towards the left and lets go (scrollView.isTracking = NO), the image views end up moving too fast towards the right and disappear almost instantly. Does anyone know how I could have the images move nicely and not disappear even when the user's not manually dragging the view and has already let go?
這是我的水平拖動代碼:
Here's my code for dragging horizontally:
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = self.scrollView.contentOffset;
CGSize size = self.scrollView.contentSize;
CGPoint newXY = CGPointMake(size.width-600, size.height-480);
// this bit here allows scrolling towards the right
if (offset.x > size.width - 320) {
[self.scrollView setContentSize:CGSizeMake(size.width+320, size.height)];
[self.scrollView setContentOffset: offset];
}
// and this is where my problem is:
if (offset.x < 0) {
for (UIImageView *imageView in self.scrollView.subviews) {
CGRect frame = imageView.frame;
[imageView setFrame:CGRectMake
(frame.origin.x+newXY.x, frame.origin.y, 200, frame.size.height)];
}
[self.scrollView setContentOffset:CGPointMake(newXY.x+offset.x, offset.y)];
}
}
現在可以使用了 - 我查看了 StreetScroller,現在一切正常.
This is now working - I had a look at StreetScroller and it's all good now.
但是,我現在想放大滾動視圖,但從未調用 viewForZoomingInScrollView.無法放大內容尺寸較大的滾動視圖嗎?
However, I now want to zoom in on the scrollview, but viewForZoomingInScrollView is never called. Is it not possible to zoom in on a scrollview with a large content size?
推薦答案
這里有一些方法.只需使用網站搜索...
There are some approaches floating around here. Just use the site search …
如果您想要 Apple 創建的更官方"示例,請查看 StreetScroller 演示.有關此示例的更多信息,請查看去年的 WWDC 會議編號.104 高級滾動查看技術.
If you want an more "official" example created by Apple take a look at the StreetScroller Demo. For some more information about this example take a look at last years WWDC session no. 104 Advanced Scroll View Techniques.
Github 上還有一個 UIScrollView 子類叫BAGPagingScrollView,就是分頁&無限,但它有一些錯誤你必須自己修復,因為它沒有處于積極開發中(尤其是 goToPage:
方法會導致問題).
There is also an UIScrollView subclass on Github called BAGPagingScrollView, which is paging & infinite, but it has a few bugs you have to fix on your own, because it's not under active development (especially the goToPage:
method leads to problems).
這篇關于無限水平滾動 UIScrollView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!