문제

i want to implement a UIScrollView where paging is enabled and i can just flick through some images. Thats all i want to be able to do for now.

I have done this so far in interface builder: can someone help?

alt text

I dont know how to do the rest. Can someone please help me with this. I dont need any zooming functionality. I dont want any preview of the previous or next image within the scrollview, i just want a simple paging enabled scroll view that allows a user to flick through images.

All help is appreciated. If you could tell me step by step how i could achieve this that would be most appreciated. thank you.

I've looked at code examples and they just have too much complexity going on. Ive looked at several and prefer a tutorial from the beginning. thank you

도움이 되었습니까?

해결책

Sounds like you just need to add your content as a subview of the UIScrollView and add a gesture recognizer.

Load your image into a UIImageView. Add the UIImageView as subview of the 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];

Add a UISwipeGestureRecognizer to the UIScrollView.

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

On the UISwipeGestureRecognizer handler, change the loaded image in the UIImageView.

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

다른 팁

maybe you want to take a look at my sample implementation of a viewcontroller that does exact this. I wrote this thing as answer to this question.
Maybe this is too complicated for you, but it won't get any easier.
And this is only the basic version, which loads all images into memory at start. This will not work in a real application. So you have to implement some UIScrollView-Delegate functions. And there the complexity starts...

//  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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top