iOS应用开发实战(5-6)-App"猜城市"

这是首次在课上遇到完整的app,对这个app的设计步骤准备作为以后自己做project时候的参考。

课题

猜城市.png
  • step1:需求分析
需求分析.png
  • step2:分析设计


    分析设计.png
  • step3:程序设计

程序设计.png
  • step4:代码结构
代码结构.png

键盘的显示问题

我写了一个类,不知道思路对不对

  • .h文件
    // keyboardEvents.h
    #import <UIKit/UIKit.h>
    @interface keyboardEvents : UIViewController<UITextFieldDelegate>
    -(void)moveView:(float)move;
    @end

  • .m文件
    // keyboardEvents.m
    #import "keyboardEvents.h"
    @interface keyboardEvents ()
    @end
    int textFieldY;
    @implementation keyboardEvents
    #pragma mark - Keyboard Events
    -(void)textFieldDidBeginEditing:(UITextField *)textField{
    textFieldY = textField.frame.origin.y;
    }

    -(void)textFieldDidEndEditing:(UITextField *)textField{
        int offset = 0;
        [self moveView:offset];
        [UIView commitAnimations];
    }
    
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
       NSTimeInterval animationDuration = 0.30f;
      [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
      [UIView setAnimationDuration:animationDuration];
      CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
      self.view.frame = rect;
      [UIView commitAnimations];
      [textField resignFirstResponder];
      return YES;
    }
    
    - (void) registerForKeyboardNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    }
    
    - (void) KeyboardWillShowNotification:(NSNotification *) notif
    {
        NSDictionary *info = [notif userInfo];
        NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGFloat keyboardY = [value CGRectValue].origin.y;
    
        if (textFieldY > keyboardY -60 ){
            int offset =  ( keyboardY - 60 ) - textFieldY ;
            [self moveView:offset];
            [UIView commitAnimations];
        }
    }
    
    -(void)moveView:(float)move{
        NSTimeInterval animationDuration = 0.30f;
        CGRect frame = self.view.frame;
        frame.origin.y = 0;
        frame.origin.y +=move;
        self.view.frame = frame;
        [UIView beginAnimations:@"ResizeView" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];
    }
    
    #pragma mark - View Lifecycle
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self registerForKeyboardNotifications];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        [[NSNotificationCenter defaultCenter]removeObserver:self];
    }
    @end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容