#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. }