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

问题描述 投票:0回答:2

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

到目前为止,我已经在界面生成器中完成了此操作:有人可以帮忙吗?

alt text

剩下的我不知道该怎么做。有人可以帮我解决这个问题吗?我不需要任何缩放功能。我不想在滚动视图中预览上一张或下一张图像,我只想要一个简单的启用分页的滚动视图,允许用户轻拂图像。

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

我查看了代码示例,发现它们过于复杂。我看过几个并且更喜欢从头开始的教程。谢谢你

objective-c iphone uiscrollview programmatically-created
2个回答
3
投票

也许你想看看我的视图控制器的示例实现,它确实做到了这一点。我写这个东西是为了回答这个问题
也许这对你来说太复杂了,但它不会变得更容易。
这只是基本版本,它在启动时将所有图像加载到内存中。这在实际应用中是行不通的。所以你必须实现一些 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
投票

听起来您只需将内容添加为 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>];
}
© www.soinside.com 2019 - 2024. All rights reserved.