代码之家  ›  专栏  ›  技术社区  ›  Pavan

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

  •  0
  • Pavan  · 技术社区  · 15 年前

    我想实现一个uiscrollview,在这个视图中启用分页,我只需浏览一些图像。这就是我现在想做的。

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

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

    感谢您的帮助。 如果你能一步一步地告诉我如何做到这一点,我将不胜感激。谢谢。

    我看过代码示例,它们的复杂性太大了。我看了几本书,从一开始就喜欢上教程。谢谢你我可以快速浏览一些图片。这就是我现在想做的。

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

    alt text

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

    感谢您的帮助。 如果你能一步一步地告诉我如何做到这一点,我将不胜感激。谢谢您。

    我看过代码示例,它们的复杂性太大了。我看了几本书,从一开始就喜欢上教程。谢谢您

    2 回复  |  直到 12 年前
        1
  •  1
  •   Altealice    15 年前

    听起来您只需要将内容添加为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>];
    }
    
        2
  •  3
  •   Community Mohan Dere    9 年前

    也许你想看看我的 sample implementation 对一个执行此操作的视图控制器。我写这封信是为了回答 this question .
    也许这对你来说太复杂了,但不会变得更容易。
    这只是基本版本,它在开始时将所有图像加载到内存中。这在实际的应用程序中不起作用。因此,您必须实现一些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