当人们翻转图像视图时,如何向左和向右移动,看起来它实际上是左右移动

时间:2012-02-10 04:04:31

标签: uiview uiimageview

风格应该像iBooks或foursquare中的图片。当我们向左翻转时,我们看到下一张图片被拉了。怎么办?有代码可以吗?我认为必须有一个现成的代码

3 个答案:

答案 0 :(得分:3)

听起来你想要UIPageViewController

答案 1 :(得分:2)

答案 2 :(得分:2)

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

创建Single View Application类型的新项目,将其命名为BookEffect,取消选中Use Storyboards,取消选中Include Unit Tests,但请务必选中Use Use Reference Counting。

然后在项目中添加几张图片,例如JPEG格式。

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

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

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

#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中添加:

@synthesize imageView, titleLabel;

并添加这些方法定义:

- (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,看起来像这样:

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

在BookEffectViewController.m中修改viewDidLoad,如下所示:

- (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中定义另外两个方法:

- (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模拟器测试它 这是一个基本的外壳,其他花里胡哨可以为你自己的乐趣添加,即取出页码,添加标题等。

希望这会有所帮助。