iPhone:启用了uisCrollView,没有Zoom,也没有预览
-
29-09-2019 - |
题
我想实现一个启用分页的UISCrollView,我可以翻阅一些图像。那就是我现在想做的一切。
我已经在接口构建器中做到了这一点:有人可以帮忙吗?
我不知道该怎么做。有人可以帮我吗?我不需要任何缩放功能。我不希望在ScrollView中的上一个或下一个图像的预览,我只希望一个简单的Pagging启用滚动视图,该视图允许用户翻阅图像。
所有帮助都得到赞赏。如果您能逐步告诉我,我该如何实现这一目标。谢谢你。
我查看了代码示例,它们的复杂性太多了。我看了几个,从一开始就喜欢一个教程。谢谢你
解决方案
听起来您只需要添加内容作为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];
将UiswipeGesturerCognizer添加到UisCrollView。
// 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>];
}
其他提示
也许你想看看我的 样本实现 确切地说的ViewController。我写了这件事作为答案 这个问题.
也许这对您来说太复杂了,但是不会变得更容易。
这只是基本版本,它在开始时将所有图像加载到内存中。这将在真实应用程序中不起作用。因此,您必须实现一些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
不隶属于 StackOverflow