標籤:
第一課
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開發基礎