iOS開發基礎

來源:互聯網
上載者:User

標籤:

第一課

UI-UIView父子類別檢視自動縮放

#import "XGOAppDelegate.h"

@implementation XGOAppDelegate

{

    UIView *_backView;

}

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    UIView *view1 = [[UIView alloc] init];

    view1.frame = CGRectMake(10, 30, 300, 30);

    view1.backgroundColor = [UIColor redColor];

    [self.window addSubview:view1];

    UIView *view2 = [[UIView alloc] init];

    view2.frame = CGRectMake(5, 5, 100, 20);

    view2.backgroundColor = [UIColor yellowColor];

    [view1 addSubview:view2];

    UIView *view3 = [[UIView alloc] init];

    view3.frame = CGRectMake(100, 5, 100, 20);

    view3.backgroundColor = [UIColor blueColor];

    [view1 addSubview:view3];

  //得到父類別檢視,在這裡view2、view3相當於子類別檢視,view1相當於父類別檢視

    UIView *superView = view2.superview;

  //將父類別檢視(view1)背景顏色設定為黑色

    superView.backgroundColor = [UIColor blackColor];

  //得到子類別檢視數組

    NSArray *subViews = view1.subviews;

    NSLog(@"count = %d",subViews.count);

    //view2,view3變紅了

//    for (UIView *view in subViews) {

//        view.backgroundColor = [UIColor redColor];

//    }

 

    

    UIView *view = [subViews objectAtIndex:0];

    view.backgroundColor = [UIColor redColor];

    UIView *blueview = [[UIView alloc] init];

    blueview.frame = CGRectMake(10, 100, 300, 30);

    blueview.backgroundColor = [UIColorblueColor];

  //如果子視圖高度超出父視圖,可用clipsToBounds屬性:YES處理

    blueview.clipsToBounds = YES;

    [self.window addSubview:blueview];

    UIView *greenView = [[UIView alloc] init];

    greenView.frame = CGRectMake(10, 5, 200, 100);

    greenView.backgroundColor = [UIColor greenColor];

  //設定透明度,數越小,透明度越高

    greenView.alpha = 0.5;

    [blueview addSubview:greenView];

    //自動布局

    _backView = [[UIView alloc] init];

    _backView.frame = CGRectMake(100, 300, 120, 120);

    _backView.backgroundColor = [UIColor blackColor];

  //准許子類別檢視自動布局

    _backView.autoresizesSubviews = YES;

    [self.windowaddSubview:_backView];

    UIView *topView = [[UIView alloc] init];

    topView.frame = CGRectMake(10, 10, 100, 100);

    topView.backgroundColor = [UIColor orangeColor];

    //設定自動布局方式寬度隨著父類自動增加

//    topView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    //如果寬度與高度同時增加用或 | 表示

    topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [_backView addSubview:topView];

    [NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(timeTick) userInfo:nilrepeats:YES];

 

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.windowmakeKeyAndVisible];

    returnYES;

}

 

 

 

-(void)timeTick

{

    _backView.frame = CGRectMake(_backView.frame.origin.x, _backView.frame.origin.y, _backView.frame.size.width+5, _backView.frame.size.height+5);

}

 

@end

iOS開發基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.