本文介绍了当人们翻转 UIImageView 时如何左右移动,使它看起来真的是左右移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样式应该像 iBooks 或 Foursquare 中的图片.当我们向左翻转时,我们看到下一张图片被拉出.怎么做?有这样做的代码吗?我认为一定有一个现成的代码

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

推荐答案

试试这个.一种解决方案.根据需要进行修改.

Try this. One solution. Revise as needed.

创建一个 Single View Application 类型的新项目,将其命名为 BookEffect,取消选中 Use Storyboards,取消选中 Include Unit Tests,但一定要选中 Use Automatic Reference Counting 选项.

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.

然后在项目中添加一些图片,例如 JPEG 格式.

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

创建一个新文件 - 使用带有 UIViewController 子类的 Objective-C 类模板,选择选项 With XIB for user interface,并将其命名为 SinglePageViewController.将文件保存在 BookEffect 文件夹中.

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.

添加后,选择 SinglePageViewController.xib 并将 UILabel 控件和 UIImageView 控件添加到视图中,并与文件所有者建立适当的连接.

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.

将 SinglePageViewController.h 修改为如下所示:

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

在 SinglePageViewController.m 中添加:

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];
}

现在转到 BookEffecViewController.h,让它看起来像这样:

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

在 BookEffectViewController.m 中修改 viewDidLoad 看起来像这样:

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;
}

在 BookEffectViewController.m 中再定义两个方法:

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;
    }
}

就是这样.Command-R 使用 iPad 模拟器对其进行测试这是一个基本的外壳,您可以根据自己的喜好添加其他花里胡哨,即取出页码,添加标题等.

That's it. Command-R to test it with the iPad SimulatorThis 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.

希望这会有所帮助.

这篇关于当人们翻转 UIImageView 时如何左右移动,使它看起来真的是左右移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:51