Giter VIP home page Giter VIP logo

afkpageflipper's Introduction

AFKPageFlipper

AFKPageFlipper is a UIView subclass that can be used to display multiple views using a 3-D page flipping mechanism similar to flipping pages in a book. The class supports both direct (by setting a property) and touch-based view transitions in both directions. The touch-based interface supports tap-to-flip on either side of the screen, and pan-to-flip in either direction from anywhere on the screen. The pan functionality is inertial, which means that the user can also swipe to flip between pages.

Each subview that needs to be shown by the subview can contain any arbitrary content (including animated subviews, UI controls, and so on, although those are frozen while switching between pages for performance reasons). Subviews are requested using a just-in-time algorithm to conserve resources (especially memory). In addition, AFKPageFlipper supports less-than-fullscreen rendering—in fact, you can have multiple instances displayed on the same screen (although, to be honest, I can't imagine why you'd need them).

AFKPageFlipper has no external dependencies, with the exception of Quartz Core. The flipping functionality can be added to your project by simply importing two files: AFKPageFlipper.h and AFKPageFlipper.m.

You can see AFKPageFlipper in action here. Please note that the poor frame rate is due to the recording software, and not to the performance of the class. On either the actual simulator or a physical iPad or iPhone, the class is capable of performing full-screen transitions in both landscape and portrait at 60fps without any problems that I've been able to detect.

Usage

Using AFKPageFlipper is extremely simple. Generally speaking, you will need to perform these steps:

  • Include AFKPageFlipper.h and AFKPageFlipper.m in your project. These are the only classes you will need.
  • Import the Quartz Core framework into your project
  • Create an instance of AFKPageFlipper (either programmatically or through Interface Builder) and add it to your window (or to an existing view)
  • Provide the instance with a data source
  • Optionally, switch programmatically to a specific page

That's it! In most cases, AFKPageFlipper will only require ten lines of actual code or less in order to be added to your project. It's actually easier to use than a UIScrollView, because all the memory management is already handled for you.

Providing a data source

AFKPageFlipper needs a data source from which it can (a) determine how many subviews needs visualizing and (b) fetch the individual subviews as it needs them. It's the developer's job to provide an object that can perform these operations.

The data source (which can be set by changing the dataSource property of an AFKPageFlipper instance) must implement the AFKPageFlipperDataSource formal protocol, which requires it to implement two methods:

  • -numberOfPagesForPageFlipper: returns the number of pages to be displayed. Note that zero is not a valid page count (you should hide the view instead).
  • -viewForPage:inFlipper: returns the particular view to be displayed in the flipper for a given page number. Note that page numbers start at 1 and not 0, and that it's up to you set the frame of the view properly (the flipper passes a pointer to itself to the method call so that you can determine its bounds and use them in your calculations).

Changing pages programmatically

There are two ways to change the current page programmatically. The first is to set the currentPage property of an AFKPageFlipper instance; this results in a cross-fade from the current page (if any) to the new page. Please note that the class only makes basic data integrity checks: passing a page number that's higher than the maximum number of pages or less than one may result in undefined behaviour.

If you prefer to use the flip transition when changing pages programmatically, you can use the -setCurrentPage:animated: method instead. Passing YES as the value of animated will result in the page change to occur through a flip transition. Passing NO will result in the new view simply appearing in place without any transition.

A note on orientation

AFKPageFlipper is orientation aware—but only in the sense that, upon sensing a change in its frame property, it will re-request the number of pages to be displayed from the data source. This is to give you an opportunity to refresh your pages in the event that a different number of views can be displayed in either orientation (for example, a simple book reader like the one implemented as a test for this project could display two pages at a time in landscape, but only one in portrait, which would mean that there are roughly twice as many pages in the latter orientation than in the former).

The sample project

AFKPageFlipper comes as part of a sample project that implements a simplistic PDF viewer. Most of the code is there as scaffolding to make the PDF rendering possible; if you want to see how AFKPageFlipper is used in practice, you can take a look at MainController.m, where you'll find both the data source implementation and the view-loading mechanism (all of eight lines of code!).

afkpageflipper's People

Contributors

mtabini avatar slaunchaman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

afkpageflipper's Issues

Flip is slow on iPad 3.

This is more of a comment then an actual bug but because of the retina display, "[self.layer renderInContext:UIGraphicsGetCurrentContext()];" is very slow causing a bad user experience.
We are wondering if there would be a way around it besides pre-loading/caching.
Thanks in advance for your feedback.

Pinch Zoom Issue

Hi Marco,
Thanks a lot for the great resource on PDF.
I have been working on the same. Also trying to add functionality in the code with the Pinch and Zoom.
In fact, I have added the UIGesturerecognizer for the ZOOM/PAN and MOVE. The zooming works fine but it turns out in blur quality of PDF, which makes pdf page unreadable.

It will be very greatful if you help to resolve this issue.

Thanks and Regards.

Swift problem

Hi! I have imported this library in my swift project. There is no errors but view does not show anything. Gestures are detected. When i swipe, viewForPage() method is called, but there is no change on the screen.
Can anybody help me?

Disable for next or previous page

It could be very interesting if we could disable the flip to the next/previous page on demand.
Exemple : you want your user to do some action in the active page before being able to flip to the next (but he can go to the previous if he want). And the opposite.

iOS 7 flip animation issue.

I have implemented AFKPageflipper in my app,when tested on iOS 7 i couldn't able to see the page flip animation.Pages are just jumping instead of flipping.But it's working great on lower versions.Please provide me suggestion on this.

Image array list

hi, this effect it's fantastic, i have a problem,
I would use it to scroll a list of images, but when I create a array of UIImage, do not show in this effect. you can see me an example? thank you so much! you are fantastic!

Flip incorrectly when tap the right half page

The right half page will flip DOWN a little when tap the right half page.
Because the CATransform3D is not correct?
To reproduce this bug, just tap the right half page, and look at the up-right corner carefully.
(It would be more easy to see if the up-right corner is not just totally white.)

Multi page Flip animation

Hi Marco,

This is really good flip project demo. It makes it really simple to implement the flippage animation int the app. Right now I am building an app similar to FlipBoard app.

I am stuck in multi-page animation.

Could you please help me to implement multi page animation in AFKPageFlipper. I mean when we change to page 1 to page 5 it should have a multi page flip effect similar to flipboard app? Also how to show shadow on the BackgroundAnimationLayer when the animation is in progress.

Really thanks for the AFKPageFlipper component.

Quick page flipping issues

There's a problem when you try to flip pages real quick, you end up with either no animations or split pages with different content.
I was able to reproduce this on iPad 3G (1st gen) running 4.3.2 and on the simulator 4.3 with XCode 4.0.2. It seems easier to reproduce on the simulator.
Just try to flip pages really quickly with swiping. Somewhere in the middle it'll start to behave incorrectly.

See the result: http://f.cl.ly/items/2Y3c003M2B0l1u3h2Q0t/IMG_0010.PNG

Darker look to page being flipped

Hi,

while flipping, is it possible to show a slightly darker shade to the page being flipped so that it gives a more authentic look of another page being flipped. Or pls give some pointers as to how this can be achieved

Views Sometimes Hidden Before Flipping

The Problem

When you're reusing views in the flipper, the second and subsequent times they are "flipped" to they are invisible (alpha = 0) until the flip is over.

When This Happens

The existing project doesn't see this behavior because it makes a new PDFRendererView every time a page is requested.

Why This Is Happening

Once a view is hidden (you flip away from it) it has an opacity of 0. If you flip back to it, it won't get an opacity of 1 until cleanupFlip is called.

How To Fix It

My solution was to update "imageByRenderingView" to set the alpha to 1 before it grabs a UIImage, and then to restore the view to whatever alpha it previously had (see the below code snippet). Another approach would be to set the self.newView.alpha to 1 before creating the screenshots of each view.

- (UIImage *) imageByRenderingView {
    CGFloat oldAlpha = self.alpha;
    self.alpha = 1;
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.alpha = oldAlpha;
    return resultingImage;
}

Pull Request

While I know the code above fixes the issue, I wasn't sure if that was the most optimal solution. I can certainly submit a pull request if you like the above approach, or I can do whatever else would be more to your liking. Whatever helps the most.

Memory leak issue

Hi . When i profile my app i see that images on afkpageflipper can't remove from ram. And it gives memory error. Is there anybody that fixed this?

Allow for full page transitions

Currently the sample project splits the page in half when going a page flip. Is it possible to change the animation to allow for the entire page to flip revealing the contents behind? Similar to when you open a hard cover book and the entire cover flips, not just half of it.

Setting dataSource Doesn't Always Refresh First Page

The Problem

If the user hasn't moved from the first page and you change the dataSource, the new page won't be redrawn.

Why It Happens

In setDataSource, you call self.currentPage = 1, like so:

https://github.com/mtabini/AFKPageFlipper/blob/master/Classes/AFKPageFlipper.m#L309

But if currentPage is already 1, setCurrentPage will return without doing anything.

Solution

Set currentPage = 0 before setting self.currentPage = 1; That will force it to redraw the current page, which it should assuming the data source has really changed.

Pull Request

En route...

Vertical Flipping

Would be great to see this adjusted to allow for vertical page flipping!

Slightly Lag in animation

Hi all,
I am experiencing a lag in flip animation when swiping the view, flip animation is not as smooth, can anybody help me regarding the same

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.