滚屏文字

#import <UIKit/UIKit.h>

@interface VCRoot : UIViewController<UITextFieldDelegate>
//滚动视图
@property (retain,nonatomic) UIScrollView* mSV ;
//左侧发送按钮
@property (retain,nonatomic) UIButton* btnLeft ;
//右侧发送按钮
@property (retain,nonatomic) UIButton* btnRight;
//文本输入框
@property (retain,nonatomic) UITextField* mTextInput ;
@end

#import "VCRoot.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //系统视图动画制作聊天滚屏
    
//*****************************************************************
    //创建滚动视图
    _mSV = [[UIScrollView alloc] init] ;
    _mSV.frame = CGRectMake(0, 20, 320, 400);
    _mSV.contentSize = CGSizeMake(320, 400) ;
    _mSV.backgroundColor = [UIColor yellowColor] ;
    //随意停止,默认为NO
    _mSV.pagingEnabled = NO ;
    [self.view addSubview:_mSV] ;
    
//*****************************************************************
    //创建Button
    _btnLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    _btnLeft.frame = CGRectMake(10, 420, 80, 40) ;
    [_btnLeft setTitle:@"发送一" forState:UIControlStateNormal] ;
    [_btnLeft addTarget:self action:@selector(pressSendLeft) forControlEvents:UIControlEventTouchUpInside] ;
    //触发事件
    [self.view addSubview:_btnLeft] ;
    
//*****************************************************************
    //输入对话框对象
    _mTextInput = [[UITextField alloc] initWithFrame:CGRectMake(80, 420, 160, 40)] ;
    _mTextInput.borderStyle = UITextBorderStyleRoundedRect ;
    _mTextInput.delegate = self ;
    [self.view addSubview:_mTextInput] ;
}

//
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:nil] ;
    [UIView setAnimationDuration:0.3] ;
    _mTextInput.frame = CGRectMake(80, 180, 160, 40);
    _btnLeft.frame = CGRectMake(10, 180, 80, 40);
    [UIView commitAnimations];
    
}
//
-(void) textFieldDidEndEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:nil] ;
    [UIView setAnimationDuration:0.3] ;
    _mTextInput.frame = CGRectMake(80, 420, 160, 40) ;
    [UIView commitAnimations];
}
//
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [_mTextInput resignFirstResponder];
    [UIView beginAnimations:nil context:nil] ;
    [UIView setAnimationDuration:0.3] ;
    _mTextInput.frame = CGRectMake(80, 420, 160, 40);
    _btnLeft.frame = CGRectMake(10, 420, 80, 40);
    [UIView commitAnimations];
    return YES ;
}
//
-(void) pressSendLeft
{
    static int count = 0 ;
    UILabel* labelShow = [[UILabel alloc] init] ;
    labelShow.frame = CGRectMake(20, count*50+100, 120, 40);
    labelShow.text = _mTextInput.text;
    if ([_mTextInput.text isEqualToString:@""] == YES){
        return;
    }
    [_mSV addSubview:labelShow] ;

    if (labelShow.frame.origin.y >= 400)
    {
        _mSV.contentSize = CGSizeMake(self.view.frame.size.width, _mSV.contentSize.height+50);//高度持续增加
        [UIView beginAnimations:nil context:nil] ;
        [UIView setAnimationDuration:1] ;
        _mSV.contentOffset = CGPointMake(0, _mSV.contentSize.height-400) ;//展示的scrollview位置
        [UIView commitAnimations];
    }
    
    count++ ;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


转载于:https://my.oschina.net/u/2319073/blog/597132

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值