仿新浪新闻留言界面

来源:互联网 发布:串口调试软件下载 编辑:程序博客网 时间:2024/05/15 23:46

#define kTextLen 200


@interface xxxView()<UITextViewDelegate>

@property (nonatomic,strong) UIView *contentView;

@property (nonatomic,strong) PlaceholderTextView *textView;


@property (nonatomic,strong) UIButton* cancelBt;

@property (nonatomic,strong) UIButton* sendBt;

@property (nonatomic,strong) UILabel *countLab;

@end


@implementation xxxView

-(instancetype)initWithFrame:(CGRect)frame image:(UIImage*)aImage

{

    CGRect frame1 = [UIScreenmainScreen].bounds;

   self = [superinitWithFrame:frame1];

    

   if (self)

    {

       UIImageView* imageView =[[UIImageViewalloc] initWithFrame:frame1];

        [imageView setImage:[aImageapplyBlurWithRadius:5tintColor:[UIColorcolorWithWhite:1alpha:0.2]saturationDeltaFactor:1.8maskImage:nil]];

        imageView.userInteractionEnabled =YES;

        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(imageTap)];

        [imageViewaddGestureRecognizer:tap1];

        [selfaddSubview:imageView];

    

        [selfaddTarget:selfaction:@selector(controlPressed)forControlEvents:UIControlEventTouchUpInside];

        

       UIView *bkView = [[UIViewalloc] init];

       CGFloat h1 = [OtherUtilscaleMessageHeight:130];//相对坐标适配

        bkView.frame =CGRectMake(0, frame1.size.height - h1,kScreenWidth, h1);

        bkView.backgroundColor =RGBCOLOR(235,235, 235);

        

       CGRect cancelRect = CGRectMake(8, 13, 46, 21);

        cancelRect = [OtherUtilscaleMessageRect:cancelRect];

        UIButton* cancelBt = [UIButtonbuttonWithType:UIButtonTypeCustom];

        cancelBt.frame = cancelRect;

        [cancelBt setTitle:@"取消"forState:UIControlStateNormal];

        [cancelBt setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        cancelBt.backgroundColor = [UIColorclearColor];

        [cancelBt addTarget:selfaction:@selector(cancelClick)forControlEvents:UIControlEventTouchUpInside];

        [bkViewaddSubview:cancelBt];

        

       CGRect labRect = CGRectMake(131,13, 58, 21);

        labRect = [OtherUtilscaleMessageRect:labRect];

       UILabel* lab = [[UILabelalloc] initWithFrame:labRect];

        lab.text =@"写回复";

        lab.textAlignment =NSTextAlignmentCenter;

        lab.backgroundColor = [UIColorclearColor];

        [bkViewaddSubview:lab];

        

       CGRect sendRect = CGRectMake(265, 13, 46, 21);

        sendRect = [OtherUtilscaleMessageRect:sendRect];

        _sendBt = [UIButtonbuttonWithType:UIButtonTypeCustom];

       _sendBt.enabled =NO;

       _sendBt.frame = sendRect;

        [_sendBtsetTitle:@"发送"forState:UIControlStateNormal];

        [_sendBtsetTitleColor:RGBCOLOR(143,143, 143) forState:UIControlStateNormal];

        _sendBt.backgroundColor = [UIColorclearColor];

        [_sendBtaddTarget:selfaction:@selector(sendClick)forControlEvents:UIControlEventTouchUpInside];

        [bkViewaddSubview:_sendBt];

        

        

       CGRect textRect = CGRectMake(8, 42, 304, 68);

        textRect = [OtherUtilscaleMessageRect:textRect];

       self.textView = [[PlaceholderTextViewalloc] initWithFrame:textRect];

       self.textView.delegate =self;

        [self.textViewsetPlaceholder:@"回复留言…"];

        [self.textViewsetMaxlen:kTextLen];

        _textView.layoutManager.allowsNonContiguousLayout =NO;

        _textView.returnKeyType =UIReturnKeyDefault;

       _textView.font = [UIFontsystemFontOfSize:17];

       __weak typeof(self) weakSelf =self;

        _textView.textChangeBlock = ^(){

            [weakSelfsetCountLab];

        };

        [bkViewaddSubview:self.textView];

        

       _countLab = [[UILabelalloc] init];

       _countLab.textColor =RGBCOLOR(153,153, 153);

        _countLab.textAlignment =NSTextAlignmentRight;

       _countLab.font = [UIFontsystemFontOfSize:14];

       CGFloat w = [OtherUtilscaleWidth:90];

       CGFloat h = [OtherUtilscaleHeight:20];

       _countLab.frame =CGRectMake(kScreenWidth-100,_textView.bottom, w, h);

        [bkViewaddSubview:_countLab];

        [selfsetCountLab];


        [selfaddSubview:bkView];

       self.contentView = bkView;

        

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillChangeFrameNotificationobject:nil];

        

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

        

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textFieldEditChanged:)name:UITextViewTextDidChangeNotificationobject:nil];

        


    }

    return self;

}


#pragma mark        KeyBoard

-(void) keyboardWillHide:(NSNotification*) notification

{

   NSDictionary *userInfo = notification.userInfo;

    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];

    

    NSNumber *duration = [notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [notification.userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey];

    

    

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIViewsetAnimationBeginsFromCurrentState:YES];

    [UIViewsetAnimationDuration:[duration doubleValue]];

    [UIViewsetAnimationCurve:[curve intValue]];

    

   self.contentView.bottom = keyboardF.origin.y;

    

    [UIViewcommitAnimations];

}


-(void) keyboardWillChangeFrame:(NSNotification*) notification

{

   NSDictionary *userInfo = notification.userInfo;

    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];


    NSNumber *duration = [notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [notification.userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey];

    


    [UIViewbeginAnimations:nilcontext:NULL];

    [UIViewsetAnimationBeginsFromCurrentState:YES];

    [UIViewsetAnimationDuration:[duration doubleValue]];

    [UIViewsetAnimationCurve:[curve intValue]];

   self.contentView.bottom = keyboardF.origin.y;

    [UIViewcommitAnimations];

}


- (void)setCountLab

{

   int strLen = (int)_textView.text.length;

   _countLab.text = [NSStringstringWithFormat:@"%d/%d",strLen,kTextLen];

}


-(void)cancelClick

{

    [selfcontrolPressed];

}


-(void)sendClick

{

    _textView.text = [_textView.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];

   if (_textView.text.length ==0)

    {

        [AlertUtilshowHudViewWithMessage:@"请输入回复..."hideAfterSecond:1];

       return;

    }

   if ([_delegaterespondsToSelector:@selector(rspSender:)])

    {

        _cardModel.rspContentStr = [_textView.textcopy];

        [_delegaterspSender:_cardModel];

    }

    [selfcontrolPressed];

}


- (void)__show

{

   UIWindow *mainWindow = [[UIApplicationsharedApplication] keyWindow];

    [mainWindowaddSubview:self];

   self.alpha =1.0f;

    [_textViewbecomeFirstResponder];

}


- (void)__hide

{

    [UIViewanimateWithDuration:0.25f

                    animations:^{

                        self.alpha =0.0f;

                     }

                    completion:^(BOOL finished) {

                         [selfremoveFromSuperview];

                     }];

}


- (void)show

{

    [self__show];

}

- (void)imageTap

{

    [selfcontrolPressed];

}

- (void)controlPressed

{

    [_textViewresignFirstResponder];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

        [[NSNotificationCenterdefaultCenter] removeObserver:self];

        [self__hide];

    });

    

}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    return YES;

}


-(void)textFieldEditChanged:(NSNotification *)obj

{

   UITextView *textView = obj.object;

   if (textView.text.length)

    {

        [_sendBtsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

       _sendBt.enabled =YES;

    }

   else

    {

        [_sendBtsetTitleColor:RGBCOLOR(143,143, 143) forState:UIControlStateNormal];

       _sendBt.enabled =NO;

    }

    [OtherUtillimitInputText:textView ByMaxLength:kTextLen];

}


@end


以上代码就是留言的代码实现,主要键盘弹起动画,其他主要文本输入框的控制
0 0