开发者

How to save state of UIScrollView?

开发者 https://www.devze.com 2022-12-16 00:58 出处:网络
This has me stumped. I\'m trying to figure out how to save the state of a UIScrollView. For example, I have several images in a UIScrollView that are loaded like so:

This has me stumped. I'm trying to figure out how to save the state of a UIScrollView. For example, I have several images in a UIScrollView that are loaded like so:

    NSUInteger i;
for (i = 1; i <开发者_如何学C;= kNumImages; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
}

Now my question is how can I make it where the user can return to the image that they stopped scrolling at? Does anyone know how to accomplish this? Thanks.


Try this:

NSUserDefaults  *defaults = [NSUserDefaults standardDefaults];
[defaults setFloat: scrollview.contentOffset.x forKey: @"myScrollPositionX"];
[defaults setFloat: scrollview.contentOffset.y forKey: @"myScrollPositionY"];

To restore:

scrollView.contentOffset = CGPointMake([defaults floatForKey: @"myScrollPositionX"], [defaults floatForKey: @"myScrollPositionY"]); 
0

精彩评论

暂无评论...
验证码 换一张
取 消