我正在尝试将一组用户交互视图添加到我现有的代码中。我有以下代码和一个数组,该数组使用滚动视图(带有页面控件)来显示一组图像。我想修改代码以显示在界面生成器中创建的一组用户交互视图。通过进行用户交互,这些视图将具有按钮和可缩放的图像视图。
------当前代码-----
带有滚动视图的视图控制器的实现文件(.m)
#import "AboutViewController.h"
@interface AboutViewController ()
@end
@implementation AboutViewController
@synthesize scrollView;
@synthesize pageControl;
@synthesize imageArray;
int page;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Can we load and display ViewControllers as opposed to images?
imageArray = [[NSArray alloc] initWithObjects:@"1.png", @"2.png", @"3.png", nil];
for (int i = 0; i < [imageArray count]; i++ ) {
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
[self.scrollView addSubview:imageView];
NSLog (@"page %d",page);
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.
width *[imageArray count],
scrollView.frame.size.height);
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = self.scrollView.frame.size.width;
//calculate current page in scrollview
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
NSLog (@"page %d",page);
}
- (IBAction)done:(id)sender
{
[self.delegate aboutViewControllerDidFinish:self];
}
@end
带有滚动视图的视图控制器的头文件(.h)
#import <UIKit/UIKit.h>
@class AboutViewController;
@protocol AboutViewControllerDelegate
- (void)aboutViewControllerDidFinish:(AboutViewController *)controller;
@end
@interface AboutViewController : UIViewController<UIScrollViewDelegate>
@property (strong, nonatomic,retain) id <AboutViewControllerDelegate> delegate;
@property (nonatomic,strong,) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *imageArray;
@property(nonatomic, assign) IBOutlet UILabel *label;
- (IBAction)done:(id)sender; //returns back to main menu after a button is pressed
@end
- - - - - - - - - - - - - -更新 - - - - - - - - - - - ------------
进一步澄清:
对于那些可能加入这个问题的人,早些时候我对在滚动视图中嵌入视图控制器而不是视图感到困惑。我现在相信不可能在滚动视图中嵌入视图控制器,但可以将视图嵌入其中。