zoukankan      html  css  js  c++  java
  • UI基础之UIViewController

    每个应用程序的window对象都有且只有一个根控制器

     每一个视图控制器都自带一个view

    UIViewController是所有控制器的根控制器

     

     

     

    #pragma mark - 定时器调用方法

    - (void)move{

    //获取当前视图子视图
    UIView * view= [self.view viewWithTag:101];

    //获取原来的x值
    CGFloat x=view.frame.origin.x;

    //当x超界限了,将方向变为后退
    if (x>kWidth-50) {
    _direction=1;
    }

    //当x<=0了,将方向变为前进
    if (x<=0) {
    _direction=0;
    }

    //根据方向判断当前是前进还是后退
    if(_direction==1){
    view.frame=CGRectMake(x-3, 20, 50, 50);
    }else{
    view.frame=CGRectMake(x+3, 20, 50, 50);
    }

     

    #import "ViewController.h"

     

    #define kWidth [UIScreen mainScreen].bounds.size.width

    #define kHeight [UIScreen mainScreen].bounds.size.height

    #define randomColor() [UIColor colorWithRed:arc4random()%10*0.1 green:arc4random()%10*0.1 blue:arc4random()%10*0.1 alpha:1]

    @interface ViewController ()

     

    @end

     

    @implementation ViewController{

        NSInteger _directionX;

        NSInteger _directionY;

        NSTimer *_timer;

    }

     

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        

        //根视图,相当于rootview

    //    每个应用程序的window对象都有且只有一个根控制器

    //    每一个视图控制器都自带一个view

        self.view.backgroundColor=[UIColor redColor];

        

        //增加一个视图

        UIView *view1=[[UIView alloc] initWithFrame:CGRectMake(0, 20, 50,50)];

        view1.backgroundColor=[UIColor blackColor];

        view1.layer.cornerRadius=25;

        view1.tag=1001;

    //    view1.layer.borderColor=[UIColor whiteColor].CGColor;

    //    view1.layer.borderWidth=2;

    //    

        [self.view addSubview:view1];

        

     

        

        

    }

     x,y轴移动弹动

    -(void) move{

        

    //获取当前视图的子视图

        UIView *view=[self.view viewWithTag:1001];

        

    //获取原来的x坐标

       CGFloat x=view.frame.origin.x;

       CGFloat y=view.frame.origin.y;

        

    //当越界时要往回走x

    //    _directionX=1往前走, _directionX=0往回走

        if (x>kWidth-50) {

            _directionX=0;

        }else if(x<=0){

            _directionX=1;

        }

        

    //当越界时要往回走y

        //    _directionY=1往下走, _directionY=0往上走

        if (y>kHeight-50) {

            _directionY=0;

        }else if(y<=20){

            _directionY=1;

        }

        

    //根据方向判断走向x

        if (_directionX==1&&_directionY==1) {

            view.frame=CGRectMake(x+3, y+3, 50, 50);

            view.backgroundColor=[UIColor whiteColor];

        }

        if (_directionX==1&&_directionY==0) {

            view.frame=CGRectMake(x+3, y-3, 50, 50);

            view.backgroundColor=[UIColor brownColor];

        }

        if (_directionX==0&&_directionY==0) {

            view.frame=CGRectMake(x-3, y-3, 50, 50);

             view.backgroundColor=[UIColor grayColor];

        }

        if (_directionX==0&&_directionY==1) {

            view.frame=CGRectMake(x-3, y+3, 50, 50);

             view.backgroundColor=[UIColor blackColor];

        }    

    }

     

    #pragma mark - 定时器调用方法

    - (void)move{

     

        //获取当前视图子视图

        UIView * view= [self.view viewWithTag:101];

        

        //获取原来的x

        CGFloat x=view.frame.origin.x;

        //获取原来的y

        CGFloat y=view.frame.origin.y;

        

        //x超界限了,将方向变为后退

        if (x>kWidth-50) {

            _directionX=1;

            view.backgroundColor=randomColor();

        }

        

        //Y超界限了,将方向变为后退

        if (y>kHeight-50) {

            _directionY=1;

            view.backgroundColor=randomColor();

        }

        

        

        //x<=0了,将方向变为前进

        if (x<=0) {

            _directionX=0;

            view.backgroundColor=randomColor();

            

        }

        //y<=0了,将方向变为前进

        if (y<=20) {

            _directionY=0;

            view.backgroundColor=randomColor();

            

        }

     

        

        //根据方向判断当前是前进还是后退

        //计算当前X值应该是多少

        CGFloat nowX=0;

        if (_directionX==0) {

            nowX=x+3;

        }else{

            nowX=x-3;

        }

        

        

        //计算当前Y值应该是多少

        CGFloat nowY=0;

        if (_directionY==0) {

            nowY=y+3;

        }else{

            nowY=y-3;

        }

     

        view.frame=CGRectMake(nowX, nowY, 50, 50);    

    }

     

     

     

    //点击时开始或停止

    - (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        

        //如果定时器对象为空,则重新启动定时器,如果定时器对象不为空,则让定时器失效 invalidate方法)

        if (_timer==nil) {

            //开启定时器

            _timer=[NSTimer scheduledTimerWithTimeInterval:0.04 target: self selector:@selector(move) userInfo:nil repeats:YES];

           

        }else {

            [_timer invalidate];

            _timer=nil;

            

        }

    }

     

     //到边界就隐藏 边界是以view1

    //    view1.clipsToBounds=YES;

     

    #import "JRModalViewController.h"

    #import "JRViewController.h"

     

    @interface JRModalViewController ()

     

    @end

     

    @implementation JRModalViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        //1 加载视图

        [self  setViews];

        

    }

     

     

    #pragma mark - 加载子视图

    - (void) setViews{

     

        

        //1  添加按钮

        self.view.backgroundColor=[UIColor grayColor];

        

        UIButton * button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];

        [button setTitle:@"确定" forState:UIControlStateNormal];

        button.backgroundColor=[UIColor redColor];

        

        //通过中心点居中视图

        button.center=self.view.center;

        button.layer.cornerRadius=20;

        

        [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

        

        [self.view addSubview:button];

        

        //2 添加背景图

        UIImage * image=[UIImage imageNamed:@"08.jpg"];

        self.view.backgroundColor=[UIColor colorWithPatternImage:image];

        

     

    }

     

     

     

     

    #pragma mark - 点击事件

    - (void) click{

     

        //初始化小球控制器

        JRViewController * vc=[[JRViewController alloc] init];

        [vc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

        

        [self presentViewController:vc animated:YES completion:^{

            NSLog(@"展示出来了");

        }];

        

    } 

    @end

     

     

  • 相关阅读:
    剑指Offer_编程题_从尾到头打印链表
    剑指Offer_编程题_替换空格
    剑指Offer_编程题_二维数组中的查找
    我用java爬虫爬了一个图片网站
    docker安装mysql5.7
    设计模式和设计原则
    nginx 限流配置
    JAVA性能监控与调优参考文档链接
    单例模式
    Java开发中对Redis的基本操作
  • 原文地址:https://www.cnblogs.com/gzoof/p/5484351.html
Copyright © 2011-2022 走看看