【问题标题】:iphone: UIScrollView Paging enabled with NO ZOOM and NO PREVIEWiphone:UIScrollView 分页启用,无缩放和无预览
【发布时间】:2010-10-13 11:39:45
【问题描述】:

我想实现一个 UIScrollView,其中启用了分页,我可以浏览一些图像。这就是我现在想做的所有事情。

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    也许你想看看我的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
    

    【讨论】:

    • 我不介意复杂性,我介意的是不需要的额外功能,例如放大功能或/和预览功能。我不介意创建委托函数,它只是让事情变得更干净,但我只需要知道如何去做。
    • 正如你所说:“它是一个 hack ......不应该使用它,因为它可能包含很多泄漏错误和很多任何东西”。如果您能指导我做一些有用的事情,那就太好了。我很感激你的时间。感谢您提供帮助。
    • 这并不是一个现成的解决方案。它更像是指向一个可能方向的指针。
    【解决方案2】:

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

    向 UIScrollView 添加一个 UISwipeGestureRecognizer。

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

    【讨论】:

    • 伪代码说得好,有没有教程的例子让我可以学习?或者你可以写一个作为答案,一步一步说?谢谢
    • 添加了示例代码。我没有时间写一个完整的教程,但我认为这涵盖了你应该需要的内容,减去你的图像跟踪逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    相关资源
    最近更新 更多