Skip to content

Instantly share code, notes, and snippets.

@taktamur
Last active August 29, 2015 14:17
Show Gist options
  • Save taktamur/68141ee48aa991dff820 to your computer and use it in GitHub Desktop.
Save taktamur/68141ee48aa991dff820 to your computer and use it in GitHub Desktop.
キーボードにくっついて、縦幅を伸ばせるTextView
@taktamur
Copy link
Author

//
//  ViewController.m
//  openTextbox
//
//  Created by tak on 2015/03/24.
//  Copyright (c) 2015年 taktamur. All rights reserved.
//

#import "ViewController.h"
#import "OpenedTextBox.h"

@interface ViewController ()
@property(nonatomic)OpenedTextBox *openedTextBox;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UINib *nib = [UINib nibWithNibName:@"openedTextBox" bundle:nil];
    self.openedTextBox = [nib instantiateWithOwner:nil options:nil].firstObject;

    // 位置合わせ
    CGRect rect = CGRectMake(0,
                             CGRectGetMaxY(self.view.frame) -46,
                             CGRectGetWidth(self.view.bounds),
                             46);

    self.openedTextBox.frame = rect;

    [self.view addSubview:self.openedTextBox];


    // ドラッグしたときのジェスチャーを追加
    UIPanGestureRecognizer *pullGesture = [[UIPanGestureRecognizer alloc]init];
    [pullGesture addTarget:self action:@selector(didSwipeBox:)];
    [self.openedTextBox.button addGestureRecognizer:pullGesture];

    UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackground:)];
    [self.view addGestureRecognizer:tapBackground];
    NSLog(@"bottom=%@",@(self.bottomLayoutGuide.length));

}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    //キーボード表示/非表示のObserver登録
    NSNotificationCenter *noticication = [NSNotificationCenter defaultCenter];
    [noticication addObserver:self
                     selector:@selector(keyboardWillShow:)
                         name:UIKeyboardWillShowNotification
                       object:nil];
    [noticication addObserver:self
                     selector:@selector(keyboardWillHide:)
                         name:UIKeyboardWillHideNotification
                       object:nil];

    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -self.bottomLayoutGuide.length);
    self.openedTextBox.transform = transform;

//    NSLog(@"bottom=%@",@(self.bottomLayoutGuide.length));
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSNotificationCenter *noticication = [NSNotificationCenter defaultCenter];
    [noticication removeObserver:self
                            name:UIKeyboardWillShowNotification
                          object:nil];
    [noticication removeObserver:self
                            name:UIKeyboardWillHideNotification
                          object:nil];
}

#pragma mark - 背景タップでキーボードを閉じる
-(void)didTapBackground:(UITapGestureRecognizer *)gesture
{
    [self.view endEditing:YES];
}

#pragma mark - 下の枠のドラッグ操作
-(void)didSwipeBox:(UIPanGestureRecognizer *)gesture
{
    // 移動量を取得
    CGPoint point = [gesture translationInView:self.view];

    // 立て幅を調整
    // 上に行った=マイナス方向
    CGFloat oldBoxHeight = CGRectGetHeight(self.openedTextBox.bounds);
    CGFloat newBoxHeight = oldBoxHeight-point.y;
    newBoxHeight = MAX(46, newBoxHeight);
    newBoxHeight = MIN(300,newBoxHeight);
    UIEdgeInsets newRectInset = UIEdgeInsetsMake(-(newBoxHeight-oldBoxHeight),
                                                 0,
                                                 0,
                                                 0);
    self.openedTextBox.frame = UIEdgeInsetsInsetRect(self.openedTextBox.frame, newRectInset);

    // ドラッグで移動した距離を初期化する
    [gesture setTranslation:CGPointZero inView:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - キーボード表示非表示
-(void) keyboardWillShow:(NSNotification *) notification
{

    // キーボードのサイズを取得
    CGRect rect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    // キーボード表示アニメーションのdurationを取得
    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // キーボード表示と同じdurationのアニメーションでViewを移動させる
    [UIView animateWithDuration:duration animations:^{
        CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -rect.size.height);
        self.openedTextBox.transform = transform;
    } completion:NULL];
}
-(void) keyboardWillHide:(NSNotification *) notification
{
    // キーボード表示アニメーションのduration
    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // Viewを元に戻す
    [UIView animateWithDuration:duration animations:^{
        self.openedTextBox.transform = CGAffineTransformMakeTranslation(0, -self.bottomLayoutGuide.length);
    } completion:NULL];
}
@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment