5. UIView

1. UIView 的初認識

官方文檔 UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area.At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content.UIView在屏幕上定義了一個矩形區域和管理區域內容的接口。在運行時,一個視圖對象控制該區域的渲染,同時也控制內容的交互。)UIView就至關於一塊白牆,這塊白牆只是負責把加入到裏面的東西顯示出來而已。git

也能夠說UIView表示屏幕上的一塊矩形區域,它在App中佔有絕對重要的地位,由於IOS中幾乎全部可視化控件都是UIView的子類。負責渲染區域的內容,而且響應該區域內發生的觸摸事件,能夠這麼說在iphone裏你看到的,摸到的,都是UIView。因此UIView 在iOS開發中擁有很重要的地位github

2. UIView 的使用

2.1 基本使用方法數組

UIView *viewOne = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];iphone

// 設置背景顏色動畫

viewOne.backgroundColor = [UIColor redColor];spa

// 設置view 中心的位置orm

viewOne.center = CGPointMake(200, 200);對象

// 設置透明度(若是父視圖的透明度改變了,全部子視圖都改變)索引

 viewOne.alpha = 0.5;接口

// 隱藏,若是父視圖隱藏,那麼子視圖也會隱藏(隱藏和透明度設置成0 效果同樣,可是隱藏相似於移除(可是並無刪除),透明度設置成0 控件仍是存在,只是咱們看不到)

 view.hidden = YES;

// 切掉子視圖超出的部分,也就是若是一個視圖超出了他所添加在的UIView,那麼超出的部分就會被切除

 viewOne.clipsToBounds = YES;

// 把子視圖從父視圖上移除

  [subView1 removeFromSuperview];

// 禁止視圖接受事件(父視圖不能接受 子視圖一樣不能)

  superView.userInteractionEnabled = YES;

// 獲取父視圖

 UIView *view = subView1.superview;

 // 獲取子視圖(能夠遍歷子視圖數組,取出你想獲取的視圖)

    NSArray *array = superView.subviews;

 

// 若是子視圖中有這個tag的視圖,直接取出。 若是沒有就會深度遍歷子視圖的子視圖去找這個tag的子視圖,若是仍是沒有 返回nil

    UIView *sView = [superView viewWithTag:11];

// 鑑定父子關係

    // isDescendantOfView 判斷一個視圖是否是另外一個視圖的直接或間接的子視圖

    BOOL boo = [subView1 isDescendantOfView:superView];

 

     // 拓展方法

  • //將一個視圖移到前面  
  • bringSubviewToFront:  
  • //將一個視圖推送到背後  
  • sendSubviewToBack:  
  • //把視圖移除  
  • removeFromSuperview  
  • //插入視圖 並指定索引  
  • insertSubview:atIndex:  
  • //插入視圖在某個視圖之上  
  • insertSubview:aboveSubview:  
  • //插入視圖在某個視圖之下  
  • insertSubview:belowSubview:  
  • //交換兩個位置索引的視圖  
  • exchangeSubviewAtIndex:withSubviewAtIndex:

 

2.2 仿射變換

// 1. view 旋轉(順時針旋轉多少度)

 view.transform = CGAffineTransformMakeRotation(M_PI / 3);

 // 2. view 變形(參數一做用:寬 * 比例  參數一做用:高 * 比例)(中心點不變)

 view.transform = CGAffineTransformMakeScale(1, 2);

  // 3. 平移(1. 平移目標 2.在水平方向平移多少(正值 右移  負值 左移)3.在垂直方向平移多少(正值 下移  負值 上移))

 view.transform = CGAffineTransformTranslate(view.transform, - 100, 0);

    

2.3 動畫

// 參數1:動畫時間 animations block:在這個時間要完成的動畫

    [UIView animateWithDuration:1.0 animations:^{

        CGRect frame = _view.frame;

        frame.origin.y += 200;

        _view.frame = frame;

    }];

    

    // 參數1:動畫時間 animations block:在這個時間要完成的動畫 completion block:這個動畫執行完成以後要作什麼操做

    [UIView animateWithDuration:2.0 animations:^{

        CGRect frame = _view.frame;

        frame.origin.y += 200;

        _view.frame = frame;

        _view.alpha = 0;

        

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:2.0 animations:^{

            CGRect frame = _view.frame;

            frame.origin.y -= 200;

            _view.frame = frame;

            _view.alpha = 1;

        }];

    }];

一個UIView動畫的簡單Demo https://github.com/mcj122755/UIViewDemo5.git

相關文章
相關標籤/搜索