iPhone: UIScrollView Paging abilitato con NO ZOOM e Anteprima
-
29-09-2019 - |
Domanda
Voglio realizzare un UIScrollView in cui il paging è attivato e posso solo sfogliare alcune immagini. Questo è tutto quello che voglio essere in grado di fare per ora.
L'ho fatto finora nel costruttore di interfaccia: può aiutare qualcuno
Non so come fare il resto. Qualcuno può aiutarmi con questo. Non ho bisogno di alcuna funzionalità di zoom. Non voglio alcun un'anteprima dell'immagine precedente o successiva all'interno del ScrollView, voglio solo un semplice paging abilitato vista di scorrimento che consente all'utente di sfogliare le immagini.
Tutto l'aiuto è apprezzato. Se potesse dirmi passo per passo come ho potuto raggiungere questo obiettivo che sarebbe più apprezzato. grazie.
Ho guardato gli esempi di codice e hanno appena hanno troppa complessità in corso. Ive guardato diversi e preferiscono un tutorial dall'inizio. grazie
Soluzione
Sembra che tu solo bisogno di aggiungere il contenuto come una visualizzazione secondaria del UIScrollView e aggiungere un sistema di riconoscimento gesto.
Carica l'immagine in un'UIImageView. Aggiungere l'UIImageView come visualizzazione secondaria del 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];
Aggiungere un UISwipeGestureRecognizer al UIScrollView.
// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];
Al gestore UISwipeGestureRecognizer, cambiare l'immagine caricata nella UIImageView.
- (void)handleSwipe:(UIGestureRecognizer *)swipe {
// do what you need to determine the next image
imageView.image = [UIImage imageNamed:<your replacement image here>];
}
Altri suggerimenti
Forse si vuole dare un'occhiata al mio implementazione campione di un viewcontroller che fa esattamente questo. Ho scritto questa cosa come risposta a questa domanda .
Forse questo è troppo complicato per te, ma non otterrà affatto più facile.
E questa è solo la versione base, che carica tutte le immagini in memoria all'avvio. Questo non funziona in un'applicazione reale. Quindi bisogna implementare alcune funzioni UIScrollView-delegato. E lì inizia la complessità ...
// 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