ios--解决键盘遮挡UITextField控件的问题(方式二)

本方法采用监听滑动手势来移动View:

1、定义UITextField控件及滑动所需的参数:

#import <UIKit/UIKit.h>

@interface AloneSetPrizeViewController : UIViewController
{
    ///滑动事件
    //触摸起始点坐标
    CGPoint startPoint;
    //是否正在移动View
    BOOL bViewMove;
    //View当前起始点Y坐标= upCount * 40.0f;
    int upCount;
}
@property(nonatomic,retain) NSMutableDictionary *dictionary1;
@property(nonatomic,retain) IBOutlet UITextField *txtOnePrize;
@property(nonatomic,retain) IBOutlet UITextField *txtTwoPrize;
@property(nonatomic,retain) IBOutlet UITextField *txtThreePrize;
@property(nonatomic,retain) IBOutlet UITextField *txtFourPrize;
@property(nonatomic,retain) IBOutlet UITextField *txtFivePrize;
@property(nonatomic,retain) IBOutlet UITextField *txtSixPrize;
-(IBAction)backOff:(id)sender;
-(IBAction)finishToSubmit:(id)sender;
@end

2、在viewDidLoad里面初始化参数:

    //初始化self.view的起始y轴坐标=upCount*40.0f;
    upCount = 0;

3、监听滑动事件:

//滑动开始事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
    bViewMove = false;
    UITouch *touch = [touches anyObject];
    CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点
    startPoint  = pointone;
}
//滑动移动事件
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //imgViewTop是滑动后最后接触的View
    CGPoint pointtwo = [touch locationInView:self.view];  //获得滑动后最后接触屏幕的点
    float moveY=pointtwo.y-startPoint.y;
    if(fabs(moveY)>40 && !bViewMove)
    {
        bViewMove = true;
        if (moveY>0) {
            if (upCount<0) {
                upCount ++;
            }
            startPoint.y += 40;
        }else
        {
            if (upCount>-8) {
                upCount --;
            }
            startPoint.y -= 40;
        }
        //判断两点间的距离
        NSLog(@"滑动开始%d",upCount);
        NSTimeInterval animationDuration=0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        float width = self.view.frame.size.width;
        float height = self.view.frame.size.height;
        //上移30个单位,按实际情况设置
        CGRect rect=CGRectMake(0.0f,upCount * 40.0f,width,height);
        self.view.frame=rect;
        [UIView commitAnimations];
        bViewMove = false;
        NSLog(@"滑动结束");
    }
}

xib文件的布局如下:



ios--解决键盘遮挡UITextField控件的问题(方式二),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。