Question

je veux mettre en place un UIScrollView où la pagination est activée et je peux feuilleter quelques images. C'est tout ce que je veux être en mesure de le faire pour l'instant.

Je l'ai fait jusqu'à présent dans le constructeur d'interface: Quelqu'un peut-il aider

text alt

Je ne sais pas comment faire le reste. Quelqu'un pourrait m'aider avec ça. Je ne demande aucune fonctionnalité de zoom. Je ne veux pas que tout aperçu de la précédente image suivante ou dans le scrollview, je veux juste un échange simple, activé vue défilement qui permet à un utilisateur de feuilleter les images.

Toute aide est appréciée. Si vous pouviez me dire pas à pas comment je pourrais réaliser ce qui serait le plus apprécié. je vous remercie.

Je l'ai regardé des exemples de code et ils ont trop de complexité en cours. Ive regardé plusieurs et préfèrent un tutoriel depuis le début. merci

Était-ce utile?

La solution

On dirait que vous avez juste besoin d'ajouter votre contenu en tant que sous-vue du UIScrollView et ajouter un geste de reconnaissance.

Chargez votre image dans un UIImageView. Ajouter le UIImageView comme sous-vue du 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];

Ajouter un UISwipeGestureRecognizer à UIScrollView.

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

Le gestionnaire UISwipeGestureRecognizer, changer l'image chargée dans le UIImageView.

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

Autres conseils

peut-être vous voulez jeter un oeil à mon échantillon d'un viewcontroller qui fait exactement cela. J'ai écrit cette chose comme réponse à cette question.
Peut-être cela est trop compliqué pour vous, mais il ne sera pas simple.
Et ce n'est que la version de base, qui charge toutes les images en mémoire au démarrage. Cela ne fonctionnera pas dans une application réelle. Vous devez donc mettre en œuvre certaines fonctions UIScrollView-délégué. Et là le début de la complexité ...

//  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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top