我正在尝试按照这个答案创建一个显示下一个和上一个图像的分页滚动视图。
我创建了一个自定义 UIView 类,并将 UIScrollView 添加为子视图。我有两个问题。
- 我希望视图与 IB 一起工作,所以我实现了 initWithCoder,并在调用超类 initWithCoder 后尝试将帧传递给我的 init。但框架的宽度和高度始终为 0。
- 为了解决这个问题,我只是在我的 initView 方法中使用了屏幕宽度。但是我的滚动视图(或者更确切地说是其中的图像)没有显示。我更改了自定义视图(红色)和滚动视图(蓝色)的背景颜色,我看到的只是红色背景。我检查了滚动视图的框架矩形,它看起来很合理(59、0、201、135),所以我不确定为什么我不能显示滚动视图。
任何想法将不胜感激。
模式选择视图.m
#import "ModeSelectView.h"
@implementation ModeSelectView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initView:frame];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:(aDecoder)];
if (self) {
CGRect frame = self.frame;
[self initView:self.frame];
}
return self;
}
- (void)initView:(CGRect)frame
{
// Initialization code
// Create an array of images for the different modes
UIImage *imgTowerOnlyMode = [UIImage imageNamed:@"tower_only_mode_icon.png"];
UIImage *imgLocalMode = [UIImage imageNamed:@"local_mode_icon.png"];
UIImage *imgHowToPlay = [UIImage imageNamed:@"how_to_play_icon.png"];
NSArray *modeSelectIcons = @[imgTowerOnlyMode, imgLocalMode, imgHowToPlay];
int iconSize = 115;
// Center to center icon spacing
int viewSpacing = 115 * 1.75;
int frameWidth = 2*viewSpacing;
int contentWidth = 4*frameWidth;
int pageWidth = viewSpacing;
int verticalPadding = 10;
int pageHeight = iconSize + 2*verticalPadding;
//int viewWidth = frame.size.width;
int viewWidth = [[UIScreen mainScreen] applicationFrame].size.width;
int scrollViewX = (viewWidth - pageWidth)/2;
// Create the scrollview, setting it to be page size in width the icon height plus padding
CGRect scrollViewFrame = CGRectMake(scrollViewX, 0, pageWidth, pageHeight);
self.scrollModeSelect = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
// Now iterate over the array creating a view for each
// The first view will be offset in X to allow it to be
// centered in the page
int imageOffset = (frameWidth-iconSize)/2;
for (int i = 0; i < [modeSelectIcons count]; ++i) {
// Get the origin x value for the image view within the scroll view
int viewOriginX = i*frameWidth + imageOffset;
// Initialize the image view
UIImageView *ivModeSelect = [[UIImageView alloc]
initWithFrame:CGRectMake(viewOriginX , verticalPadding/2,
iconSize, iconSize)];
// Set the image
ivModeSelect.image = (UIImage *)modeSelectIcons[i];
// Tell the parent view to scale the iamge, preserving aspect ratio, to
// fit the parent view.
ivModeSelect.contentMode = UIViewContentModeScaleAspectFit;
// Add the image view to the scroll view
[self.scrollModeSelect addSubview:ivModeSelect];
}
[self.scrollModeSelect setContentSize:CGSizeMake(contentWidth, pageHeight)];
// Turn off clipping so we can see the adjacent icons
[self.scrollModeSelect setClipsToBounds:FALSE];
// Add the scrollview as a subview
[self addSubview:self.scrollModeSelect];
[self.scrollModeSelect setBackgroundColor:[UIColor blueColor]];
[self setBackgroundColor:[UIColor redColor]];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end