
当你开发图标或者任何有输入区域的界面,偶尔输入框再键盘d出时会被挡住。这样用户体验不好,用户在输入时看不到他们所输入的东西。一个解决方案,是滑动整个vIEw让编辑区域一直是可见的。
我提供的整个解决方案对UIVIEw添加了一些方法(我知道,添加类别到cocoa的类是顽皮的)这将决定基于整个屏幕的输入位置滑动视图的多少,还有和键盘d起一样的速度滑动视图。在编辑完成时滑动回到原来的位置。
做到这样很简单,这是我如何通过计算来滚动视图:
- (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 }}
这里是工程拷贝(见我的51CTO博客同名译文附件)。
总结以上是内存溢出为你收集整理的iphone:保持表单输入的可见性——自动滚动视图全部内容,希望文章能够帮你解决iphone:保持表单输入的可见性——自动滚动视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)