iPhone
iPhone

Reputation: 4170

Single UIView in UIScrollview on each page

I want to create 10 UIViews with UIButtons and UILabels in a scrollview with paging enabled.

I want to reuse view on each page of scrollview. I don't want to create 10 views using loop.

How can I do it?

And another question. I want to give some animation effect on previous and next view while scrolling. so how can I identify the view?

Upvotes: 0

Views: 293

Answers (4)

sztembi
sztembi

Reputation: 223

You can always use DMLazyScrollView library.

I'm using it frequently, and it's just work as you want :)

https://github.com/malcommac/DMLazyScrollView

Upvotes: 0

blackrain
blackrain

Reputation: 56

Definitely use UICollectionView. Pros?

  1. Easier to add/remove additional pages. What if you'd like to change order, etc.? ;)
  2. Reusable elements (memory efficient).
  3. Easy to design in IB.

EDIT: UICollectionView uses UIScrollViewDelegate as well.

Upvotes: 1

Nikolay Mamaev
Nikolay Mamaev

Reputation: 1474

I'd use 3 UIViews - one for currently displaying view, one for previous and one for next view. While scrolling, I'd update frames of these views (or constraints), for example:

  1. Initial state:
    • 'Previous view' frame: (-100, 0, 100, 100)
    • 'Current view' frame: (0, 0, 100, 100)
    • 'Next view' frame: (100, 0, 100, 100)
  2. After scrolling for one page:
    • 'Previous view' frame: (0, 0, 100, 100)
    • 'Current view' frame: (100, 0, 100, 100)
    • 'Next view' frame: (200, 0, 100, 100)

These views are only for displaying purpose, so there is no need to identify the views themselves; I only need to have a current page index and also I need to update each view after page scrolling with content data basing on a data model, e.g. array elements with the following indexes: (currentPageIndex - 1), currentPageIndex, (currentPageIndex + 1).

Upvotes: 0

Avi Tsadok
Avi Tsadok

Reputation: 1843

You can use UICollectionView in order to do that. You don't need to create your own flow layout - just use UICollectionViewFlowLayout, and change the item size and scroll direction.

Upvotes: 1

Related Questions