基础试图控制器ZJBaseViewController

备注:继承该视图控制器后,做屏幕适配工作时,只需要相对屏幕宽带即可,因为高度可以上下滑动

@interface ZJBaseViewController : UIViewController


@end

@interface UIView(FindFirstResponder)

- (UIView *)findFirstResponder;

@end


#import "ZJBaseViewController.h"


@interface ZJBaseViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>


@property(nonatomic,strong) UIScrollView * scroll;

@end


@implementation ZJBaseViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self addTapGestureRecognizer];

}

-(void)loadView{

    // 一定要解决手势冲突

    self.view = self.scroll;

}

-(UIScrollView *)scroll{

    if(_scroll == nil){

        _scroll = [[UIScrollView alloc] init];

        _scroll.backgroundColor = [UIColor whiteColor];

        _scroll.showsVerticalScrollIndicator = NO;

        _scroll.showsHorizontalScrollIndicator = NO;

        _scroll.delegate = self;

    }

    return _scroll;

}

#pragma mark - 

-(void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

    CGFloat height = 0.00;

    for(UIView * subview in self.view.subviews){

        if(!subview.hidden)

            height=height>=CGRectGetMaxY(subview.frame)?:CGRectGetMaxY(subview.frame);

    }

    self.scroll .contentSize = CGSizeMake(0, height);

    //self.view = self.scroll;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [self.view endEditing:YES];

}

#pragma mark - 添加点击手势

-(void)addTapGestureRecognizer{

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchSelfView)];

    tap.delegate = self;

    [self.view addGestureRecognizer:tap];

}

#pragma mark  点击view结束编辑

-(void)touchSelfView{

    [self.view endEditing:YES];

    NSLog(@"=========");

}

#pragma mark  解决手势冲突

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    if(touch.view.gestureRecognizers){

        return self.view == touch.view;

    }

    return YES;

}

#pragma mark - 监听输入框键盘动向

- (void)viewDidAppear:(BOOL)animated

{   [super viewDidAppear:animated];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}


- (void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}


-(void)viewWillDisappear:(BOOL)animated

{   [super viewWillDisappear:animated];

    

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWillShow:(NSNotification *)notification

{

    //    CGRect begin = [[[notification userInfo] objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];

    //    CGRect end = [[[notification userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];

    

    CGFloat keyBoardShowTime = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    CGRect  keyBoardFrame    = [[[notification userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];

    UIView *firstView = [[[UIApplication sharedApplication] keyWindow] findFirstResponder];

    CGFloat  firstViewMaxY = 0.0;

    if(firstView != nil){

        CGRect rect = [[UIApplication sharedApplication].keyWindow convertRect:firstView.frame fromView:firstView.superview];

        firstViewMaxY = rect.origin.y + rect.size.height;

    }

    CGFloat keyboardY = self.view.window.frame.size.height - keyBoardFrame.size.height;

    CGFloat offset = firstViewMaxY - keyboardY;

    //

    NSLog(@"%f , %f",self.view.window.frame.origin.y,self.view.frame.origin.y);

    if(offset>0){

        [self.view.layer removeAllAnimations];

        [UIView animateWithDuration:keyBoardShowTime animations:^{

            self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -offset);

            //self.view.transform =CGAffineTransformMakeTranslation(0, -offset);

        }];

        NSLog(@"%f",offset);

    }

    // 注:输入框第一次会有闪动情况出现,原因是本方法会走很多次

}


- (void)keyboardWillHide:(NSNotification *)notification

{

    CGFloat keyBoardHideTime = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    [UIView animateWithDuration:keyBoardHideTime animations:^{

        self.view.transform = CGAffineTransformMakeTranslation(0, 0);

        //        CGSize size = self.view.window.frame.size;

        //        self.view.window.frame = CGRectMake(0, 0, size.width, size.height);

    }];

    NSLog(@"AAAAAAAA %f , %f",self.view.window.frame.origin.y,self.view.frame.origin.y);

}

@end

/* ⚠️注⚠️:

 * 在 "AppDelegate" 里,添加如下方法

 - (void)applicationWillResignActive:(UIApplication *)application123 {

 [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

 }

 */


@implementation UIView(FindFirstResponder)

- (UIView *)findFirstResponder

{

    if (self.isFirstResponder) {

        return self;

    }

    for (UIView *subView in self.subviews) {

        UIView *responder = [subView findFirstResponder];

        if (responder) return responder;

    }

    return nil;

}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值