UIView,uiview動畫

來源:互聯網
上載者:User

UIView,uiview動畫

  /*

     圖層關係

     子視圖 - 嵌套在父視圖之上的視圖

     父視圖 - 被嵌套的視圖

     視圖和視圖之間可以層層嵌套

     */

    UIView *red = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];

    red.backgroundColor = [UIColor redColor];

   

    //1.在父視圖上嵌套一個子視圖

    [self.window addSubview:red];

    

    //2.如何擷取父視圖(僅有一個)

    UIView *window = [red superview];

    //通過地址相同可知道,這是同一個window

    NSLog(@"%p",self.window);

    NSLog(@"%p",window);

    

    // 3.擷取子視圖(可以有多個)[注意]傳回值是一個數組

    NSArray *subViews = [self.window subviews];

    NSLog(@"%lu",[subViews count]);

    

    //4.刪除一個視圖

    //[red removeFromSuperview];

    

    UIView *blue = [[UIView alloc]initWithFrame:CGRectMake(60, 110, 100, 100)];

    blue.backgroundColor = [UIColor blueColor];

    [self.window addSubview:blue];

    

    //5.將一個視圖放在父視圖前面

    //[self.window bringSubviewToFront:red];

    

    //6.將一個視圖放在父視圖後面

    //[self.window sendSubviewToBack:blue];

    

    UIView *yellow = [[UIView alloc]initWithFrame:CGRectMake(70, 120, 100, 100)];

    yellow.backgroundColor = [UIColor yellowColor];

    //可以用以下4種方法將yellow視圖添加到window中

    //[self.window addSubview:yellow];

    

    //7.在某個視圖的後面插入一個視圖

    //[self.window insertSubview:yellow belowSubview:red];

    

    //8.在某個視圖的前面插入一個視圖

    //[self.window insertSubview:yellow aboveSubview:red];

    

    //9.利用下標在指定位置插入

    //座標index是從內部算起

    [self.window insertSubview:yellow atIndex:2];

 

    //***************分割線*******************

    //10.交換兩個視圖

    //[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:2];

    

    //檢查某個視圖是否為子孫視圖或本身

    BOOL is = [red isDescendantOfView:self.window];

    NSLog(@"%d",is);

    

    //黃色視圖的左上方為原點

    UIView *green = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];

    green.backgroundColor = [UIColor greenColor];

    [yellow addSubview:green];

    //將黑色視圖作為子視圖放在黃色父視圖中

    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)];

    black.backgroundColor = [UIColor blackColor];

    [yellow addSubview:black];

    

    //11.裁剪超出父視圖之外的部分

    yellow.clipsToBounds = YES;

    NSLog(@"%ld",[self.window subviews].count);

    

    //12.隱藏一個視圖

    //black.hidden = YES;

    //倘若父視圖被隱藏了,那麼它的子視圖也將要被隱藏

    //yellow.hidden = YES;

    

    //13.透明度:如果父視圖被設定透明度,父視圖上的所有子視圖也會被一起設定透明度

    //yellow.alpha = 0.6;

    

    //以下方式可以設定父視圖的透明度,子視圖不受影響

    yellow.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];

    

    //14.使用者互動的設定

    //YES 使用者互動開啟 (使用者可以操作這個UI控制項)

    //NO 使用者互動關閉 (使用者不可以操作這個UI控制項)

    /*

     UIView 預設情況互動開啟

     UIButton 預設使用者互動開啟

     UIImageView 預設使用者互動關閉

     UILabel 預設使用者互動關閉

     */

    //先訪問 window螢幕

    //再訪問 self.view (根控制器) -- 實際上也是UIView

    //之後訪問 self.view 中的子視圖  myImageView

    //最後訪問 子視圖myImageView中嵌套的子視圖 myUIView

    //如果其中一個環節關閉了使用者互動 那麼下面的子視圖都不能訪問到

    window.userInteractionEnabled = YES;

    /*

     【注】

    ️使用者互動層層傳遞,其中有一個視圖的使用者互動關閉,則不會繼續往下傳遞

    ️子視圖不要超過父視圖的邊界 否則也會改層終止傳遞 使用者互動也是無效的(即使你已經在父視圖開啟使用者互動)

    */

        

    //15.銷毀一個視圖(removeFromSuperview)

    //將一個控制項從其父視圖中移除

    //[注意] 移除是有跟隨性的 子視圖也會全部移除

    [yellow removeFromSuperview];

  

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.