- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
UIView *view1 = [[UIViewalloc] initWithFrame:CGRectMake(20, 20, 280, 320)];
view1.tag = 100;
view1.backgroundColor = [UIColorredColor];
[self.viewaddSubview:view1];
[view1 release];
UIView *view2 = [[UIViewalloc] initWithFrame:CGRectMake(20, 20, 240, 280)];
view2.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
view2.tag = 101;
view2.backgroundColor = [UIColorgreenColor];
[view1 addSubview:view2];
[view2 release];
UIView *view3 = [[UIViewalloc] initWithFrame:CGRectMake(20, 20, 200, 240)];
// view3.alpha = 0.2;
view3.tag = 102;
view3.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
view3.backgroundColor = [UIColorblackColor];
[view2 addSubview:view3];
[view3 release];
UIButton *zoomOutBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[zoomOutBtn setTitle:@"放大"forState:UIControlStateNormal];
zoomOutBtn.frame = CGRectMake(20, 350, 100, 50);
[zoomOutBtn addTarget:self action:@selector(zoomOut) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:zoomOutBtn];
UIButton *zoomInBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[zoomInBtn setTitle:@"缩小"forState:UIControlStateNormal];
zoomInBtn.frame = CGRectMake(200, 350, 100, 50);
[zoomInBtn addTarget:selfaction:@selector(zoomIn) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:zoomInBtn];
colorArray = [[NSArrayalloc] initWithObjects:[UIColorredColor],[UIColororangeColor], [UIColoryellowColor],[UIColorgreenColor],[UIColorblackColor],[UIColorblueColor],[UIColorpurpleColor], nil];
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeColor) userInfo:nilrepeats:YES];
}
- (void)changeColor
{
/*
UIView *view1 = [self.view viewWithTag:100];
UIView *view2 = [self.view viewWithTag:101];
UIView *view3 = [self.view viewWithTag:102];
UIColor *temp = view1.backgroundColor;
view1.backgroundColor = view2.backgroundColor;
view2.backgroundColor = view3.backgroundColor;
view3.backgroundColor = temp;
*/
staticint i = 0;
staticint j = 1;
staticint k = 2;
UIView *view1 = [self.viewviewWithTag:100];
view1.backgroundColor = [colorArray objectAtIndex:i];
UIView *view2 = [self.viewviewWithTag:101];
view2.backgroundColor = [colorArray objectAtIndex:j];
UIView *view3 = [self.viewviewWithTag:102];
view3.backgroundColor = [colorArray objectAtIndex:k];
if (i++ == 6) {
i = 0;
}
if (j++ == 6) {
j = 0;
}
if (k++ == 6) {
k = 0;
}
}
- (void)zoomOut
{
UIView *view1 = [self.viewviewWithTag:100];
[view1 setFrame:CGRectInset(view1.frame, -10, -10)];
}
- (void)zoomIn
{
UIView *view1 = [self.viewviewWithTag:100];
[view1 setFrame:CGRectInset(view1.frame, 10, 10)];
}
//注意CGRectInset
/*
CGRect CGRectInset (
CGRect rect,
CGFloat dx,
CGFloat dy
);
该结构体的应用是以原rect为中心,再参考dx,dy,进行缩放或者放大。
*/