問(wèn)題描述
我正在努力編寫(xiě)應(yīng)用程序的一部分,該應(yīng)用程序的行為應(yīng)該類似于原生 iphone 照片應(yīng)用程序.查看了 Orielly 的 iphone sdk 應(yīng)用程序開(kāi)發(fā)書(shū),其中提供了實(shí)現(xiàn)這種所謂的頁(yè)面滑動(dòng)的示例代碼.那里的代碼首先創(chuàng)建了所有子視圖,然后隱藏/取消隱藏它們.在給定時(shí)間,只有 3 個(gè)子視圖可見(jiàn),其余的被隱藏.經(jīng)過(guò)一番努力,我讓它與當(dāng)時(shí)只有大約 15 頁(yè)的應(yīng)用程序一起工作.
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.
當(dāng)我添加 300 頁(yè)時(shí),很明顯這種預(yù)先分配這么多子視圖的方法存在性能/內(nèi)存問(wèn)題.然后我想可能就我的情況而言,我應(yīng)該只分配 3 個(gè)子視圖,而不是隱藏/取消隱藏它們.可能我應(yīng)該在運(yùn)行時(shí)刪除/添加子視圖.但是不知道 UIScrollView 是否可以動(dòng)態(tài)更新內(nèi)容.例如,如 UIScrollView 所理解的,一開(kāi)始有 3 個(gè)幀位于屏幕的不同 x 偏移量(0、320、640).一旦用戶移動(dòng)到第 3 頁(yè),我如何確保我能夠添加第 4 頁(yè)并刪除第 1 頁(yè),但 UIScrollView 不會(huì)混淆?
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 ?
希望有針對(duì)此類問(wèn)題的標(biāo)準(zhǔn)解決方案...有人可以指導(dǎo)嗎?
Hoping there is a standard solution to this kind of problem...can someone guide ?
推薦答案
UIScrollView 只是 UIView 的一個(gè)子類,因此可以在運(yùn)行時(shí)添加和刪除子視圖.假設(shè)您有固定寬度的照片(320 像素)并且有 300 張,那么您的主視圖將是 300 * 320
像素寬.創(chuàng)建滾動(dòng)視圖時(shí),將框架初始化為那么寬.
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.
因此,滾動(dòng)視圖的框架將具有 (0, 0) 到 (96000, 480) 的尺寸.每當(dāng)您添加子視圖時(shí),您都必須更改它的框架,使其適合其父視圖中的正確位置.
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.
假設(shè)我們將第四張照片添加到滾動(dòng)視圖中.它的幀將從 (960, 480) 到 (1280, 480).如果您可以以某種方式將索引與每張圖片相關(guān)聯(lián),那么這很容易計(jì)算.然后用它來(lái)計(jì)算索引從0開(kāi)始的圖片幀:
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)
刪除第一張圖片/子視圖應(yīng)該很容易.保留當(dāng)前屏幕上的 3 個(gè)子視圖的數(shù)組.每當(dāng)你在屏幕上添加一個(gè)新的子視圖時(shí),也要將它添加到這個(gè)數(shù)組的末尾,然后從屏幕上刪除這個(gè)數(shù)組中的第一個(gè)子視圖.
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.
這篇關(guān)于如何實(shí)現(xiàn)具有 1000 多個(gè)子視圖的 UIScrollView?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!