2012-07-23 36 views
3

解决方案:对于那些谁在将来查看此哪天,我使用的溶液确实viewDidLayoutSubviews。该解决方案实际上相当复杂 - 我必须计算几个缩放值,并在每次页面需要重新布局时动态重新设置Art视图的大小。有几个奇怪的问题需要处理,但是在每个问题都被解决之后,实施就会非常稳固。目的-C:AutoresizingMask + 2子视图=挣扎

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


我有一个“空白”的UIView,一个子视图是包含单个图像的UIImageView的,第二子视图,基本上是CGContext上弧和线和所有的集合。

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

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

事情我已经试过:我已经试过几乎所有autoresizingMask选项选项&组合,但按照上述我不能完全与舔问题。鉴于我也尝试在viewDidLayoutSubviews中使用一些自定义代码,但这与使用autoresizingMasks相比,感觉真的很脆弱,可扩展性也更低。所以我在取得了一些名义上的进步之后放弃了这条路

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

这里是想我的图来实现:

+ = UIView的

~ =的UIImageView子视图

. =艺术子视图

肖像

其中,的UIImageView内的图像占据基本上整个屏幕(虽然不是完全),以及本领域子视图层叠在下面表示的粗线/弧的点的它的上面。

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

景观

其中,所述子层的UIImageView保持其纵横比,以及艺术子层撑“锁定”到的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; 
} 
+0

您如何在“艺术”视图中绘制? – 2012-07-23 19:32:55

+0

它使用触摸在它自己的视图代码中处理 - 因此在子视图加载到屏幕上后,用户绘制一条直线或一条弧线或其他任何东西 - 当然Art是理想地剪切到图像上的,因此锁定两个 – toblerpwn 2012-07-23 19:35:02

+0

您的代码将'artView'设置为'UIView'。你如何绘制它? – 2012-07-23 19:35:48

回答

2

有没有组合utoresizing标志和内容模式,将做你想做的。使用viewDidLayoutSubviews是处理布局的一种合理方法。这就是它存在的原因:自动识别标志非常有限。

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

+0

谢谢,抢 - 我会检查出'drawRect:'&&我的主要挑战重新设计'viewDidLayoutSubviews'已重新 - 绘制图像大小(它会导致应用程序执行得不好甚至崩溃)。缩放感觉就像是需要关注的合适区域 - 我还没有使用过的东西 - 再次感谢! – toblerpwn 2012-07-23 19:51:33