PRSlideView 0.3.1

PRSlideView 0.3.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2015

Maintained by Elethom Hunter.



General

Slide view with gracefully written UIKit-like methods, delegate and data source protocol.

Note: Auto layout not supported due to the special behaviours of UIScrollView. Please use autoresizing mask instead or wrap it with a container view.

Features

  • Horizontal or vertical scrolling
  • Infinite scrolling
  • Page control (horizontal mode only)

Installation

Usage

Create a Slide View

PRSlideView *slideView = [[PRSlideView alloc] initWithFrame:self.view.bounds];
slideView.delegate = self;
slideView.dataSource = self;
slideView.direction = PRSlideViewDirectionHorizontal; // horizontal by default
slideView.infiniteScrollingEnabled = YES; // disabled by default
slideView.showsPageControl = YES; // enabled by default
[slideView registerClass:PRAlbumPage.class
  forPageReuseIdentifier:NSStringFromClass(PRAlbumPage.class)];
slideView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                              UIViewAutoresizingFlexibleHeight);
self.slideView = slideView;
[self.view addSubview:slideView];

Create a Slide View Page Subclass

#import "PRSlideViewPage.h"

@interface PRAlbumPage : PRSlideViewPage

@property (nonatomic, weak) UIImageView *coverImageView;

@end
#import "PRAlbumPage.h"

@implementation PRAlbumPage

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *coverImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        coverImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                           UIViewAutoresizingFlexibleHeight);
        coverImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.coverImageView = coverImageView;
        [self addSubview:coverImageView];
    }
    return self;
}

Use Data Source

#pragma mark - PRSlideViewDataSource

- (NSInteger)numberOfPagesInSlideView:(PRSlideView *)slideView
{
    return self.albumData.count;
}

- (PRSlideViewPage *)slideView:(PRSlideView *)slideView pageAtIndex:(NSInteger)index
{
    PRAlbumPage *page = [slideView dequeueReusablePageWithIdentifier:NSStringFromClass(PRAlbumPage.class)
                                                            forIndex:index];

    NSString *imageName = [self.albumData[index] stringByAppendingPathExtension:@"jpg"];
    page.coverImageView.image = [UIImage imageNamed:imageName];

    return page;
}

Use Delegate

#pragma mark - PRSlideViewDelegate

- (void)slideView:(PRSlideView *)slideView didScrollToPageAtIndex:(NSInteger)index
{
    self.titleLabel.text = self.albumData[index];
}

- (void)slideView:(PRSlideView *)slideView didClickPageAtIndex:(NSInteger)index
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You clicked an album"
                                                    message:self.albumData[index]
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

All done! You can check out the code in the demo provided.

License

This code is distributed under the terms and conditions of the MIT license.

Donate

You can support me by:

:-)

Contact