iphone:保持表单输入的可见性——自动滚动视图
2011-08-16 08:24
211 查看
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]; } }
..然后我重置了视图通过使用:
- (void) resetViewToIdentityTransform { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3f]; //this is speed of keyboard CGAffineTransform slideTransform = CGAffineTransformIdentity; self.transform = slideTransform; [UIView commitAnimations]; }
你只需要对你的代码做一些小的改动,并在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 } }
这里是工程拷贝(见附件)。
相关文章推荐
- iphone:保持表单输入的可见性——自动滚动视图
- iPhone: Maintain visibility of form inputs – auto-scrolling views【保持表单输入的可视性 - 自动滚动】
- 用 Ruby on Rails 和 Eclipse 开发 iPhone 应用程序之 3 :构建视图和表单
- iPhone UITableViewCell如何滚动到视图顶端
- iPhone设置滚动视图
- iOS tableView下拉顶部视图放大 自动滚动 无限滚动 自动布局 在code4App和github上早就上传了代码,这边记录下
- asp.net 程序自动提交登陆表单并保持Session及Cookie
- Swift UIScrollView滚动视图/无限循环滚动/自动滚动
- 滚动视图(UIScrollView)无限无缝左右切换图片,自动顺序加逆序循环播放图片
- iOS--UIScrollView的一个封装好的自动滚动视图
- ios UIScrollView自动滚动 循环滚动视图实现
- Qml输入文本并自动滚动
- Loop--Banner循环自动滚动视图
- 一个简单的注册表单验证保持原来输入的内容实例
- web安全 应用表单密码类型输入启用了自动完成操作
- 7.28 UIView 剪切半径 和 ios滚动视图的自动偏移特性
- 当达到输入长度时表单自动切换焦点
- iOS 新特性分列式 之 iOS 6.x - 主要内容:自动布局、集合视图、UI 状态保持
- 如何在滚动报表时保持标题可见 (Reporting Services)
- 一个简单的注册表单验证保持原来输入的内容实例