- (void)loadView
{
//必须先初始化一个底层的 View
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = aView;
[aView release];
UIView *twoView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 60,60)];
twoView.backgroundColor = [UIColor blueColor];
//自动调整大小//自动调整宽和高
twoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
oneView.tag = 1000; //设置View的tag值,可以在方法外通过tag值取得这个View
oneView.backgroundColor = [UIColor redColor];
oneView.alpha = 1.0f;//设置透明度(0.1 <-> 1)默认是1.0
oneView.center = CGPointMake(160, 300);//设置View的中心点
NSLog(@"bounds = %@",NSStringFromCGRect(oneView.bounds));//输出oneView的尺寸
oneView.userInteractionEnabled = YES;//是否接收响应点击
oneView.autoresizesSubviews = YES;//是否自动调整subviews的大小,默认YES。
[oneView addSubview:twoView];
[twoView release];
[self.view addSubview:oneView];
[oneView release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"UP" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 0, 100, 30);
btn.center = CGPointMake(160, 460-30);
[btn addTarget:self action:@selector(up:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
-(void)up:(id)sender
{
UIView *oneView = [self.view viewWithTag:1000];//取出Tag值为1000所对应的View
//
// CGRect r = oneView.frame;
// r.size.width += 5;
// r.size.height += 5;
CGRect rect =CGRectInset(oneView.frame, -10, -10);
//CGRect rect =CGRectInset(oneView.frame, 10, 10);//缩小
oneView.frame = rect;
}
改变视图的放大 缩小
最新推荐文章于 2023-06-05 01:16:00 发布