How to Move Left and Righ of when people flip UIIm

2019-03-07 02:22发布

The style should be like iBooks or pictures in foursquare. When we flip to the left we see the next picture being pulled. How to do so? Is there a code to do so? I think there must have been a ready code for that

3条回答
Evening l夕情丶
2楼-- · 2019-03-07 02:23

It sounds like you want UIPageViewController.

查看更多
做自己的国王
4楼-- · 2019-03-07 02:46

Try this. One solution. Revise as needed.

Create a new project of type Single View Application, name it BookEffect, uncheck Use Storyboards, uncheck Include Unit Tests, but be sure to check the option Use Automatic Reference Counting.

Then add to the project a few pictures, for example, in JPEG format.

Create a new file - use the Objective-C Class template with a subclass of UIViewController, select the option With XIB for user interface, and name it SinglePageViewController. Save the file inside the BookEffect folder.

Once added, select the SinglePageViewController.xib and add to the view a UILabel control and UIImageView control and make the appropriate connections to the File's Owner.

Revise the SinglePageViewController.h to look like this:

#import <UIKit/UIKit.h>
@interface SinglePageViewController : UIViewController
{
    NSUInteger pageNumber;
}
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, assign) NSUInteger pageNumber;
- (void) setPageNumber: (NSUInteger) newPageNumber;
- (NSUInteger) pageNumber;
@end

In SinglePageViewController.m add:

@synthesize imageView, titleLabel;

and add these definitions of methods:

- (void) setPageNumber: (NSUInteger) newPageNumber{
    pageNumber = newPageNumber;
}
- (NSUInteger) pageNumber{
    return pageNumber;
}
- (void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [titleLabel setText:[NSString stringWithFormat:@"Page %d", pageNumber]];
    NSArray *images = [[NSBundle mainBundle] pathsForResourcesOfType:@".jpg" inDirectory:nil];
    UIImage *img = [UIImage imageWithContentsOfFile:[images objectAtIndex:pageNumber-1]];
    [imageView setImage:img];
}

Now go to the BookEffecViewController.h, and make it look like this:

#import <UIKit/UIKit.h>
#import "SinglePageViewController.h"
@interface BookEffectViewController : UIViewController 
<UIPageViewControllerDataSource>
@property( nonatomic, strong) UIPageViewController *pageViewController;
@end

In BookEffectViewController.m revise viewDidLoad to look like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.dataSource = self;
    SinglePageViewController *singlePage = [[SinglePageViewController alloc]  initWithNibName:@"SinglePageViewController" bundle:nil];
    [singlePage setPageNumber:1];
    NSArray *controllers = [[NSArray alloc] initWithObjects:singlePage, nil];
    [self.pageViewController setViewControllers:controllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    self.pageViewController.view.frame = self.view.bounds;
}

Define two more methods in BookEffectViewController.m:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger previousPageNumber = [((SinglePageViewController*)viewController) pageNumber];
    if ( previousPageNumber == 1 ) {
        return nil;
    }
    else{
        SinglePageViewController *singlePage = [[SinglePageViewController alloc] initWithNibName:@"SinglePageViewController" bundle:nil];
        [singlePage setPageNumber:previousPageNumber-1];
        return singlePage;
    }
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger previousPageNumber = [((SinglePageViewController*)viewController) pageNumber];
    if ( previousPageNumber >= [[[NSBundle mainBundle] pathsForResourcesOfType:@".jpg" inDirectory:nil] count] ) {
        return nil;
     }
    else{
        SinglePageViewController *singlePage = [[SinglePageViewController alloc] initWithNibName:@"SinglePageViewController" bundle:nil];
         [singlePage setPageNumber:previousPageNumber+1];
        return singlePage;
    }
}

That's it. Command-R to test it with the iPad Simulator This is a basic shell and other bells and whistles can be added for your own pleasure, i.e. take out Page Number, add a caption, etc.

Hope this helped.

查看更多
登录 后发表回答