Pra*_*d G 3 transform uiview ios
我创建了mainView objcet UIView
并在其上添加了一个子视图.我在mainView上应用了变换以减小帧大小.但是mainView的子视图框架没有减少.如何减小这个子视图的大小.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat widthM=1200.0;
CGFloat heightM=1800.0;
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthM, heightM)];
mainView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"te.png"]];
[self.view addSubview:mainView];
CGFloat yourDesiredWidth = 250.0;
CGFloat yourDesiredHeight = yourDesiredWidth *heightM/widthM;
CGAffineTransform scalingTransform;
scalingTransform = CGAffineTransformMakeScale(yourDesiredWidth/mainView.frame.size.width, yourDesiredHeight/mainView.frame.size.height);
mainView.transform = scalingTransform;
mainView.center = self.view.center;
NSLog(@"mainView:%@",mainView);
UIView *subMainView= [[UIView alloc] initWithFrame:CGRectMake(100, 100, 1000, 1200)];
subMainView.backgroundColor = [UIColor redColor];
[mainView addSubview:subMainView];
NSLog(@"subMainView:%@",subMainView);
}
Run Code Online (Sandbox Code Playgroud)
NSlog的这些观点:
mainView:<UIView: 0x8878490; frame = (35 62.5; 250 375); transform = [0.208333, 0, 0, 0.208333, 0, 0]; layer = <CALayer: 0x8879140>>
subMainView:<UIView: 0x887b8c0; frame = (100 100; 1000 1200); layer = <CALayer: 0x887c160>>
Run Code Online (Sandbox Code Playgroud)
这里mainView的宽度是250,子视图的宽度是1000.但是当我在模拟器中得到输出时,子视图被正确占用,但它没有穿过mainView.怎么可能?如何在转换后获得关于mainView框架的子视图框架?
您所看到的是预期的行为.a的框架UIView
相对于其父框架,因此在将转换应用于其superview时它不会更改.虽然视图也会显示为"扭曲",但框架不会反映更改,因为它仍然与相对于其父级的位置完全相同.
但是,我假设你想获得相对于最顶层的视图框架UIView
.在这种情况下,UIKit提供以下功能:
– [UIView convertPoint:toView:]
– [UIView convertPoint:fromView:]
– [UIView convertRect:toView:]
– [UIView convertRect:fromView:]
我将这些应用于您的示例:
CGRect frame = [[self view] convertRect:[subMainView frame] fromView:mainView];
NSLog(@"subMainView:%@", NSStringFromCGRect(frame));
Run Code Online (Sandbox Code Playgroud)
这是输出:
subMainView:{{55.8333, 83.3333}, {208.333, 250}}
Run Code Online (Sandbox Code Playgroud)