gpt4 book ai didi

objective-c - UIScrollView 分页有 2 个 View

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:57 26 4
gpt4 key购买 nike

我尝试使用本教程中的 2 个 View 实现分页:

Multiple virtual pages in a UIScrollView with just 2 child views

我想通过将 UIScrollView 放入页面并在每个页面中用新内容更新它来修改它:

-(void)addContent:(NSString*)txt{

int size = 5;
UITextView *txtView;
int row = 0;

//from left
int xPlace = 0;
//space between row's
int rowSpace = 20;
//from top
int yPlace = 10;

// int button_with = scrollView.frame.size.width;
int button_height = 20;

for (int i = 0; i < size; i++) {


txtView = [[UITextView alloc] initWithFrame:CGRectMake(xPlace, row*rowSpace+yPlace, scrollView.frame.size.width, 2*button_height)];
[txtView setFont:[UIFont fontWithName:@"ArialMT" size:14]];
txtView.backgroundColor = [UIColor blueColor];

[txtView setText:[NSString stringWithFormat:@"Numer %@ - %i",txt,i]];

// answerCheck.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];


[scrollView addSubview:txtView];
[txtView release];
row++;
}

[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, txtView.frame.size.height*size)];
}

想法是在滚动时用新内容替换 uiviewcontroller。它调用:

- (void)setPageIndex:(NSInteger)newPageIndex
{
pageIndex = newPageIndex;

if (pageIndex >= 0 && pageIndex < [[DataSource sharedDataSource] numDataPages])
{
NSDictionary *pageData =
[[DataSource sharedDataSource] dataForPage:pageIndex];
label.text = @"Tekstas";//[pageData objectForKey:@"pageName"];
textView.text = [pageData objectForKey:@"pageText"];
[self addContent:[pageData objectForKey:@"pageText"]];
CGRect absoluteRect = [self.view.window
convertRect:textView.bounds
fromView:textView];
if (!self.view.window ||
!CGRectIntersectsRect(
CGRectInset(absoluteRect, TEXT_VIEW_PADDING, TEXT_VIEW_PADDING),
[self.view.window bounds]))
{
textViewNeedsUpdate = YES;
}
}
}

好吧,我卡在这里了。我的 TextView 正在更新,但 UIscrollView 的新内容不会在 iPad 模拟器中更新。

是的,我的意思是在 iPhone 中它可以工作,而在 iPad 中却不能。如果我想实现此功能,我可以改进什么?

也许还有其他方法可以从 iOS 5 进行分页?

我会放一个试图使它工作的源代码。

Download source

最佳答案

您可以只使用 nib 文件添加一个 UIScrollView 并修复 widthheight。现在将一些页面制作成 UIView 并将它们添加到具有相同大小和相同 originUIScrollView 中。在主 UIView 中:

- (void)viewDidLoad
{
[super viewDidLoad];

scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 2);
}

-(void)viewWillAppear:(BOOL)animated{
CGFloat h = scrollView.frame.size.height;
[pageView1 setFrame:CGRectMake(0, 0, pageView1.frame.size.width, pageView1.frame.size.height)];

[pageView2 setFrame:CGRectMake(0, h, pageView2.frame.size.width, pageView2.frame.size.height)];
}

并且不要忘记在 nib 文件的 Attribute 中为 UIScrollView 检查 Paging Enabled .

希望对你有用!

关于objective-c - UIScrollView 分页有 2 个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com