滚屏文字

#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.
}
相关文章
相关标签/搜索