设备旋转后UITableView校正滚动位置 [英] UITableView correcting scroll position after device rotation

查看:74
本文介绍了设备旋转后UITableView校正滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一本书构建类似读者的东西。当用户旋转手机时,我想增加字体大小。我正在使用 UITableView 来显示文本块。

I'm building something like a reader for a book. When the user rotates the phone I want to increase the font size. I'm using a UITableView to display chunks of text.

问题是,增加字体大小会增加高度在我的表视图中的行,如果我在纵向模式下读取段落320,我在横向模式下得到280或类似的东西。

Problem is that, increasing the font size increases height of rows in my table view and if I was reading paragraph 320 in portrait mode I get 280 or something similar in landscape mode.

我已经设置了一个旋转通知监听器此代码:

I have set up a rotation notification listener using this code:

    UIDevice *device = [UIDevice currentDevice];                
    [device beginGeneratingDeviceOrientationNotifications];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self                                
           selector:@selector(orientationChanged:)
               name:UIDeviceOrientationDidChangeNotification
             object:device];

并尝试在旋转前保存最后一个段落索引,然后在旋转后向下滚动到它但我似乎无法达到预期的效果。

and tried to save the last paragraph index before rotation and then scroll down to it after the rotation but I can't seem to achieve desired effect.

处理这种情况的最佳方法是什么?我在哪里实际实现之前和之后轮换状态?

What's the best way to handle this kind of situation and where do I actually implement "before" and "after" states of rotation?

我希望它可以在iOS 4 +上运行。

I'd like it to work on iOS 4+.

推荐答案

因为我无法获得一个好的答案,我会回答自己。我到处寻找但找不到办法去做我想要的事情所以我只是用 shouldAutorotateToInterfaceOrientation 方法来增加字体大小,然后开始一个小线程睡眠0.2秒,然后滚动到所需的行。感谢您的帮助。

Since I couldn't get a good answer I'll answer myself. I've looked everywhere but couldn't find a way to do what I wanted so I just used the shouldAutorotateToInterfaceOrientation method to increase the size of font and then start a little thread that sleeps for 0.2 seconds and after that scrolls to the desired row. Thanks for your help.

编辑:使用委托方法willRotateToInterfaceOrientation:将可见单元存储在数组中然后使用委托方法didRotateFromInterfaceOrientation:滚动到可见的索引路径你记录在数组中。

Use delegate method willRotateToInterfaceOrientation: to store the visible cell in an array then use the delegate method didRotateFromInterfaceOrientation: to scroll to the visible index path that you recorded in the array.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    // Save the visible row position
    visibleRows = [tableview indexPathsForVisibleRows];
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    // Scroll to the saved position prior to screen rotate
    [tableView scrollToRowAtIndexPath:[visibleRows objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

这篇关于设备旋转后UITableView校正滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆