iPhone:UIScrollView分页启用没有ZOOM和没有预览

时间:2010-10-13 11:39:46

标签: iphone objective-c uiscrollview paging programmatically-created

我想实现一个启用分页的UIScrollView,我可以浏览一些图像。这就是我现在想做的一切。

到目前为止,我已经在界面构建器中完成了这个:有人可以帮忙吗?

alt text

我不知道如何做其余的事情。有人可以帮我这个。我不需要任何缩放功能。我不想在scrollview中预览上一个或下一个图像,我只想要一个简单的分页启用滚动视图,允许用户浏览图像。

感谢所有帮助。 如果你能一步一步地告诉我如何实现这一点,我将不胜感激。谢谢。

我看过代码示例,他们只是有太多的复杂性。我看过几个,从一开始就喜欢教程。谢谢

2 个答案:

答案 0 :(得分:3)

也许你想看看我的sample implementation视图控制器确实如此。我写这个东西作为this question的回答 也许这对你来说太复杂了,但它不会变得更容易 这只是基本版本,它在开始时将所有图像加载到内存中。这不适用于实际应用程序。所以你必须实现一些UIScrollView-Delegate函数。复杂性开始......

//  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

答案 1 :(得分:1)

听起来您只需将内容添加为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];

将UISwipeGestureRecognizer添加到UIScrollView。

// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];

在UISwipeGestureRecognizer处理程序上,更改UIImageView中加载的图像。

- (void)handleSwipe:(UIGestureRecognizer *)swipe {
  // do what you need to determine the next image
  imageView.image = [UIImage imageNamed:<your replacement image here>];
}