- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试滚动到在 webView 中查看的 PDF 的上次查看位置。 PDF卸载时,会保存webView的scrollView的y偏移量。然后当 PDF 重新打开时,我想跳到他们离开的地方。
下面的代码在 animated 设置为 YES 时工作正常,但是当它设置为 NO 时,什么也没有发生
float scrollPos = [[settingsData objectForKey:kSettingsScrollPosition]floatValue];
NSLog(@"scrolling to %f",scrollPos);
[webView.scrollView setContentOffset:CGPointMake(0, scrollPos) animated:NO];
NSLog(@"ContentOffset:%@",NSStringFromCGPoint(webView.scrollView.contentOffset));
这个输出:
滚动到 5432.000000
CO:{0, 5432}
但是PDF仍然显示首页
我在这里查看了类似问题的答案,但他们没有解决这个问题。
感谢您的帮助:)
最佳答案
在 UIWebView
组件完成 PDF 渲染之前,您不能触摸 contentOffset
。它适用于 setContentOffset: animated:YES
因为动画会强制渲染。
如果在渲染开始后将contentOffset
设置为至少0.3s(根据我的测试),则完全没有问题。
例如,如果您在 UIViewController
的 viewDidLoad
中加载 PDF,您可以在 中使用
延迟performSelector:withObject:afterDelay:
>viewDidAppear:contentOffset
设置。
要在设置 contentOffset
之前隐藏 PDF,您可以将其 alpha 设置为 0.01(不要将其设置为 0,除非渲染不会开始)并在设置后将其设置回 1 contentOffset
。
@interface ViewController : UIViewController
{
UIWebView *w;
}
@property (nonatomic, retain) IBOutlet UIWebView *w;
@end
@implementation ViewController
@synthesize w;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *u = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf"];
[w loadRequest:[NSURLRequest requestWithURL:u]];
w.alpha = 0.01f;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:@selector(adjust) withObject:nil afterDelay:0.5f];
}
- (void)adjust
{
float scrollPos = 800;
NSLog(@"scrolling to %f",scrollPos);
[w.scrollView setContentOffset:CGPointMake(0, scrollPos) animated:NO];
NSLog(@"ContentOffset:%@", NSStringFromCGPoint(w.scrollView.contentOffset));
w.alpha = 1;
}
@end
关于ios - setContentOffset : animated: wont do anything if animated = NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730815/
子类化时是否允许覆盖 UIScrollView 的 setContentOffset 方法?这个 AppStore 有存档吗? 样本: -(void)setContentOffset:(CGPoint
如果我设置 contentOffset (无动画)UIScrollView在滚动时, ScrollView 会跳转到正确的偏移量,然后继续滚动。如何防止它继续滚动? 最佳答案 您可以尝试通过调用另一个
我正在尝试使用 contentOffset 方法将表格滚动到最后一行。当数据不多时,此方法工作正常,但如果用户滚动到太多,它会将表格移到最后一个单元格。我正在使用以下代码 self.delega
我有一个应用程序,我需要将 UIScrollView 的 contentOffset 初始化为某个值。我注意到当我设置 contentOffset 时,它会将点四舍五入为最接近的整数。我已经尝试同时使
出于某种奇怪的原因,当设置 scrollofset 时,我的 UITextView 的文本显示为裁剪。 这是它的样子: 执行以下操作后会发生这种情况: textview.content
我有一个关于 setContentOffset 的奇怪问题,我似乎无法解决:我正在尝试构建一个“无尽”的 ScrollView ,所以我想在某个位置重置内容偏移量。使用下面的代码,setContent
我想滚动到 UIScrollView 的顶部.你有两种可能: setContentOffset() scrollView.contentOffset 如果我使用动画,则 ScrollView 有时会滚
我用 subview 初始化 UIScrollView。按钮操作后,我想: 添加新的 subview 滚动到带有动画的新 subview 动画完成后删除旧的 subview 。 为此,我执行以下操作:
我的代码是这样的: [tableview setContentOffset:CGPointMake(0,180) animated]; 但有时,这是不正确的,tableview的y偏移仅为20或其他某
我正在尝试找出 ScrollView 以及如何使它们以编程方式滚动。 我在一个 View Controller 中设置了 2 个屏幕( ImageView )。 我通过选择顶部、底部、左侧和右侧以及高
我有一个 UIScrollView,它显示了一系列不同的图。我希望 UIScrollView 从左滚动到右,所以我想将 UIScrollView 的 ContentOffset 设置为 Content
当所选单元格到达 UITableView 中的最后一个位置时,我试图将 UITableViewCell 向上滚动一个 UITableViewCell 位置 我有正确的逻辑来识别 UITableView
当我的 View 加载时,我正在模拟从 API 获取数据的拉动刷新。 [refreshControl beginRefreshing]; CGPoint newOffset = CGPointMake
我正在为 iPad pro 12.9 创建家谱应用程序。这是我在 Storyboard 中看到的布局。 Family Tree Layout 我创建了一个高大的 View Controller 来容纳
我有一个 UITableViewController,当用户滚动到顶部时,我想在第一个单元格上方添加一堆单元格而不影响当前滚动位置(就像 iMessage 一样) 我知道我可以使用 insertRow
我正在使用 Collection View 。尽管可以将 directionLockEnabled 设置为 YES,但仍然启用对角线滚动。 所以我在某处找到了解决方案: - (void)scrollV
这真的很奇怪。我有一个包含三张图片的 ScrollView ,用户滑动以查看下一张图片。但是,我希望第一个屏幕从中间图像开始。简单的;我将使用 setContentOffset,一切都会好起来的。 该
我在理解 setContentOffset 机制时遇到问题,如果我的 tableView 的 contentSize.height 为 100 点并且我将内容偏移量设置为 200 点,则表格不会不会像
我有一个 UIScrollView,里面有一些标签。我可以使用按钮将 ScrollView 移动到另一个“页面”。但是当我推得太快时,偏移量不正确。 我将 ScrollView 移动到下一页的代码:
我的问题是关于这个方法: (void)setContentOffset:(CGPoint)contentOffset 动画:(BOOL)animated 我已经阅读了文档,但我不明白这个方法是干什么用
我是一名优秀的程序员,十分优秀!