Just My Life & My Work

[iOS] Scroll View 改變翻頁大小

在Scroll View上翻頁(Paging)似乎只能根據Scroll View這個元件的大小來決定,然而今日我有需求來改變其翻頁的大小,該如何做呢?

scroll view_


  1. turn off clipsToBounds
  2. put a UIView over the scrollview, wherever you want the user to be able to touch
  3. move the panGestureRecognizer to that view
    [touchableView addGestureRecognizer:_scrollView.panGestureRecognizer];

如果只是縮小Scroll View,且讓其上的元件不被切掉(第一點),那麼觸控範圍只有在被縮小Scroll View上頭,這樣不太合理,那麼我們就希望讓兩邊空白處也能觸控Scroll View。神人建議可以將panGestureRecognizer轉移共用,在Scroll View底下鋪View,將Scroll View的panGestureRecognizer設定到View上頭,這樣就大功告成!

scroll view


神人說這麼做,Apple是允許的,在WWDC的Session (WWDC Session 223, “Enhancing User Experience with Scroll Views)上有提到,Scroll View有兩個gesture recognizer,就是因為大家的期望而設計出來的,如此就能在非該Scroll View上的元件來移動縮放該Scroll View囉~

// Use these accessors to configure the scroll view’s built-in gesture recognizers.

// Do not change the gestures’ delegates or override the getters for these properties.

@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);

// `pinchGestureRecognizer` will return nil when zooming is disabled.

@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);

參考:Changing the size of a paging scroll view



WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


%d 位部落客按了讚: