Day10:UITextView和UIMotion

UITextView:文本视图控件,父类是UIScrollView,用于输入或者显示一行或多行内容,当输入或展示的内容超过textView.fame.size.width时自动换行,当内容超过textView.fame.size.height时自动出现滚动条
UITextField:用来输入一行内容

一.UITextView

  1. 常用设置
  2. 隐藏键盘方法
  3. 模仿微信输入界面

二.UIMotion

手机摇动时执行的方法("UIResponder"中的motionBegan:withEvent方法)

一.UITextView

  1. 常用设置
     UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 74, 200, 100)];
     [self.view addSubview:textView];
     //    背景色
     textView.backgroundColor = [UIColor yellowColor];
     //    本文颜色
     textView.textColor = [UIColor greenColor];
     //    字体
     textView.font=[UIFont systemFontOfSize:24];
     //    设置是否允许编辑,默认是YES
     textView.editable = YES;
     //    设置是否允许滚动,默认是YES
     textView.scrollEnabled = YES;
     textView.delegate = self;
     textView.text = @"this is textView";
    
  • 隐藏键盘方法
    1. 如果应用程序需要换行符时,可以设置单击换行时隐藏键盘 (缺点: textView一般需要换行功能的存在)

       //协议方法
       //是否允许修改文本内容,默认是YES
       -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
       {
       //    if([text isEqualToString:@"\n"]){
       //        [textView resignFirstResponder];
       //        return NO;
       //    }
           return YES;
       }
      
    2. 用于采用了导航控制器的视图

       //开始编辑时执行此协议方法(在导航栏上添加一个按钮用于隐藏键盘)
       -(void)textViewDidBeginEditing:(UITextView *)textView
       {
           UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(hideKey)];
           self.navigationItem.rightBarButtonItem=item;
       }
       //编辑结束时执行此协议方法(将导航栏上用于隐藏键盘的按钮项去掉)
       -(void)textViewDidEndEditing:(UITextView *)textView
       {
           self.navigationItem.rightBarButtonItem=nil;
       }
       -(void)hideKey
       {
           UITextView *textView=(UITextView *)[self.view viewWithTag:200];
           [textView resignFirstResponder];
       }
      
    3. 设定一个工具栏

         UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
       UIBarButtonItem *itemDone = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKey)];
       UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
       NSArray *array = [[NSArray alloc]initWithObjects:item1,itemDone, nil];
       toolBar.items = array;
       //将工具栏设为inputAccessoryView,自动在键盘上面显示,不需要将toolBar再次添加到self.view
       textView.inputAccessoryView = toolBar;
      
  • 模仿微信输入界面

    实现方法使视图随着键盘的弹出上移, 随着键盘隐藏收回
    当文本内容超过textView的height时,增加其height

    1. 去消息通知中心订阅一条消息(当键盘将要显示时UIKeyboardWillShowNotification)执行相应的方法
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
      去消息通知中心订阅一条消息(当键盘将要显示时UIKeyboardWillHideNotification)执行相应的方法
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    2. 当键盘将要显示时,将底部的view向上移到键盘的上面

       -(void)keyboardWillShow:(NSNotification*)notification
       {
           NSLog(@"keyboardWillShow");
           //通过消息中的信息可以获取键盘的frame对象
           NSValue *keyboardObj = [[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
       //    获取键盘的尺寸
           CGRect keyrect;
           [keyboardObj getValue:&keyrect];
           CGRect rect=_bottomView.frame;
           //将底部视图的y值设置总高度-键盘的高度-自己的高度
           rect.origin.y=HEIGHT-rect.size.height- keyrect.size.height;
           _bottomView.frame=rect;
       }
      

    当键盘将要隐藏时(将原来移到键盘上面的视图还原)

         -(void)keyboardWillHide:(NSNotification *)notification
         {
          CGRect rect=_bottomView.frame;
          rect.origin.y=HEIGHT-rect.size.height;
          _bottomView.frame=rect;
         }  
    

    当文本内容超过textView的height时,增加其height

             -(void)textViewDidChange:(UITextView *)textView
             {
              //获取内容大小
              CGSize textSize = textView.contentSize;
              //当输入内容尺寸大于textView的大小时,增加内容时
              if(textSize.height>textView.frame.size.height){
                  //修改底部视图的大小和坐标
                  CGRect vrect=_bottomView.frame;
                  //计算出差值
                  CGFloat difference=textSize.height-textView.frame.size.height;
                  //底部视图的高度增加,y坐标减少相应的差值
                  vrect.size.height+=difference;
                  vrect.origin.y-=difference;
                  _bottomView.frame=vrect;
                //        重设textView的height
                  textView.frame=CGRectMake(10, 10, WIDTH-70, textSize.height);
              }
              //当减少内容时,内容尺寸小于textView的大小时,需要调整textView和底部视图的大小,位置
              if(textView.frame.size.height>textSize.height&&textSize.height>30){
                  CGFloat difference=textView.frame.size.height-textSize.height;
                  CGRect vrect=_bottomView.frame;
                  vrect.size.height-=difference;
                  vrect.origin.y+=difference;
                  _bottomView.frame=vrect;
                  textView.frame=CGRectMake(10, 10, WIDTH-70, textSize.height);
                  NSLog(@"len=%d",textView.text.length);
                  if(textView.text.length==0){
                      textView.frame=CGRectMake(10, 10, WIDTH-70, 30);
                  }
              }
            }
    

UIMotion

  • 手机摇动时执行的方法("UIResponder"的方法)
      摇动设备时执行此方法播放声音, 窗口视图frame左右移动
      - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
      {
          NSLog(@"motionBegan");
          SystemSoundID soundID;
          AudioServicesCreateSystemSoundID((__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:@"音效" withExtension:@"caf"], &soundID);
          AudioServicesPlaySystemSound(soundID);
          //增加震动效果
          AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
          
          [UIView animateWithDuration:1 animations:^{
              CGRect rect=self.view.frame;
              rect.origin.x+=20;
              self.view.frame=rect;
          } completion:^(BOOL finished) {
              [UIView animateWithDuration:1 animations:^{
                  CGRect rect=self.view.frame;
                  rect.origin.x-=40;
                  self.view.frame=rect;
              } completion:^(BOOL finished) {
                  [UIView animateWithDuration:1 animations:^{
                      CGRect rect=self.view.frame;
                      rect.origin.x+=20;
                      self.view.frame=rect;
                  } completion:^(BOOL finished) {
                      
                  }];
              }];
          }];
      }
    

你可能感兴趣的:(Day10:UITextView和UIMotion)