改变了contentinsetstart,视图位置怎么不变

contentInset&和&contentOffset和frame&之间的含义&让我们彻底搞懂!
今天下载了一个,下拉更新的Demo,原理简单。使用方便!先谢谢开发、分享这个好东东的前辈!
但是,中间有几个属性把我搞的晕头转向。是啊,就是contentInset 和 contentOffset
这两个属性,以及响应的设置。
很多效果,最常见的是滑动视图的切换,和滑动。等等很多特效的处理,也是基于这些属性的变换。
所以,能彻底了解,掌握这些属性,对我们以后的提高,是有很大的好处的!
于是乎,“骑驴看唱本-走着瞧”吧。
我们先要看看,页面结构,我只截图,来配合的能够理解。
页面布局,就是一个View+Table
运行后的界面,就是一个全屏的talbeview
那么就,开始攻克堡垒吧!
---------------------------------
是UIView中表示该控件所占有矩形区域。该属性是相对与它的superView(父视图控件)来说的。
[superView insertSubview:childView&atIndex:0];
&childView.frame
= CGRectMake(0,
childView 视图将被定义在superView
视图的(0,0)位置,宽320 高460。(注意,是针对父视图来定位矩形区域)
---------------------------------
contentOffset
官方解释:
contentOffset
The point at which the origin of the content view is offset from
the origin of the scroll view.
是一个坐标点,这个坐标点是滚动视图中内容视图所在的坐标点。
这描述真心别扭啊!!!
<img TYPE="face" src="/blog7style/images/common/sg_trans.gif" real_src ="/uc/myshow/blog/misc/gif/E___6706EN00SIGG.gif"
ALT="contentInset&和&contentOffset和frame&之间的含义&让我们彻底搞懂!"
TITLE="contentInset&和&contentOffset和frame&之间的含义&让我们彻底搞懂!" />
换个思路理解:
可以把滚动视图看成是两层:“UIScrollView外壳” 和 “内容视图”
contentOffset属性是UIScrollView外壳内部,显示内容所在的坐标点。
也就是说,滚动视图中,用户滚动时,滚动的是内容视图,而contentOffset坐标,指代的就是内容视图的坐标。
contentOffset&的默认点为&&
值得注意的是:
1.contentOffset
坐标值,会随着用户滑动内容视图,而改变。
2.内容视图的contentOffset为负数时,在滚动视图中是看不到的。
---------------------------------
contentInset属性
这个属性理解起来,很容易,但是,表述起来有点困难。
直接说效果:就跟,css中padding属性作用是一样的。
scrollView.contentInset = UIEdgeInsetsMake(50,
这样定义后,内容视图开始显示的坐标为(0,51)。
希望对您有所帮助!
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。阿布网—电商加油站!
您当前的位置:&>&&>&&>&
iOS控件详解之UIScrollView概述UIScrollView是一个非常重要的控件,其可以展示比设备屏幕更大区域的内容,我们可以通过手指滑动来查看内容视图的每一部分内容,也可以通过手指捏合来对内容视图进行缩放操作,UITableView、UICollectionView和UITextView等常用控件全部继承自UIScrollView,而UIWebView控件内部也是基于UIScrollView实现的,我们每天开发中都不断显式或隐式地与UIScrollView打交道
注: 本文中所说的”内容视图”在官方文档中称作”content view”,表示UIScrollView中可以用来展示内容的部分属性与方法内容视图相关@property(nonatomic) CGSize contentS@property(nonatomic) UIEdgeInsets contentI@property(nonatomic) CGPoint contentO- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)滑动相关@property(nonatomic,getter=isScrollEnabled) BOOL scrollE@property(nonatomic, getter=isDirectionalLockEnabled) BOOL directionalLockE@property(nonatomic) BOOL scrollsToT@property(nonatomic, getter=isPagingEnabled) BOOL pagingE@property(nonatomic) BOOL@property(nonatomic) BOOL alwaysBounceH@property(nonatomic) BOOL alwaysBounceV@property(nonatomic) CGFloat decelerationR- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)指示器相关@property(nonatomic) UIScrollViewIndicatorStyle indicatorS@property(nonatomic) UIEdgeInsets scrollIndicatorI@property(nonatomic) BOOL showsHorizontalScrollI@property(nonatomic) BOOL showsVerticalScrollI- (void)flashScrollI事件相关UIScrollView处理触摸事件原理当用户在UIScrollView的一个子视图上按下时,UIScrollView并不知道用户是想要滑动内容视图还是点击对应子视图,所以在按下的一瞬间,事件UIEvent从UIApplication传递到UIScrollView后,其会先将该事件拦截而不会立即传递给对应的子视图,同时开始一个150ms的倒计时,并监听用户接下来的行为当倒计时结束前,如果用户的手指发生了移动,则直接滚动内容视图,不会将该事件传递给对应的子视图;当倒计时结束时,如果用户的手指位置没有改变,则调用自身的-touchesShouldBegin:withEvent:inContentView:方法询问是否将事件传递给对应的子视图(如果返回NO,则该事件不会传递给对应的子视图,如果返回YES,则该事件会传递给对应的子视图,默认为YES)当事件被传递给子视图后,如果手指位置又发生了移动,则调用自身的-touchesShouldCancelInContentView:方法询问是否取消已经传递给子视图的事件@property(nonatomic,readonly,getter=isTracking) BOOL@property(nonatomic,readonly,getter=isDragging) BOOL@property(nonatomic,readonly,getter=isDecelerating) BOOL@property(nonatomic) BOOL delaysContentT@property(nonatomic) BOOL canCancelContentT- (BOOL)touchesShouldBegin:(NSSet&UITouch *& *)touches withEvent:(UIEvent *)event inContentView:(UIView *)- (BOOL)touchesShouldCancelInContentView:(UIView *)缩放相关当用户使用两个手指进行缩放操作时,我们调整内容视图的偏移量和缩放比例(注: 用户两个手指操作结束后,有可能仍然有一个手指在操作,这时不会将事件传递给子视图)@property(nonatomic) CGFloat minimumZoomS@property(nonatomic) CGFloat maximumZoomS@property(nonatomic) CGFloat zoomS- (void)setZoomScale:(CGFloat)scale animated:(BOOL)- (void)zoomToRect:(CGRect)rect animated:(BOOL)@property(nonatomic) BOOL bouncesZ@property(nonatomic,readonly,getter=isZooming) BOOL@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomB键盘相关@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissM代理滑动相关- (void)scrollViewDidScroll:(UIScrollView *)scrollV- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollV- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentO- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollV- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollV- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollV- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollV- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollV缩放相关- (void)scrollViewDidZoom:(UIScrollView *)scrollV- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollV- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)常见需求原理解析导航栏半透明效果// 导航栏半透明效果.gif上传失败,有空再传原理解析:默认情况下,在有UINavigationBar存在时,系统为了防止UIScrollView被遮挡,其contentInset和scrollIndicatorInsets属性都会被设置为UIEdgeInsetsMake(64, 0, 0, 0);在有UITabBar存在时,系统为了防止UIScrollView被遮挡,其contentInset和scrollIndicatorInsets属性都会被设置为UIEdgeInsetsMake(0, 0, 49, 0)因此,为了使用此种半透明效果,可以直接将UIScrollView的frame设置为整个屏幕的大小
注1: 系统只在UIScrollView是控制器视图的第0个子视图时才会自动修改contentInset和scrollIndicatorInsets属性
注2: 如果不想让系统自动修改contentInset和scrollIndicatorInsets属性,可以设置self.automaticallyAdjustsScrollViewInsets = NO;控件悬停// 控件悬停.gif上传失败,有空再传原理解析:方式一: 在悬停位置放置一个与待悬停控件相同的控件,通过-scrollViewDidScroll:代理方法跟踪contentOffset的的变化,当不满足悬停条件时,将该控件hidden属性设置为YES;当满足悬停条件时,将该控件hidden属性设置为NO方式二: 通过-scrollViewDidScroll:代理方法跟踪contentOffset的的变化,当不满足悬停条件时,待悬停控件属于UIScrollView的子视图,当满足悬停条件时,待悬停控件属于UIScrollView的父视图的子视图// 以"方式二"为例- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y &= 100)
CGRect rect = label.frame
rect.origin.y = 0
label.frame = rect
[self.view addSubview:label]
CGRect rect = label.frame
rect.origin.y = 100
label.frame = rect
[scrollView addSubview:label]
}}下拉头部图片放大// 下拉头部图片放大.gif上传失败,有空再传原理解析:通过-scrollViewDidScroll:代理方法跟踪contentOffset的的变化,根据contentOffset动态设置图片的缩放比例- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scale = 1 - (scrollView.contentOffset.y / 100);
scale = (scale &= 1) ? scale : 1;
scale = (scale &= 2) ? scale : 2;
imageView.transform = CGAffineTransformMakeScale(scale, scale);}图片无限轮播// 图片无限轮播.gif上传失败,有空再传原理解析:在已知图片数组有N个元素前提下,在UIScrollView中创建N+2个UIImageView,其中第1个至第N个图片为真实内容,第0个与第N个一样,第N+1个与第1个一样,通过-scrollViewDidScroll:代理方法跟踪contentOffset的的变化,在滑动到首尾两个图片处直接设置contentOffset到真实图片处即可
无相关信息IOS开发中滑动视图(UIScrollView, UITableView)的键盘遮挡处理_其他编程_网页教学基地
热门关键字:
         
IOS开发中滑动视图(UIScrollView, UITableView)的键盘遮挡处理
来源:网页教学基地 时间: 09:02:48&&浏览次数:0
一、键盘遮挡的场景分类
1. 开始页面录入。输入控件在屏幕的下部,键盘出现后遮挡输入控件
2. 切换焦点。新输入框被当前键盘部分遮挡,可点击
3. 切换输入法。
4. 屏幕旋转。屏幕高度发生变化,原未被遮挡输入框旋转后被遮挡
二、UI需上移的距离计算
计算控件底部与键盘终点顶部的距离,调整阀值自定。通常选择输入控件最近的UIViewController-&view作为同一参照
& & & & & & NSDictionary&*userInfo = [notification&userInfo];
& & & & & &&NSValue* aValue& & & & = [userInfo&objectForKey:UIKeyboardFrameEndUserInfoKey];
& & & & & &&CGRect&keyboardRect = [aValue&CGRectValue];
& & & & & & keyboardRect& & & & = [self.view&convertRect:keyboardRect&fromView:nil];
& & & & & &&CGFloat&keyboardTop = keyboardRect.origin.y;
& & & & & &&CGFloat&margin =&20;//文本框距键盘顶边最小距离
&& & & & & &
& & & & & &&CGRect&textFieldFrame = [self.viewconvertRect:_textFieldCall4Adjust.framefromView:&_textFieldCall4Adjust.superview];
& & & & & &&CGFloat&textFieldBottom = textFieldFrame.origin.y&+ textFieldFrame.size.
& & & & & &&CGFloat&delta = textFieldBottom& + margin - keyboardT
三、获取键盘的动画时间和时间函数
UIKeyboardAnimationCurveUserInfoKey
UIKeyboardAnimationDurationUserInfoKey
[UIView&setAnimationCurve:[[[notif&userInfo]&objectForKey:UIKeyboardAnimationCurveUserInfoKey]&intValue]];
& & & & [UIView&animateWithDuration:[[[notif&userInfo]&objectForKey:UIKeyboardAnimationDurationUserInfoKey]&floatValue]&animations:^(){
四、UI上移调整方式
1.调整最上层UIViewController-&view的frame. 最上层VC定义与转屏控制中的最上层VC相同,如UITableViewController,UINavigationControlller
2. 输入控件在UIScrollView或其子类,调整contentSizeH + contentOffsetY
3. 输入控件在UIScrollView或其子类,调整contentInsetB + contentOffsetY
注意:1. &单独调整contetntOffset以偏移视图的状态是不可靠的,页面手动滑动会弹回contentSize内。
& & & & & &2. 有时单独调整contentInset能够触发contentOffset的变化,如更改contentInsetT时视图上移, offset增加。
& & & & & & & &有文章说,单独设置contentInsetB会使contentOffset自动增加,然而Demo测试中未呈现该结果。
& &3. &键盘隐藏下移时,单独设置contentSize或contentInset, 恢复原contentSize或置contentInsetB为0即可
五、相关系统通知的触发
监听通知:
UIKeyboardWillShowNotification,&UIKeyboardDidShowNotification,&UIKeyboardWillHideNotification,&UIKeyboardDidHideNotification,&
UIKeyboardWillChangeFrameNotification,&UIKeyboardDidChangeFrameNotification,&UIApplicationWillChangeStatusBarOrientationNotification,&UIApplicationDidChangeStatusBarOrientationNotification
键盘使用系统默认全键盘之中文,英文,数字加符号
1. 新获得焦点
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
2.失去焦点
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
3.切换焦点
无(新旧键盘相同或等高)
无(新旧键盘相同或等高)
无(新旧键盘相同或等高)
4.更换英文至中文输入法,并输入中文字符使得键盘增高
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
(开启输入预测增加一个循环)
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
(切换时无,即时开闭输入预测代入一次循环)
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
5.更换中文(有多一行)至英文
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
(开启输入预测增加一个循环)
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
(切换时无,即时开闭输入预测代入一次循环)
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
(willShow的时候键盘为旧转向frame)
& didShow时键盘为新转向frame
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
(输入预测开启无关)
UIApplicationWillChangeStatusBarOrientationNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification
7.滑动隐藏键盘
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardWillChangeFrameNotification
UIKeyboardWillHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidHideNotification
结论:1. willShow, didShow, willHide,didHide覆盖了所有通知存在Case
& & & & & &2. willShow,willHide为最佳同步键盘动画时机。但为适配iOS67屏. 选择willShow, didShow, willHide三个键盘通知作键盘适应事件处理入口& & & & & &3. 切换焦点需另作处理获取事件处理时机& & & & & &4. 转屏时,willShow, didShow, willHide事件,如将动画合并提交,可能会不符合预想
& & & & & &5. 其它:人为触发endEditing 和&keyboardDismissMode设为UIScrollViewKeyboardDismissModeOnDrag的差异,人为触发时可以将retain的输入控件释放,&UIScrollViewKeyboardDismissModeOnDrag时,不便于获取置nil时机。
& & & & & & &iOS8,9&UIScrollViewKeyboardDismissModeOnDrag时,会触发两次keyboardXXXHide通知 & &
六、处理方案
1. 获取切换焦点时机
a. VC保持每次新获取焦点的输入控件。输入控件将要开始编辑时,根据新输入控件和旧有实例判断是否需调整键盘。
SuperClass:
- (BOOL)textFieldShouldBeginEditing:(UITextField&*)textField {
& &&//&在子类中应调用该方法
& &&if&(_textFieldFocusing&&&&_textFieldFocusing.isFirstResponder&&&&_textFieldFocusing&!= textField ) {
& & & &&//&处理切换输入焦点不会发出键盘SHOW通知的问题
& & & &&_textFieldFocusing&= textF
& & & & [self&p_handleKeyboardShow:nil];
& & }&else&{
& & & &&_textFieldFocusing&= textF
& &&return&YES;
- (BOOL)textFieldShouldBeginEditing:(UITextField&*)textField{
& &&if&(textField.tag&==&TagCouldNotEdit) {
& & & &&return&NO;
& &&return&[super&textFieldShouldBeginEditing:textField];;
& &b.切换焦点时,无法获取键盘frame,因此需保存最近一次键盘通知
& & notification = notification?:oldKeyBoardN
& &&if&(!notification) {
& &&oldKeyBoardNoti&=
2. 键盘处理事件串行化处理
& &每次动画提交同步至上次动画结束。
& & dispatch_semaphore_t&
& &&dispatch_queue_t&
& & semaphore&=&dispatch_semaphore_create(1);
& &&queue&=&dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,&0);
- (void) keyboardDidShow:(NSNotification&*) notif {
& &&if&(!self.textFieldFocusing&|| ![self.textFieldFocusingisFirstResponder]) {
& &&//&转屏支持
& &&// iphone7, ios4转屏时只走一遍hide-&show,而且willShow时键盘尺寸异常
& & [self&p_handleKeyboardShow:notif];
-(void)keyboardWillShow:(NSNotification&*)notification {
& &&if&(!self.textFieldFocusing&|| ![self.textFieldFocusingisFirstResponder]) {
& & [self&p_handleKeyboardShow:notification];
- (void) p_handleKeyboardShow:(NSNotification&*)notification {
& & notification = notification?:oldKeyBoardN
& &&if&(!notification) {
& &&oldKeyBoardNoti&=
& &&//&第一次键盘出现,且未获取tableView的contentSize时,获取tableview的size
& &&//&最多只执行一次
& &&if&(_idealTableViewSizeHeight&==&0) {
& & & &&_idealTableViewSizeHeight&=&self.tableView.contentSize.
& &&//&动画块
& &&void&(^changeContentOffset)() = ^(){
& & & & [UIView&setAnimationCurve:[[[notification&userInfo]&objectForKey:UIKeyboardAnimationCurveUserInfoKey]&intValue]];
& & & & [UIView&animateWithDuration:([[[notification&userInfo]&objectForKey:UIKeyboardAnimationDurationUserInfoKey]&floatValue]?:0.2)&animations:^(){
& & & & & &&// @IMPORTANT&每次block动画任务被执行时,重新计算移动距离
& & & & & &&//&需操作的环境参数
& & & & & &&NSDictionary&*userInfo = [notification&userInfo];
& & & & & &&NSValue* aValue& & & & = [userInfo&objectForKey:UIKeyboardFrameEndUserInfoKey];
& & & & & &&CGRect&keyboardRect = [aValue&CGRectValue];
& & & & & & keyboardRect& & & & = [self.view&convertRect:keyboardRect&fromView:nil];
& & & & & &&CGFloat&keyboardTop = keyboardRect.origin.y;
& & & & & &&CGFloat&margin =&20;//文本框距键盘顶边最小距离
&& & & & & &
& & & & & &&CGRect&textFieldFrame = [self.viewconvertRect:_textFieldFocusing.framefromView:_textFieldFocusing.superview];
& & & & & &&CGFloat&textFieldBottom = textFieldFrame.origin.y&+ textFieldFrame.size.
& & & & & &&CGFloat&delta = textFieldBottom& + margin - keyboardT
&& & & & & &
& & & & & &&CGFloat&contentSizeHeight =&MAX(&self.tableView.frame.size.height,&self.tableView.contentSize.height);
& & & & & & [self.tableViewsetContentSize:CGSizeMake(self.view.frame.size.width, contentSizeHeight + delta)];
& & & & & & [self.tableView&setContentOffset:&CGPointMake(0, delta +&oldTableViewOffset.y)&animated:NO];
& & & & }&completion:^(BOOL&finished) {
& & & & & &&dispatch_async(&queue, ^{
& & & & & & & &&dispatch_semaphore_signal(semaphore);
& & & & & & });
& & & & }];
& &&//&串行动画控制
& &&dispatch_async(queue, ^{
& & & &&dispatch_semaphore_wait(semaphore,&DISPATCH_TIME_FOREVER);
& & & &&//&需操作的环境参数
& & & &&NSDictionary&*userInfo = [notification&userInfo];
& & & &&NSValue* aValue& & & & = [userInfo&objectForKey:UIKeyboardFrameEndUserInfoKey];
& & & &&CGRect&keyboardRect = [aValue&CGRectValue];
& & & & keyboardRect& & & & = [self.view&convertRect:keyboardRect&fromView:nil];
& & & &&CGFloat&keyboardTop = keyboardRect.origin.y;
& & & &&CGFloat&margin =&20;//文本框距键盘顶边最小距离
& & & &&CGRect&textFieldFrame = [self.viewconvertRect:_textFieldFocusing.framefromView:_textFieldFocusing.superview];
& & & &&CGFloat&textFieldBottom = textFieldFrame.origin.y&+ textFieldFrame.size.
& & & &&if&( & !self.textFieldFocusing
& & & & & & || ![self.textFieldFocusing&isFirstResponder]
& & & & & & || textFieldBottom& + margin& &= keyboardTop
& & & & & & ||&fabs(textFieldBottom& + margin - keyboardTop) &&0.1) {
& & & & & &&//&不需要执行动画
& & & & & &&dispatch_semaphore_signal(semaphore);
& & & & }&else&{
& & & & & &&oldTableViewOffset&=&self.tableView.contentO
& & & & & &&dispatch_async(dispatch_get_main_queue(), ^{
& & & & & & & & changeContentOffset();
& & & & & & });
- (void) keyboardWillHide:(NSNotification&*) notif {
& &&//&通过drag隐藏时,会两次发出键盘隐藏通知,但对UI目前没有影响,不做原因查找
& &&//&动画块
& &&void&(^changeContentOffset)() = ^(){
& & & &&self.tableView.bounces&=&NO;
& & & & [UIView&setAnimationCurve:[[[notif&userInfo]&objectForKey:UIKeyboardAnimationCurveUserInfoKey]&intValue]];
& & & & [UIView&animateWithDuration:[[[notif&userInfo]&objectForKey:UIKeyboardAnimationDurationUserInfoKey]&floatValue]&animations:^(){
& & & & & & [self.tableViewsetContentSize:CGSizeMake(self.view.frame.size.width,_idealTableViewSizeHeight)];
& & & & }&completion:^(BOOL&finished) {
& & & & & &&self.tableView.bounces&=&YES;
& & & & & &&dispatch_async(queue, ^{
& & & & & & & &&dispatch_semaphore_signal(semaphore);
& & & & & & });
& & & & }] ;
& &&//&串行动画控制
& &&dispatch_async(&queue, ^{
& & & &&dispatch_semaphore_wait(semaphore,&DISPATCH_TIME_FOREVER);
& & & &&if&(self.tableView.contentSize.height&&_idealTableViewSizeHeight
& & & & & & ||&fabs(self.tableView.contentSize.height&-&_idealTableViewSizeHeight)&0.1) {
& & & & & &&//&不需要执行动画
& & & & & &&dispatch_semaphore_signal(semaphore);
& & & & }&else&{
& & & & & &&dispatch_async(dispatch_get_main_queue(), ^{
& & & & & & & & changeContentOffset();
& & & & & & });
七、自定义键盘
1.iputView
注意,当inputView的ViewController自定义,inputView的相关响应方法写在ViewController时,使用三方键盘可能发生异常
禁用三方键盘方法
- (BOOL)application:(UIApplication&*)application shouldAllowExtensionPointIdentifier:(NSString&*)extensionPointIdentifier
& &&if&([extensionPointIdentifier&isEqualToString:&UIApplicationKeyboardExtensionPointIdentifier])
& & & &&return&NO;
& &&return&YES;
2.UIInputViewController
UIResponder的inputViewController为readOnly,需要强制改为readWrite
@interface&TextField:UITextField
@property&(nonatomic,&readwrite,&strong)&UIInputViewController*inputViewC
@implementation&TextField
3.Application Extension
先新建Application,然后新建customed Keyboard Target, XCode将生成基础代码。开发时不能用Interface Builder, 必须全部代码手写

我要回帖

更多关于 contentinsetleft 的文章

 

随机推荐