問題描述
當有人用擦除手勢從左到右滾動內容時,我希望背景圖像以不同的速度滾動到相同的方向.就像這些經典游戲在 20 年前所做的一樣(記住這一點,有人嗎????)
When somebody does a wipe gesture to scroll the content from left to right, I would like to have a background image scrolling into the same direction, but at a different speed. Much like what these classic games did do 20 years ago (remember that, anybody????)
推薦答案
我通過使用 兩個 UIScrollView
實例完成了這個.第一個是顯示實際內容的位置,第二個(在 z 順序中位于第一個之后)是我移動較慢的背景的位置.從那里開始,頂部的 UIScrollView
附加了一個委托,當 contentOffset
更改時會收到通知.反過來,該委托以編程方式設置背景滾動條的 contentOffset
,乘以一個常數以減慢相對于前景的滾動速度.因此,例如,您可能有以下內容:
I accomplished this by using two UIScrollView
instances. The first is where the actual content is displayed, and the second (which is behind the first in z-order) is where I have my slower-moving background. From there the top UIScrollView
has a delegate attached to it that gets notified when the contentOffset
changes. That delegate, in turn, programatically sets the contentOffset
of the background scroller, multiplied against a constant to slow the scroll down relative to the foreground. So, for instance, you might have something like:
// Defined as part of the delegate for the foreground UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UIScrollView* scroll_view(static_cast<UIScrollView*>(bkg_scroller_m.view));
CGPoint offset(scrollView.contentOffset);
offset.x = offset.x / 3;
offset.y = offset.y / 3;
// Scroll the background scroll view by some smaller offset
scroll_view.contentOffset = offset;
}
這篇關于在 UIScrollView 上以不同的速度滾動背景的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!