我有一个问题:我正在创建一个UIView
这是从方法返回的,这部分很好,但我注意到,当我将子层添加到其中一个子视图时,这些层与子层添加的层次结构中较高的子视图(textView 和 imageView)重叠到testViewCopy
出现在这些子视图之上,但它们不应该出现。我不知道这里发生了什么导致这种情况。
Code:
- (void)makeShareImages
{
UIView *shareView = [self shareView];
UIView *testViewCopy = [shareView viewWithTag:0];
NSUInteger currentIndex = 1;
for (NSDictionary *sub in self.array)
{
NSArray *lastArray = [sub objectForKey:@"LastArray"];
for (NSDictionary *dict in lastArray)
{
@autoreleasepool
{
currentIndex ++;
CircleLayer *layer = [[CircleLayer alloc]init];
layer.portrait = [[dict objectForKey:@"Portrait"]boolValue];
layer.frame = testViewCopy.bounds;
[testViewCopy.layer addSublayer:layer];
NSData *frameData = [self getSnapshotDataFromView:shareView];
NSString *savePath = [NSString stringWithFormat:@"%@/%lu.png",somePath,(unsigned long)currentIndex];
[frameData writeToFile:savePath options:0 error:nil];
}
}
}
}
- (UIView *)shareView
{
UIColor *bgColor = self.containerView.backgroundColor;
CGSize size = self.containerView.bounds.size;
UIView *viewToShare = [[UIView alloc]init];
viewToShare.backgroundColor = bgColor;
viewToShare.layer.cornerRadius = 6.0;
viewToShare.layer.masksToBounds = YES;
UIView *testViewCopy = [[UIView alloc]init];
testViewCopy.backgroundColor = [UIColor clearColor];
testViewCopy.contentMode = UIViewContentModeScaleAspectFit;
testViewCopy.layer.masksToBounds = YES;
testViewCopy.tag = 0;
UITextView *textViewCopy = [[UITextView alloc]init];
textViewCopy.backgroundColor = [UIColor clearColor];
textViewCopy.tag = 1;
textViewCopy.textContainerInset = self.textView.textContainerInset;
UIImageView *profileImageViewCopy = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 32, 32)];
profileImageViewCopy.contentMode = UIViewContentModeScaleAspectFit;
profileImageViewCopy.layer.masksToBounds = YES;
profileImageViewCopy.image = [self profileImage];
profileImageViewCopy.tag = 2;
profileImageViewCopy.layer.cornerRadius = profileImageViewCopy.frame.size.width / 2.0;
viewToShare.frame = CGRectMake(0, 0, size.width, size.height);
testViewCopy.frame = CGRectMake(0, 0, size.width, size.width);
textViewCopy.frame = CGRectMake(0, 0, size.width, size.height);
NSAttributedString *attributedStringCopy = [[NSAttributedString alloc]initWithAttributedString:self.textView.attributedText];
textViewCopy.attributedText = attributedStringCopy;
[viewToShare addSubview:testViewCopy];
[viewToShare addSubview:textViewCopy];
[viewToShare addSubview:profileImageViewCopy];
return viewToShare;
}
- (NSData *)getSnapshotDataFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImagePNGRepresentation(snapShot);
}
您的代码行为正确。所有同级视图(公共超级视图的子视图)都有明确的分层顺序,从后到前。addSubview:
将子视图添加为last在它的兄弟姐妹中;因此,它位于同一超级视图的所有现有子视图的前面。如果这不是您想要的,请将子视图插入到距离前面更远的一层,或者添加它(在前面),然后将其按分层顺序向后移动。
此外(在这里我认为我们越来越接近您注意到的特定现象),视图本身只是层。因此,视图的分层顺序实际上是层的分层顺序的子集。我刚才所说的关于视图的同样的事情也同样适用于图层,因为视图are层:如果将子层添加到超级层,它将添加到该超级层的所有其他子层之前,包括任何子视图如果该超级层实际上是一个视图。
正如我在当前版本的书中所写:
视图的子视图的底层是该视图底层的子层,就像该视图底层的任何其他子层一样。因此,它可以按绘图顺序放置在其中的任何位置。视图可以散布在其父视图底层的子层中这一事实令初学者感到惊讶。
听起来你刚刚发现了这一点,并且感到相当惊讶。
从渲染树的绘制角度来思考它可能会有所帮助。换句话说,不要去思考事情是如何发生的look,想想iOS是什么does。一个层可以有一个超级层,除了最终超级层之外,一个层可以有一个先前的兄弟层,除了超级层的第一个子层。相反,一个层可以有子层,并且可以有下一个兄弟层。我们首先绘制最终的超级层(窗口)。然后,对于我们刚刚绘制的每一层,按顺序遵循以下两个规则:
绘制其第一个子层(如果有)。
画出它的下一个兄弟姐妹(如果有的话)。
每次我们绘制一个图层时,它都在前面一切我们之前已经画过。因此,超级层的所有子层都出现在超级层的前面,并且所有后面的兄弟层(及其子层)出现在所有先前兄弟层(及其子层)的前面。这会导致您在屏幕上看到的结果。
(但是,请注意,您具有更大的灵活性,因为在兄弟姐妹中,您可以通过设置zPosition
层的属性。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)