inputView典型實現

#import "PostDetailInputCommentView.h"

@interface PostDetailInputCommentView() <UITextFieldDelegate>

@end

@implementation PostDetailInputCommentView{
    MMButton *recordAudioBtn;
    MMButton *selectImageBtn;
}

- (void)addSubviews{
    CGFloat btnWH = 32;
    CGFloat originY = 6;
    
    recordAudioBtn = [MMButton initImageBtnWithFrame:CGRectMake(10, originY, btnWH, btnWH) backgroundImageStr:nil superView:self];
    recordAudioBtn.backgroundColor = [UIColor redColor];
    [recordAudioBtn addBorderWithRadius:btnWH/2 borderColor:[UIColor blackColor]];
    
    selectImageBtn = [MMButton initWithFrame:CGRectMake(recordAudioBtn.frame.origin.x + recordAudioBtn.frame.size.width + 6, originY, btnWH, btnWH) superView:self];
    selectImageBtn.backgroundColor = [UIColor greenColor];
    [selectImageBtn addBorderWithRadius:btnWH/2 borderColor:[UIColor blackColor]];
    
    
    CGFloat TFOriginX = selectImageBtn.frame.origin.x + selectImageBtn.frame.size.width + 6;
    _replyContentTF = [MMTextField initWithFrame:CGRectMake(TFOriginX, originY, DEVICE_W - TFOriginX, btnWH) textColor:[UIColor redColor] textFont:FONTSIZE14 placeholder:@"請輸入評論" placeholderColor:[UIColor blueColor] superView:self];
    _replyContentTF.returnKeyType = UIReturnKeySend;
    _replyContentTF.delegate = self;
    [_replyContentTF addBorderWithRadius:0 borderColor:[UIColor grayColor]];
    
    [self addKeyboardNotificationCenter];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return NO;
}

#pragma mark keyboardaction
- (void)addKeyboardNotificationCenter {
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)removeKeyboardNotificationCenter {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardAction:(NSNotification *)notification complete:(void(^)(CGFloat hKeyB))complete {
    NSDictionary *userInfo = [notification userInfo];
    NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    NSValue *animationDurationObject =[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    
    NSUInteger animationCurve = 0;
    double animationDuration = 0.0f;
    CGRect keyboardEndRect = CGRectMake(0,0, 0, 0);
    [animationCurveObject getValue:&animationCurve];
    [animationDurationObject getValue:&animationDuration];
    [keyboardEndRectObject getValue:&keyboardEndRect];
    
    CGFloat hKeyB = 0;
    hKeyB = keyboardEndRect.size.height;
    
    [UIView beginAnimations:@"keyboardAction" context:NULL];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];
    complete(hKeyB);
    [UIView commitAnimations];
}

#pragma mark Responding to keyboard events

- (void)keyboardWillShow:(NSNotification *)notification {
        [self keyboardAction:notification complete:^(CGFloat hKeyB) {
            CGRect frame = self.frame;
            frame.origin.y = DEVICE_H - hKeyB - 40;
            self.frame = frame;
        }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
        [self keyboardAction:notification complete:^(CGFloat hKeyB) {
            //        CGRect frame = self.frame;
            //        frame.origin.y = hKeyB - 22;
            //        self.frame = frame;
        }];
}

- (void)dealloc {
    [self removeKeyboardNotificationCenter];
}
相關文章
相關標籤/搜索