繁体   English   中英

Objective-C:AutoresizingMask + 2 Subviews =奋斗

[英]Objective-C: AutoresizingMask + 2 Subviews = struggling


解决方案:对于那些将来某天查看此内容的人,我使用的解决方案确实是viewDidLayoutSubviews 解决方案实际上相当复杂 - 每次页面需要重新布局时,我必须计算几个缩放值并动态调整Art视图的大小。 有几个奇怪的问题需要处理,但在每个问题被取消之后,实现感觉非常可靠。

如果有人稍后遇到类似问题,请告诉我,我可以发布相关代码。


我有一个'空白'UIView,一个包含单个图像的UIImageView的子视图,以及第二个子视图,它基本上是CGContext弧和线的集合以及所有这些。

我在哪里:我已经将UIImageView子视图放在UIView的顶部,这样它的图像就是'背景'。 然后我将CGContext弧和线子视图置于其上(为了清楚起见,我将其称为Art子视图)。 都好。 一切都显示完美和对齐。

问题:当我旋转时,事情变得棘手了。 Art子视图上的弧线和线条以错误的坐标结束,并且根据我的autoresizingmask设置,图像被拉伸等等。我可以一次解决其中一个问题,但我找不到合适的组合来修复他们俩!

我尝试过的事情:我已经尝试了几乎每个autoresizingMask选项和选项的组合,但除此之外我不能完全解决这些问题。 鉴于我也尝试在viewDidLayoutSubviews中使用一些自定义代码,但与使用autoresizingMasks相比,这感觉非常脆弱且不易扩展。 所以我在取得一些名义上的进步后放弃了这条路。

我学到了什么:只要我将UIImageView框架和Art子视图框架绑定到相同的尺寸,那么弧线和线条就会保持在正确的坐标上。 这可能是说明我的目标的最简单的方法:让UIImageView保持正确的宽高比(不仅仅是图像内部,而是视图本身),然后将Art子视图与其框架完全匹配 - 即使屏幕也是如此旋转等

这是我想要实现的图表:

+ = UIView

~ = UIImageView子视图

. =艺术子视图

肖像

其中UIImageView中的图像基本上占据整个屏幕(虽然不完全),并且Art子视图在其上面分层,下面的点代表粗线/弧。

++++++++++
+~~~~~~~~+
+~   .  ~+
+~  .   ~+
+~ .    ~+
+~ .    ~+
+~~~~~~~~+
++++++++++

景观

其中UIImageView子层保持其纵横比,并且Art子层在UIImageView中保持“锁定”到图像。

++++++++++++++++++++++++++
+         ~~~~~~~~       +
+         ~   .  ~       +
+         ~  .   ~       +
+         ~ .    ~       +
+         ~ .    ~       +
+         ~~~~~~~~       +
++++++++++++++++++++++++++

我的视图控制器 (我认为问题在于 - 也删除了所有autoresizingMask设置以清理代码)

- (void)viewWillAppear:(BOOL)animated {
    // get main screen bounds & adjust to include apple status bar
    CGRect frame = [[UIScreen mainScreen] applicationFrame];

    // get image - this code would be meaningless to show, but suffice to say it works
    UIImage *image = [.. custom method to get image from my image store ..];

    // custom method to resize image to screen; see method below
    image = [self resizeImageForScreen:image];

    // create imageView and give it basic setup
    imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setUserInteractionEnabled:YES];

    [imageView setFrame:CGRectMake(0,
                                   0,
                                   image.size.width, 
                                   image.size.height)];

    [imageView setCenter:CGPointMake(frame.size.width / 2,
                                     frame.size.height / 2)];

    [[self view] addSubview:imageView];

    // now put the Art subview on top of it
    // (customArtView is a subclass of UIView where I handle the drawing code)
    artView = [[customArtView alloc] initWithFrame:imageView.frame];

    [[self view] addSubview:artView];
}

resizeImageForScreen:方法 (这似乎工作正常,但我想我还是包括它)

- (UIImage *)resizeImageForScreen:(UIImage *)img {

    // get main screen bounds & adjust to include apple status bar
    CGRect frame = [[UIScreen mainScreen] applicationFrame];

    // get image
    UIImage *image = img;

    // resize image if needed
    if (image.size.width > frame.size.width || image.size.height > frame.size.height) {

        // Figure out a scaling ratio to make sure we maintain the same aspect ratio
        float ratio = MIN(frame.size.width / image.size.width, frame.size.height / image.size.height);

        CGRect newImageRect;
        newImageRect.size.width = ratio * image.size.width;
        newImageRect.size.height = ratio * image.size.height;
        newImageRect.origin.x = 0;
        newImageRect.origin.y = 0;

        // Create a transparent context @ the newImageRect size & no scaling
        UIGraphicsBeginImageContextWithOptions(newImageRect.size, NO, 0.0);

        // Draw the image within it (drawInRect will scale the image for us)
        [image drawInRect:newImageRect];

        // for now just re-assigning i since i don't need the big one
        image = UIGraphicsGetImageFromCurrentImageContext();

        // Cleanup image context resources; we're done!
        UIGraphicsEndImageContext();

    }

    return image;
}

没有自动调整标志和内容模式的组合可以执行您想要的操作。 使用viewDidLayoutSubviews是处理布局的一种合理方法。 这就是它存在的原因:自动化标志非常有限。

另一种方法是更改-[ArtView drawRect:]方法,以便自动调整标志可以执行您想要的操作。 您可以使drawRect:方法实现UIImageViewUIViewContentModeScaleAspectFit实现的相同算法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM