我有一个应用程序,我有一个 640 x 9000 的UIScrollView TheoryViewController 。
看到内容如此之多,我希望用户能够关闭视图( TheoryViewController ),(或退出应用),以及当他们重新启动应用并打开< em> TheoryViewController ,我希望ScrollView位置再次位于最后位置。
我已经找了几个选项,但要么被弃用,要么是在Swift中。
我正在运行Xcode 8并定位iOS 8 SDK。
答案 0 :(得分:2)
将当前offset
保存在UserDefaults
CGPoint tableOffset = [tableView contentOffset];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:NSStringFromCGPoint(tableOffset) forKey:@"pointPosition"];
[userDefaults synchronize];
滚动到已保存的位置:
CGPoint thePoint = CGPointFromString([userDefaults objectForKey:@"tableOffset"]);
[tableView setContentOffset:thePoint animated:NO]];
答案 1 :(得分:1)
将contentOffset
保存在NSUserDefaults
中。然后,当视图再次出现时,使用setContentOffset()
滚动到该位置。
// Save.
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:NSStringFromCGPoint(self.scrollView.contentOffset) forKey:@"scrollViewOffset"];
... somewhere else in your code ...
// Restore.
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
CGPoint offset = CGPointFromString([userDefaults objectForKey:@"scrollViewOffset"]);
[self.scrollView setContentOffset:offset animated:NO]
答案 2 :(得分:1)
您还需要contentSize,contentOffset和zoomScale才能获得准确的输出
您可以将所有内容存储到UserDefault中,并在需要时再次恢复
点击此处查看您如何处理NSUserDefaults
https://stackoverflow.com/a/4562488/4601900
NSMutableDictionary *dict = [@{
kZoomLevel : [NSString stringWithFormat:@"%f", self.zoomScale],
kRotationDegree :[NSString stringWithFormat:@"%f", self.rotationAngle],
kScrollViewContentSize : NSStringFromCGSize(self.scrollViewPhoto.contentSize),
kScrollViewContentOffset : NSStringFromCGPoint(self.scrollViewPhoto.contentOffset),
}mutableCopy];
你需要以这种方式恢复它
if (dictMainData) {
NSString *contentSize = [dictMainData valueForKey:kScrollViewContentSize];
if (contentSize) {
self.contentSize = CGSizeFromString(contentSize);
self.scrollViewPhoto.contentSize = self.contentSize;
}
NSString *contentOffset = [dictMainData valueForKey:kScrollViewContentOffset];
if (contentOffset) {
self.contentOffset = CGPointFromString(contentOffset);
self.scrollViewPhoto.contentOffset = self.contentOffset;
}
NSString *strZoomLevel = [dictMainData valueForKey:kZoomLevel];
if ([strZoomLevel floatValue] > 0) {
self.zoomScale = [strZoomLevel floatValue];
self.viewBase.transform = CGAffineTransformMakeScale(self.zoomScale,[strZoomLevel floatValue]);
CGRect frame = self.viewBase.frame;
frame.origin = CGPointMake(0, 0);
self.viewBase.frame = frame;
}
NSString *strRotationDegree = [dictMainData valueForKey:kRotationDegree];
if ([strRotationDegree floatValue] > 0) {
self.rotationAngle = [strRotationDegree floatValue];
self.imageView.transform = CGAffineTransformMakeRotation(self.rotationAngle * M_PI/180);
//[self rotateTargetPointsAsPerRotationAngle];
}
}