如何基于键盘的自定义UIView类上设置TextView的拉升动画 [英] How to set the Textview move up animation based on keyboard in the custom UIView class

查看:234
本文介绍了如何基于键盘的自定义UIView类上设置TextView的拉升动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的TextView UIView类。自定义视图类框架仅设置某一个视图控制器。我的想法是,当文本视图委托方法的基础上无法正常工作的键盘高度textviewdidbeginediting的TextView的动画。我使用以下code

I have Custom UIView class in one textview. Custom View class frame set only some one view controller. My thought is when text view delegate methods textviewdidbeginediting animation of textview based on the keyboard height not worked correctly. I am using following code

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect textFieldRect=
[self.window convertRect:textView.bounds fromView:textView];
CGRect viewRect =[self.window convertRect:self.bounds fromView:self];
//So now we have the bounds, we need to calculate the fraction between the top and bottom of the middle section for the text field's midline:
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
//Clamp this fraction so that the top section is all "0.0" and the bottom section is all "1.0".
if (heightFraction < 0.0)
{
    heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
    heightFraction = 1.0;
}

//    Now take this fraction and convert it into an amount to scroll by multiplying by the keyboard height for the current screen orientation. Notice the calls to floor so that we only scroll by whole pixel amounts.
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
//Finally, apply the animation. Note the use of setAnimationBeginsFromCurrentState: — this will allow a smooth transition to new text field if the user taps on another.

CGRect viewFrame = self.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self setFrame:viewFrame];

[UIView commitAnimations];

 }

-(void)textViewDidEndEditing:(UITextView *)textView{
CGRect viewFrame = self.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self setFrame:viewFrame];
[UIView commitAnimations];
 }

和还附上我的屏幕截图

如何解决这个问题。

推荐答案

您和您的移动使用TextView的动画,

You and move your textView using animation ,

这里是code,您可以根据您设定框架

here is the code , you can set frame according to you

-(void)makeAnimation
{
    yourView.alpha = 0;
    CGRect optionsFrame = yourView.frame;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.5];

    CGRect optionsFrame2  = {0,0,320,266};
    yourView.frame  = optionsFrame2;
    yourView.alpha=1;
    [UIView commitAnimations];
}

这篇关于如何基于键盘的自定义UIView类上设置TextView的拉升动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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