iphone:保持表单输入的可见性——自动滚动视图
2011-08-16 08:27
281 查看
iPhone: Maintain visibility
of form inputs – auto-scrolling views
当你开发图标或者任何有输入区域的界面,偶尔输入框再键盘弹出时会被挡住。这样用户体验不好,用户在输入时看不到他们所输入的东西。一个解决方案,是滑动整个view让编辑区域一直是可见的。我提供的整个解决方案对UIView添加了一些方法(我知道,添加类别到cocoa的类是顽皮的)这将决定基于整个屏幕的输入位置滑动视图的多少,还有和键盘弹起一样的速度滑动视图。在编辑完成时滑动回到原来的位置。
做到这样很简单,这是我如何通过计算来滚动视图:
- (void) maintainVisibityOfControl:(UIControl *)control offset:(float)offset { static const float deviceHeight = 480; static const float keyboardHeight = 216; static const float gap = 5; //gap between the top of keyboard and the control //Find the controls absolute position in the 320*480 window - it could be nested in other views CGPoint absolute = [control.superview convertPoint:control.frame.origin toView:nil]; //If it would be hidden behind the keyboard.... if (absolute.y > (keyboardHeight + gap)) { //Shift the view float shiftBy = (deviceHeight - absolute.y) - (deviceHeight - keyboardHeight - gap - offset); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3f]; //this is speed of keyboard CGAffineTransform slideTransform = CGAffineTransformMakeTranslation(0.0, shiftBy); self.transform = slideTransform; [UIView commitAnimations]; } }
[/code]
..然后我重置了视图通过使用:
- (void) resetViewToIdentityTransform { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3f]; //this is speed of keyboard CGAffineTransform slideTransform = CGAffineTransformIdentity; self.transform = slideTransform; [UIView commitAnimations]; }
[/code]
你只需要对你的代码做一些小的改动,并在UITextFieldDelegate调用这些方法(或其他控制代理):
- (void) textFieldDidBeginEditing:(UITextField *)textField { [self.view maintainVisibityOfControl:textField offset:0.0f]; } - (void)textFieldDidEndEditing:(UITextField *)textField { if (textField == currentControl) { //If the textfield is still the same one, we can reset the view animated [self.view resetViewToIdentityTransform]; }else { //However, if the currentControl has changed - that indicates the user has //gone into another control - so don't reset view, otherwise animations jump around } }
[/code]
这里是工程拷贝(见我的51CTO博客同名译文附件)。
相关文章推荐
- iphone:保持表单输入的可见性——自动滚动视图
- iPhone: Maintain visibility of form inputs – auto-scrolling views【保持表单输入的可视性 - 自动滚动】
- 用 Ruby on Rails 和 Eclipse 开发 iPhone 应用程序之 3 :构建视图和表单
- 一个简单的注册表单验证保持原来输入的内容实例
- iPhone UITableViewCell如何滚动到视图顶端
- iPhone设置滚动视图
- 获取浏览器顶部距离,可见距离,屏幕宽度,滚动条保持状态,滚动条距离顶部,底部的距离,屏幕分辨率等
- 定时器 · UIScrollView滚动视图自动翻页滚动
- asp.net 程序自动提交登陆表单并保持Session及Cookie
- 借助CSS Shapes实现元素滚动自动环绕iPhone X的刘海
- puppeteer 实例:自动输入表单并提交搜索显示新的页面
- 滚动视图(UIScrollView)无限无缝左右切换图片,自动顺序加逆序循环播放图片
- ios UIScrollView自动滚动 循环滚动视图实现
- 7.28 UIView 剪切半径 和 ios滚动视图的自动偏移特性
- 当达到输入长度时表单自动切换焦点
- 如何在滚动报表时保持标题可见 (Reporting Services)
- OCUI界面设计:滚动视图与分页控件结合NSTimer实现图片自动循环与无限滚动展示
- iOS中UITableView在视图出现时自动滚动到底部(无痕,不闪动)
- iPhone UITableViewCell如何滚动到视图顶端。
- 输入表单输入完一个后按住enter光标自动跳到下一个表单