zoukankan      html  css  js  c++  java
  • IOS UI 笔记整理回顾

    注意手势会冒泡上抛,一个view没有实现的手势,如果父类view有实现,父视图就处理,如果不想让父视图处理,就把本视图添加到底层window上

     

     

    setMasksToBounds:YES

     

    imageView.contentMode=UIViewContentModeScaleAspectFit;

     

     

    imageview.animationImages=图片数组

     

     

    将某个子视图移到最上层

        [self.window bringSubviewToFront:redView];

        

    将某个子视图移到最底层

        [self.window sendSubviewToBack:blueView];

     

    是否裁剪子视图

        View.clipsToBounds=YES;

     

    当父视图的尺寸发生变化时,子视图以怎样的方式发生变化,子视图的宽度和高度灵活的随着父视图变化

        topView.autoresizingMask=UIViewAutoresizingFlexibleWidth|

    UIViewAutoresizingFlexibleHeight;

        

    UIImageView *imgview; 图片动画,区别于UIview动画(属性动画和翻转动画)

        [imgview startAnimating];

     

     

    连续缩放

        imageView.transform=CGAffineTransformScale(imageView.transform, 1.2, 1.2);

     

     

    方法可以设置动画的延迟时间、动画效果、动画结束后执行的代码

        [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear 

    animations:^{

            

        } completion:^(BOOL finished) {

            

        }];

     

     

     

    nil 和null 的区别

     

     

     

     

    UInavigationController.view.appearance

     

     

     

     

    UIView翻转 AnimationTransition

     

    1.

    设置翻转动画的样式,对象

        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];

     

    设置翻转动画,第一个参数:操作的视图对象,持续时间,翻转样式选项,动画代码(桌面翻转)

        [UIView transitionWithView:self.window duration:2 options:UIViewAnimationOptionTransitionFlipFromTop 

    animations:^{

            [self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:2];

       } completion:nil];

     

     

     

     

    CALayer动画

    实例化一个动画对象,指定动画内容

        CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"position"];

        设置动画的起始位置

        animation.fromValue=[NSValue valueWithCGPoint:imageView.center];

        设置动画的结束位置

        animation.toValue=[NSValue valueWithCGPoint:CGPointMake(300, 600)];

        动画的持续时间

        animation.duration=2;

        设置是否将动画还原为初始状态

        animation.autoreverses=YES;

    动画重复次数

        animation.repeatCount=1;

        动画是添加在layer上的,动画结束后会恢复原来的样式,如果不想还原,需要设置layer的相应属性

        imageView.layer.position=CGPointMake(300, 600);

        给layer添加动画

        [imageView.layer addAnimation:animation forKey:@"position"];

     

     

    UITextField 

    leftView设置textField内左侧的视图 leftViewMode设置左视图一直显示(UITextFieldViewModeAlways)

     

    clearButtonMode设置清除文本的按钮的显示模式

     

    clearsOnBeginEditing设置编辑一开始清空原始文本内容

     

    adjustsFontSizeToFitWidth是否自动调整字号

     

     

    //可以设置验证

    设置是否允许结束编辑,默认是YES,可以设置最少输入的字数

    -(BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    设置用户输入字符个数必须大于6,否则键盘不会隐藏

        if (textField.text.length<6) {

            return NO;

        }

        return YES;

    }

     

    是否允许响应return键,默认YES,并在单击return后实现隐藏键盘,需要实现UITextFieldDelegate协议方法

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{

        [textField resignFirstResponder];//不能少

        return YES;

    }

     

     

    UIViewController

     

    数据共享,在代理类中添加共享的成员变量

    AppDelegate *delegate=[UIApplication sharedApplication].delegate;

    单例类:从[Application sharedApplication] 获取应用程序对象,是一个单例类

        获取应用程序的代理对象,(应用程序对象只有一个,代理对象也只有一个,用来做全局变量,共享数据)

     

     

    动画切换

    sub.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

        [self presentViewController:sub animated:YES completion:nil];

     

    是否返回前一个视图控制器对象root

       [self dismissViewControllerAnimated:YES completion:nil];

    获取上一次的视图控制器对象

    lastViewController *last=(lastViewController *)[self presentingViewController];

     

     

     

    隐藏状态栏

    -(BOOL)prefersStatusBarHidden{

        return YES;

    }

     

     

    UINavigationController 

    切换到根视图

        [self.navigationController popToRootViewControllerAnimated:YES];

     

      设置是否裁剪背景图片(背景图片不在状态栏显示)

        self.navigationController.navigationBar.clipsToBounds=YES;

     

     

    所有的视图控制器共用一个navigationBar

        但是每个视图控制器都需要设置自己的navigationItem

     

     

    UIBarButtonItem

      UIBarButtonItem *item4=[[UIBarButtonItem alloc]initWithCustomView:btn2];

    UIBarButtonItem *itemSystem=[[UIBarButtonItem 

                             alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera 

    target:self action:@selector(back:)];

     

     

    设置空白项,宽度是根据布局灵活变化的

      UIBarButtonItem *item2=[[UIBarButtonItem 

    alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 

    target:self action:@selector(back:)];

     

     

     

     

     

     

     

    UIWebView加载网络资源

    1、获取资源的url

        NSString *path=[[NSBundle mainBundle]pathForResource:@"about" ofType:@"html"];

       NSURL *url=[NSURL fileURLWithPath:path];

        2、将url封装到一个NSURLRequest请求对象中

        NSURLRequest *request=[NSURLRequest requestWithURL:url];

       

        UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 375,667)];

        [self.view addSubview:webView];

        3、将请求到的资源在webveiw上显示

        [webView loadRequest:request];    

        

     

     

     

    小组件

    [XXX addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];

    UISwitch:开关组件

     

    UIActivityIndicatorView简单的指示器,没有监听功能(就是那个下载时的菊花)

       XXX.hidesWhenStopped=NO;

     

    UISegmentedControl分段选取器

       NSArray *titles=@[@"",@"绿",@""];

        UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:titles];

    获取用户选择的segment的下标(从0开始)

        NSInteger index=sender.selectedSegmentIndex;

       NSString *title=[sender titleForSegmentAtIndex:index];

     

    定时器

        _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(XXX) userInfo:nil 

    repeats:YES];

    停止定时器

            [_timer invalidate];

            _timer=nil;

     

      xib可以快速创建界面,代码比较慢

      代码适合git等代码管理,xib不适合

      代码适合维护升级,xib不适合

     

      1)如果xib资源名和类文件的名称相同,自动加载同名的资源

     

      2)加载资源包中的MyView文件,取出其中的内容,为一个数组,

        第一个参数是xib文件名,第二个参数是相应的处理事件的对象,第三个参数一般为nil

        owner属于谁,谁去监听

       NSArray *views= [[NSBundle mainBundle]loadNibNamed:@"MyView" owner:self options:nil];

     

     NSUserDefaults:单例类

       NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];

        将数据同步写入文件里面

        [userDefaults synchronize];

     

     

     

     

        

    沙盒目录

        /*获取Document目录,返回的是数组   

            第一个参数是获取目录的类型

            第二个参数是获取IOS应用程序的一个参数

            第三个参数是否展开

         */

        NSString *docPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

     

        获取Library目录

        NSString *libraryPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];

           

        获取Cache 目录,Library目录下的Cache目录

        NSString *cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

          

        获取应用程序的tmp目录

        NSString *tmpPath=NSTemporaryDirectory();

     

     

    ===========================================================================================================

    app目录

        NSString *appPath=[NSBundle mainBundle].bundlePath;

           

        获取应用程序的数据家目录

        NSString * homePath=NSHomeDirectory();

        绝对路径,同上

        NSString *path=[homePath stringByAppendingPathComponent:@"Documents/app.plist"];

     

     

     

     

     

     

     使用图片原始色彩样式效果,阻止使用系统颜色渲染,防止出现系统颜色遮盖原始色彩

        image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

     

     

    设置tabBarItem徽章

        view2.tabBarItem.badgeValue=@"2";

     

     

    遍历中的一些问题

      声明为父类,指向任意类型的子类

            for (UIViewController * vc in controllers) {

                if ([vc.title isEqualToString:title]) {

                    tabBarController.selectedViewController=vc;

                    break;

                }

            }

     

    指定为父类的指针,只看对象不看指针

     

    允许与用户交互,默认是NO,子视图也不能与用户交互

        UIView.userInteractionEnabled=YES;

     

     

     

     

     

     

     

    UIResponder是所有的响应者对象的父类

     

    UIView UIWindow AppDelegate UIViewController 都是UIResponder的子类

     

     

    设置是否允许多点触摸

        view.multipleTouchEnabled=YES;

     

    当用户在屏幕上移动,还没有离开屏幕时

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

     

    [touches count]获取触摸的手指数

     

     

    将触摸开始时手指在当前视图的坐标保存下来(用于计算偏移值之类)

        CGPoint _originPoint=[touch locationInView:self];

     

     

    =============================================================================

    处理移动手势

    获取手势在某个视图上的坐标偏移值

            CGPoint pt=[sender translationInView:sender.view];

      重新清空手势的偏移值(偏移值会累积)CGPointZero,相当于CGPointMake(0,0)

            [sender setTranslation:CGPointZero inView:sender.view];

     

     

     获取滚动视图的内容偏移值

        CGPoint pt=scrollView.contentOffset;

     

     

     

    处理旋转手势

    设置view的旋转角度(通过sender.rotation属性获取)

    view.transform=CGAffineTransformRotate(view.transform, sender.rotation);

            清空

            sender.rotation=0

     

    允许与用户交互(可以响应用户事件)

        imageView.userInteractionEnabled=YES;

    • UITapGestureRecognizer敲击手势(单击和双击)
    •  UIPanGestureRecognizer(拖动手势)
    •  UIPinchGestureRecognizer(缩放手势)
    •  UISwipeGestureRecognizer(擦碰手势)
    •   UIPinchGestureRecognizer(张合手势)
    •  UIRotationGestureRecognizer(旋转手势)
    •  UILongPressGestureRecognizer(长按手势)

    一个单向关系两个手势识别器

    [rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];

     

    响应手势的协议方法

    if (sender.state==UIGestureRecognizerStateBegan||sender.state==UIGestureRecognizerStateChanged

     

     

    设置是否实现同时识别多个手势

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

        判断两个手势是否对同一个视图的操作,如果是,可以同时识别

        if (gestureRecognizer.view==otherGestureRecognizer.view) {

            return YES;

        }return NO;

    }

     

     

     

     

     

     

     

    滚动视图必须设置的项

      scrollView.contentSize=size;

       设置是否有回弹效果

        scrollView.bounces=NO;

     

     

        设置是否允许分页

        scrollView.pagingEnabled=NO;

        

        设置内容视图的最大、最小的缩放比例

        scrollView.minimumZoomScale=0.5;

        scrollView.maximumZoomScale=2;

    如果设置pageEnable 为yes,一定会调用此方法(减速效果)

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

     

    UIPageControl *_pageControl;页码控制器

    动视图当前视图、页码指示器保持一致。

     

     

     

     

     

     

     

     

     

     

     

    实时监听

    在消息通知中心订阅消息(键盘将要显示),当消息来到时,self调用@selector中的方法响应

        

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:) 

    name:UIKeyboardWillShowNotification object:nil];

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillHide:) 

    name:UIKeyboardWillHideNotification object:nil];

        

     

    将一个变量转换成一个id类型  @{}  @[] @()

     

     

     

     

    警告框

    自定义

    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示" message:@"你确定要退

    出吗" preferredStyle:UIAlertControllerStyleAlert];

     

    模态展示

        [self presentViewController:alertController animated:YES completion:nil];  

     

     

    表格的可编辑状态,默认NO

    _tableView.editing=NO;

    开关

    [_tableView setEditing:!_tableView.editing animated:YES];

     

     

     

     

     

     

    隐藏状态栏

    -(BOOL)prefersStatusBarHidden{

        return YES;

    }

     

     

    =====================================================================================

    ASI 

    预编译

    $(SRCROOT)/项目名/JDDemo-PrefixHeader.pch

     

     

    自适应

    当父视图的尺寸发生变化时,子视图以怎样的方式发生变化,子视图的宽度和高度灵活的随着父视图变化

        topView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

     

    self.view.adjustsFontSizeToFitWidth=YES;

     

    注意手势会冒泡上抛,一个view没有实现的手势,如果父类view有实现,父视图就处理,如果不想让父视图处理,就把本视图添加到底层window上

     

     

    动画===============================================================

    UIImageView *imgview; 图片动画,区别于UIview动画(属性动画和翻转动画)

     

    [imgview startAnimating];

     

    imageview.animationImages=图片数组//连续动画

     

    方法可以设置动画的延迟时间、动画效果、动画结束后执行的代码

        [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear 

    animations:^{

            

        } completion:^(BOOL finished) {

            

        }];

    ===============================================================

    可变属性字符串

      NSMutableAttributedString *selectInfo = [[NSMutableAttributedString alloc] initWithString:[NSString 

    stringWithFormat:@"已选 %@ %ld",XXX,XXX]];

     

      [selectInfo addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(3

    selectInfo.length - 3)];

     

     

     

     

     

    方法调用和执行

     reponseForSelector

    performSelector: withObject://运行时确定是否有这个方法,编译时不关心

     

     

  • 相关阅读:
    Golang相关环境变量
    mac catalina关闭系统更新提示
    Git常用命令
    UDP打洞原理
    LANMP相关配置
    Windows平台编译libevent
    Sublime text2 + cygwin编译C++
    C++的指针常量和常量指针
    C++的new和delete
    C/C++产生随机数
  • 原文地址:https://www.cnblogs.com/guaishushu/p/4776138.html
Copyright © 2011-2022 走看看