iphone:UIScrollView分页已启用,无缩放和预览

iphone:UIScrollView分页已启用,无缩放和预览,iphone,objective-c,uiscrollview,paging,programmatically-created,Iphone,Objective C,Uiscrollview,Paging,Programmatically Created,我想实现一个UIScrollView,其中启用了分页功能,我可以轻松浏览一些图像。这就是我现在想要做的 到目前为止,我已经在interface builder中完成了这项工作:有人能帮忙吗 剩下的我不知道怎么做。谁能帮我一下吗。我不需要任何缩放功能。我不想在scrollview中预览上一个或下一个图像,我只想要一个简单的支持分页的滚动视图,允许用户浏览图像 感谢所有的帮助。 如果你能一步一步地告诉我如何才能做到这一点,我将不胜感激。多谢各位 我看过一些代码示例,它们太复杂了。我看了几个,从一开

我想实现一个UIScrollView,其中启用了分页功能,我可以轻松浏览一些图像。这就是我现在想要做的

到目前为止,我已经在interface builder中完成了这项工作:有人能帮忙吗

剩下的我不知道怎么做。谁能帮我一下吗。我不需要任何缩放功能。我不想在scrollview中预览上一个或下一个图像,我只想要一个简单的支持分页的滚动视图,允许用户浏览图像

感谢所有的帮助。 如果你能一步一步地告诉我如何才能做到这一点,我将不胜感激。多谢各位


我看过一些代码示例,它们太复杂了。我看了几个,从一开始就喜欢教程。谢谢

听起来您只需要将内容添加为UIScrollView的子视图并添加手势识别器

将图像加载到UIImageView中。将UIImageView添加为UIScrollView的子视图

// do this in init or loadView or viewDidLoad, wherever is most appropriate
// imageView is a retained property
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"];
[scrollView addSubview:imageView];
// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];
向UIScrollView添加UISweepGestureRecognitor

// do this in init or loadView or viewDidLoad, wherever is most appropriate
// imageView is a retained property
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"];
[scrollView addSubview:imageView];
// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];
在UIWipegestureRecognitor处理程序上,在UIImageView中更改加载的图像

- (void)handleSwipe:(UIGestureRecognizer *)swipe {
  // do what you need to determine the next image
  imageView.image = [UIImage imageNamed:<your replacement image here>];
}
-(无效)无把手擦拭:(UIgestureRecognitor*)刷卡{
//执行确定下一个图像所需的操作
imageView.image=[UIImage ImageName:];
}

听起来您只需要将内容添加为UIScrollView的子视图,并添加手势识别器

将图像加载到UIImageView中。将UIImageView添加为UIScrollView的子视图

// do this in init or loadView or viewDidLoad, wherever is most appropriate
// imageView is a retained property
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"];
[scrollView addSubview:imageView];
// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];
向UIScrollView添加UISweepGestureRecognitor

// do this in init or loadView or viewDidLoad, wherever is most appropriate
// imageView is a retained property
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"];
[scrollView addSubview:imageView];
// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];
在UIWipegestureRecognitor处理程序上,在UIImageView中更改加载的图像

- (void)handleSwipe:(UIGestureRecognizer *)swipe {
  // do what you need to determine the next image
  imageView.image = [UIImage imageNamed:<your replacement image here>];
}
-(无效)无把手擦拭:(UIgestureRecognitor*)刷卡{
//执行确定下一个图像所需的操作
imageView.image=[UIImage ImageName:];
}

也许您想看看我的viewcontroller,它可以做到这一点。我写这篇文章是为了回答这个问题。
也许这对你来说太复杂了,但不会变得更容易。
这只是一个基本版本,它在开始时将所有图像加载到内存中。这在实际应用程序中不起作用。因此,您必须实现一些UIScrollView委托函数。复杂性就从这里开始

//  ImageViewController.h
//
//  Created by Matthias Bauch on 12.10.10.
//  Copyright 2010 Matthias Bauch. All rights reserved.
//

#import <UIKit/UIKit.h>

#warning this is just a quick hack, you should not use this if you dont understand this. There might be leaks, bugs and a lot of whatever.

@interface ImageViewController : UIViewController {
    NSString *imagePath;
}
@property (nonatomic, copy) NSString *imagePath;
- (id)initWithImageDirectory:(NSString*)imgPath;
@end


//
//  ImageViewController.m
//
//  Created by Matthias Bauch on 12.10.10.
//  Copyright 2010 Matthias Bauch. All rights reserved.
//

#import "ImageViewController.h"


@implementation ImageViewController
@synthesize imagePath;

- (id)initWithImageDirectory:(NSString*)imgPath {
    if (self = [super init]) {
        imagePath = [imgPath copy];
    }
    return self;
}


- (UIView *)viewFullOfImagesAtPath:(NSString *)path withSize:(CGSize)size {
    NSError *error = nil;
    NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    if (!filenames) {
        NSLog(@"Error accessing files: %@ [%@]", [error localizedDescription], error);
        return nil;
    }
    UIView *aView = [[UIView alloc] init];
    CGFloat xOffset = 0;
    for (NSString *filename in filenames) {
        NSString *fullPath = [path stringByAppendingPathComponent:filename];
        UIImage *image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease];
        if (!image)
            continue;
        CGRect frameRect = CGRectMake(xOffset, 0, size.width, size.height);
        UIImageView *imageView = [[[UIImageView alloc] initWithFrame:frameRect] autorelease];
        [imageView setImage:image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [aView addSubview:imageView];
        xOffset += size.width;
    }
    aView.frame = CGRectMake(0, 0, xOffset, size.height);
    return [aView autorelease];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
    scrollView.pagingEnabled = YES;
    UIView *contentView = [self viewFullOfImagesAtPath:imagePath withSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)];
    NSLog(@"%f %f %f %f", contentView.frame.origin.x, contentView.frame.origin.y, contentView.frame.size.width, contentView.frame.size.height);
    [scrollView addSubview:contentView];
    scrollView.contentSize = CGSizeMake(CGRectGetWidth(contentView.frame), CGRectGetHeight(contentView.frame));
    [self.view addSubview:scrollView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [imagePath release];
    [super dealloc];
}


@end
//ImageViewController.h
//
//由Matthias Bauch于2010年10月12日创作。
//版权所有2010马蒂亚斯·鲍奇。版权所有。
//
#进口
#警告:这只是一个快速破解,如果你不理解,就不应该使用它。可能会有漏洞、bug和其他很多东西。
@接口ImageViewController:UIViewController{
NSString*imagePath;
}
@属性(非原子,复制)NSString*imagePath;
-(id)initWithImageDirectory:(NSString*)imgPath;
@结束
//
//ImageViewController.m
//
//由Matthias Bauch于2010年10月12日创作。
//版权所有2010马蒂亚斯·鲍奇。版权所有。
//
#导入“ImageViewController.h”
@ImageViewController的实现
@综合成像路径;
-(id)initWithImageDirectory:(NSString*)imgPath{
if(self=[super init]){
imagePath=[imgPath copy];
}
回归自我;
}
-(UIView*)viewFullOfImagesAtPath:(NSString*)路径大小:(CGSize)大小{
n错误*错误=nil;
NSArray*文件名=[[NSFileManager defaultManager]目录目录路径:路径错误:&错误];
如果(!文件名){
NSLog(@“访问文件时出错:%@[%@]”,[Error localizedDescription],错误);
返回零;
}
UIView*aView=[[UIView alloc]init];
CGFloat xOffset=0;
for(文件名中的NSString*文件名){
NSString*fullPath=[path stringByAppendingPathComponent:filename];
UIImage*image=[[UIImage alloc]initWithContentsOfFile:fullPath]autorelease];
如果(!图像)
继续;
CGRect frameRect=CGRectMake(xOffset,0,size.width,size.height);
UIImageView*imageView=[[UIImageView alloc]initWithFrame:frameRect]自动释放];
[图像视图设置图像:图像];
imageView.contentMode=UIViewContentModeScaleAspectFit;
[查看添加子视图:图像视图];
xOffset+=大小.宽度;
}
aView.frame=CGRectMake(0,0,xOffset,size.height);
返回[查看自动释放];
}
-(无效)viewDidLoad{
[超级视图下载];
UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds]autorelease];
scrollView.PaginEnabled=是;
UIView*contentView=[self-viewFullOfImagesAtPath:imagePath with-size:CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height)];
NSLog(@%f%f%f),contentView.frame.origin.x,contentView.frame.origin.y,contentView.frame.size.width,contentView.frame.size.height);
[滚动视图添加子视图:内容视图];
scrollView.contentSize=CGSizeMake(CGRectGetWidth(contentView.frame),CGRectGetHeight(contentView.frame));
[self.view addSubview:scrollView];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//覆盖以允许任何方向。
返回YES;
}
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
-(无效)视图卸载{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(无效)解除锁定{
[图像路径释放];
[super dealoc];
}
@结束

也许您想看看我的viewcontroller,它可以做到这一点。我写这篇文章是为了回答这个问题。
也许这对你来说太复杂了,但不会变得更容易。
这只是一个基本版本,它在开始时将所有图像加载到内存中。这在实际应用程序中不起作用。因此,您必须实现一些UIScrollView委托函数。复杂性就从这里开始

//  ImageViewController.h
//
//  Created by Matthias Bauch on 12.10.10.
//  Copyright 2010 Matthias Bauch. All rights reserved.
//

#import <UIKit/UIKit.h>

#warning this is just a quick hack, you should not use this if you dont understand this. There might be leaks, bugs and a lot of whatever.

@interface ImageViewController : UIViewController {
    NSString *imagePath;
}
@property (nonatomic, copy) NSString *imagePath;
- (id)initWithImageDirectory:(NSString*)imgPath;
@end


//
//  ImageViewController.m
//
//  Created by Matthias Bauch on 12.10.10.
//  Copyright 2010 Matthias Bauch. All rights reserved.
//

#import "ImageViewController.h"


@implementation ImageViewController
@synthesize imagePath;

- (id)initWithImageDirectory:(NSString*)imgPath {
    if (self = [super init]) {
        imagePath = [imgPath copy];
    }
    return self;
}


- (UIView *)viewFullOfImagesAtPath:(NSString *)path withSize:(CGSize)size {
    NSError *error = nil;
    NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    if (!filenames) {
        NSLog(@"Error accessing files: %@ [%@]", [error localizedDescription], error);
        return nil;
    }
    UIView *aView = [[UIView alloc] init];
    CGFloat xOffset = 0;
    for (NSString *filename in filenames) {
        NSString *fullPath = [path stringByAppendingPathComponent:filename];
        UIImage *image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease];
        if (!image)
            continue;
        CGRect frameRect = CGRectMake(xOffset, 0, size.width, size.height);
        UIImageView *imageView = [[[UIImageView alloc] initWithFrame:frameRect] autorelease];
        [imageView setImage:image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [aView addSubview:imageView];
        xOffset += size.width;
    }
    aView.frame = CGRectMake(0, 0, xOffset, size.height);
    return [aView autorelease];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
    scrollView.pagingEnabled = YES;
    UIView *contentView = [self viewFullOfImagesAtPath:imagePath withSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)];
    NSLog(@"%f %f %f %f", contentView.frame.origin.x, contentView.frame.origin.y, contentView.frame.size.width, contentView.frame.size.height);
    [scrollView addSubview:contentView];
    scrollView.contentSize = CGSizeMake(CGRectGetWidth(contentView.frame), CGRectGetHeight(contentView.frame));
    [self.view addSubview:scrollView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [imagePath release];
    [super dealloc];
}


@end
//ImageViewController.h
//
//由Matthias Bauch于2010年10月12日创作。
//警察