仿新浪新闻留言界面

#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 = [UIScreen mainScreen].bounds;

    self = [super initWithFrame:frame1];

    

    if (self)

    {

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

        [imageView setImage:[aImage applyBlurWithRadius:5 tintColor:[UIColor colorWithWhite:1 alpha:0.2] saturationDeltaFactor:1.8 maskImage:nil]];

        imageView.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap)];

        [imageView addGestureRecognizer:tap1];

        [self addSubview:imageView];

    

        [self addTarget:self action:@selector(controlPressed) forControlEvents:UIControlEventTouchUpInside];

        

        UIView *bkView = [[UIView alloc] init];

        CGFloat h1 = [OtherUtil scaleMessageHeight: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 = [OtherUtil scaleMessageRect:cancelRect];

        UIButton* cancelBt = [UIButton buttonWithType:UIButtonTypeCustom];

        cancelBt.frame = cancelRect;

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

        [cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        cancelBt.backgroundColor = [UIColor clearColor];

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

        [bkView addSubview:cancelBt];

        

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

        labRect = [OtherUtil scaleMessageRect:labRect];

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

        lab.text = @"写回复";

        lab.textAlignment = NSTextAlignmentCenter;

        lab.backgroundColor = [UIColor clearColor];

        [bkView addSubview:lab];

        

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

        sendRect = [OtherUtil scaleMessageRect:sendRect];

        _sendBt = [UIButton buttonWithType:UIButtonTypeCustom];

        _sendBt.enabled = NO;

        _sendBt.frame = sendRect;

        [_sendBt setTitle:@"发送" forState:UIControlStateNormal];

        [_sendBt setTitleColor:RGBCOLOR(143, 143, 143) forState:UIControlStateNormal];

        _sendBt.backgroundColor = [UIColor clearColor];

        [_sendBt addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];

        [bkView addSubview:_sendBt];

        

        

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

        textRect = [OtherUtil scaleMessageRect:textRect];

        self.textView = [[PlaceholderTextView alloc] initWithFrame:textRect];

        self.textView.delegate = self;

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

        [self.textView setMaxlen:kTextLen];

        _textView.layoutManager.allowsNonContiguousLayout = NO;

        _textView.returnKeyType = UIReturnKeyDefault;

        _textView.font = [UIFont systemFontOfSize:17];

        __weak typeof(self) weakSelf = self;

        _textView.textChangeBlock = ^(){

            [weakSelf setCountLab];

        };

        [bkView addSubview:self.textView];

        

        _countLab = [[UILabel alloc] init];

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

        _countLab.textAlignment = NSTextAlignmentRight;

        _countLab.font = [UIFont systemFontOfSize:14];

        CGFloat w = [OtherUtil scaleWidth:90];

        CGFloat h = [OtherUtil scaleHeight:20];

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

        [bkView addSubview:_countLab];

        [self setCountLab];


        [self addSubview:bkView];

        self.contentView = bkView;

        

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

        

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

        

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:) name:UITextViewTextDidChangeNotification object:nil];

        


    }

    return self;

}


#pragma mark        KeyBoard

-(void) keyboardWillHide:(NSNotification*) notification

{

    NSDictionary *userInfo = notification.userInfo;

    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    

    

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:[duration doubleValue]];

    [UIView setAnimationCurve:[curve intValue]];

    

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

    

    [UIView commitAnimations];

}


-(void) keyboardWillChangeFrame:(NSNotification*) notification

{

    NSDictionary *userInfo = notification.userInfo;

    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];


    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    


    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:[duration doubleValue]];

    [UIView setAnimationCurve:[curve intValue]];

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

    [UIView commitAnimations];

}


- (void)setCountLab

{

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

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

}


-(void)cancelClick

{

    [self controlPressed];

}


-(void)sendClick

{

    _textView.text = [_textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    if (_textView.text.length == 0)

    {

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

        return;

    }

    if ([_delegate respondsToSelector:@selector(rspSender:)])

    {

        _cardModel.rspContentStr = [_textView.text copy];

        [_delegate rspSender:_cardModel];

    }

    [self controlPressed];

}


- (void)__show

{

    UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];

    [mainWindow addSubview:self];

    self.alpha = 1.0f;

    [_textView becomeFirstResponder];

}


- (void)__hide

{

    [UIView animateWithDuration:0.25f

                     animations:^{

                         self.alpha = 0.0f;

                     }

                     completion:^(BOOL finished) {

                         [self removeFromSuperview];

                     }];

}


- (void)show

{

    [self __show];

}

- (void)imageTap

{

    [self controlPressed];

}

- (void)controlPressed

{

    [_textView resignFirstResponder];

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

        [[NSNotificationCenter defaultCenter] 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)

    {

        [_sendBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        _sendBt.enabled = YES;

    }

    else

    {

        [_sendBt setTitleColor:RGBCOLOR(143, 143, 143) forState:UIControlStateNormal];

        _sendBt.enabled = NO;

    }

    [OtherUtil limitInputText:textView ByMaxLength:kTextLen];

}


@end


以上代码就是留言的代码实现,主要键盘弹起动画,其他主要文本输入框的控制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值