問題描述
我正在努力編寫應用程序的一部分,該應用程序的行為應該類似于原生 iphone 照片應用程序.查看了 Orielly 的 iphone sdk 應用程序開發書,其中提供了實現這種所謂的頁面滑動的示例代碼.那里的代碼首先創建了所有子視圖,然后隱藏/取消隱藏它們.在給定時間,只有 3 個子視圖可見,其余的被隱藏.經過一番努力,我讓它與當時只有大約 15 頁的應用程序一起工作.
I am struggling with writing portion of an app which should behave like the native iphone photo app. Looked at iphone sdk app development book from Orielly which gave an example code for implementing this so-called page-flicking. The code there first created all subviews and then hide/unhide them. At a given time only 3 subviews are visible rest are hidden. After much effort I got it working with app which at that time had only around 15 pages.
當我添加 300 頁時,很明顯這種預先分配這么多子視圖的方法存在性能/內存問題.然后我想可能就我的情況而言,我應該只分配 3 個子視圖,而不是隱藏/取消隱藏它們.可能我應該在運行時刪除/添加子視圖.但是不知道 UIScrollView 是否可以動態更新內容.例如,如 UIScrollView 所理解的,一開始有 3 個幀位于屏幕的不同 x 偏移量(0、320、640).一旦用戶移動到第 3 頁,我如何確保我能夠添加第 4 頁并刪除第 1 頁,但 UIScrollView 不會混淆?
As soon as I added 300 pages, it became clear that there are performance/memory issues with that approach of pre-allocating so many subviews. Then I thought may be for my case I should just allocate 3 subviews and instead of hide/unhide them. May be I should just remove/add subviews at runtime. But can't figure out whether UIScrollView can dynamically update contents. For example, at the start there are 3 frames at different x-offsets ( 0, 320, 640 ) from the screen as understood by UIScrollView. Once user moves to 3rd page how do I make sure I am able to add 4th page and remove 1st page and yet UIScrollView doesn't get confused ?
希望有針對此類問題的標準解決方案...有人可以指導嗎?
Hoping there is a standard solution to this kind of problem...can someone guide ?
推薦答案
UIScrollView 只是 UIView 的一個子類,因此可以在運行時添加和刪除子視圖.假設您有固定寬度的照片(320 像素)并且有 300 張,那么您的主視圖將是 300 * 320
像素寬.創建滾動視圖時,將框架初始化為那么寬.
UIScrollView is just a subclass of UIView so it's possible to add and remove subviews at runtime. Assuming you have fixed width photos (320px) and there are 300 of them, then your main view would be 300 * 320
pixels wide. When creating the scroll view, initialize the frame to be that wide.
因此,滾動視圖的框架將具有 (0, 0) 到 (96000, 480) 的尺寸.每當您添加子視圖時,您都必須更改它的框架,使其適合其父視圖中的正確位置.
So the scroll view's frame would have the dimensions (0, 0) to (96000, 480). Whenever you are adding a subview, you will have to change it's frame so it fits in the correct position in its parent view.
假設我們將第四張照片添加到滾動視圖中.它的幀將從 (960, 480) 到 (1280, 480).如果您可以以某種方式將索引與每張圖片相關聯,那么這很容易計算.然后用它來計算索引從0開始的圖片幀:
So let's say, we are adding the 4th photo to the scroll view. It's frame would be from (960, 480) to (1280, 480). That is easily to calculate, if you can somehow associate an index with each picture. Then use this to calculate the picture's frame where indexes start at 0:
Top-Left -- (320 * (index - 1), 0)
到
Bottom-Right -- (320 * index, 480)
刪除第一張圖片/子視圖應該很容易.保留當前屏幕上的 3 個子視圖的數組.每當你在屏幕上添加一個新的子視圖時,也要將它添加到這個數組的末尾,然后從屏幕上刪除這個數組中的第一個子視圖.
Removing the first picture/subview should be easy. Keep an array of the 3 subviews currently on-screen. Whenever you are adding a new subview to the screen, also add it to the end of this array, and then remove the first subview in this array from the screen too.
這篇關于如何實現具有 1000 多個子視圖的 UIScrollView?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!