菜鸟教程小白 发表于 2022-12-11 17:17:02

ios - 旋转后随机放置的 UILabel 大小错误


                                            <p><p>有一个 UIVew 容器,用于随机生成、旋转并放置到此 View 中,如果我使用角度 0、90、180、270 看起来不错,但如果角度随机生成的标签看起来扩展了,这对我来说是个问题因为单词应该放置得非常接近并且没有交叉,这看起来像生成器:</p>

<pre><code>- (void) generate:(UIView *)container
            words:(NSArray *)words
         colors:(NSArray *)colors
      minFontSize:(float)minSize
      maxFontSize:(float)maxSize
      rotateWords:(BOOL)rotate
    useAngleRange:(BOOL)useRange
{
    usingRange = useRange;
    for (int i = 0; i &lt; ; i++) {
      UILabel *word_l = [ init];
      word_l.font = ];
      ];
      )]];
      ];
      ;

      CGRect fr = CGRectMake(0,
                               0,
                               word_l.frame.size.width,
                               word_l.frame.size.height);
      word_l.frame = fr;
      word_l.center = CGPointMake(, );
      if (rotate) {
            int angleType = arc4random_uniform(2);
            if (useRange) {
                int angle = (angleType == 1) ? : ;
                ;
            } else {
                ;
            }
      }
      ;
      while () {
            viewPositionMovingStep++;
            ;
      }
    }
}

- (void)placeItem:(UIView *)container :(UILabel *)word_l
{
    CGRect initFr = word_l.frame;
    for (int i = 1; i &lt;= 8; i++) {
      CGRect f = word_l.frame;
      switch (i) {
            case 1:
                f.origin.x = f.origin.x + viewPositionMovingStep;
                break;
            case 2:
                f.origin.x = f.origin.x + viewPositionMovingStep;
                f.origin.y = f.origin.y + viewPositionMovingStep;
                break;
            case 3:
                f.origin.y = f.origin.y + viewPositionMovingStep;
                break;
            case 4:
                f.origin.x = f.origin.x - viewPositionMovingStep;
                f.origin.y = f.origin.y + viewPositionMovingStep;
                break;
            case 5:
                f.origin.x = f.origin.x - viewPositionMovingStep;
                break;
            case 6:
                f.origin.x = f.origin.x - viewPositionMovingStep;
                f.origin.y = f.origin.y - viewPositionMovingStep;
                break;
            case 7:
                f.origin.y = f.origin.y - viewPositionMovingStep;
                break;
            case 8:
                f.origin.x = f.origin.x + viewPositionMovingStep;
                f.origin.y = f.origin.y - viewPositionMovingStep;
                break;
            default:
                break;
      }
      word_l.frame = f;
      if () {
            word_l.frame = initFr;
      } else {
            viewPositionMovingStep = 0;
            return;
      }
    }
}

-(BOOL)viewIntersectsWithAnotherView:(UIView *)container chosenView:(UIView *)chosenView
{
    for(UIView *view in ){
      if (!){
            BOOL framePartiallyOut = !CGRectEqualToRect(CGRectIntersection(chosenView.superview.bounds, chosenView.frame), chosenView.frame);
            if (usingRange) {
                if( || framePartiallyOut){
                  return YES;
                }
            } else {
                if(CGRectIntersectsRect(chosenView.frame, view.frame) || framePartiallyOut){
                  return YES;
                }
            }
      }
    }
    return NO;
}

- (BOOL)view:(UIView *)view1 intersectsWith:(UIView *)view2
{
    CGPoint poly1;
    CGRect bounds1 = view1.bounds;
    poly1 = ;
    poly1 = ;
    poly1 = ;
    poly1 = ;

    CGPoint poly2;
    CGRect bounds2 = view2.bounds;
    poly2 = ;
    poly2 = ;
    poly2 = ;
    poly2 = ;

    CGPoint ctl2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, ctl2)){
      return YES;
    }
    CGPoint ctr2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, ctr2)){
      return YES;
    }
    CGPoint cbr2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, cbr2)){
      return YES;
    }
    CGPoint cbl2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, cbl2)){
      return YES;
    }
    CGPoint ctl1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, ctl1)){
      return YES;
    }
    CGPoint ctr1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, ctr1)){
      return YES;
    }
    CGPoint cbr1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, cbr1)){
      return YES;
    }
    CGPoint cbl1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, cbl1)){
      return YES;
    }
    return NO;
}

-(int)getRandomNumberBetween:(int)from to:(int)to
{
    return (int)from + arc4random() % (to-from+1);
}
</code></pre>

<p>这看起来正确放置标签:</p>

<p> <a href="/image/5aH6s.png" rel="noreferrer noopener nofollow"><img src="/image/5aH6s.png" alt="well transformed subviews"/></a> </p>

<p>以及这个带有随机生成角度的短词示例(范围 270-360、0-90):</p>

<p> <a href="/image/MktEq.png" rel="noreferrer noopener nofollow"><img src="/image/MktEq.png" alt="enter image description here"/></a> </p>

<p>正如您所注意到的,没有文本显示在那里,标签看起来展开并且在这种情况下交叉检查器不起作用,如果从 <code>viewDidLoad</code> 或 <code>viewDidApper</code> 这个检查器调用的函数也是如此根本不工作,我真的需要帮助,有什么想法吗?
谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>调整大小问题是这样解决的:</p>

<pre><code>- (void)placeItem:(UIView *)container :(UILabel *)word_l
{
    CGPoint initPoint = word_l.center;
    for (int i = 1; i &lt;= 8; i++) {
      switch (i) {
            case 1:
                word_l.center = CGPointMake(word_l.center.x + viewPositionMovingStep, word_l.center.y);
                break;
            case 2:
                word_l.center = CGPointMake(word_l.center.x + viewPositionMovingStep, word_l.center.y + viewPositionMovingStep);
                break;
            case 3:
                word_l.center = CGPointMake(word_l.center.x, word_l.center.y + viewPositionMovingStep);
                break;
            case 4:
                word_l.center = CGPointMake(word_l.center.x - viewPositionMovingStep, word_l.center.y + viewPositionMovingStep);
                break;
            case 5:
                word_l.center = CGPointMake(word_l.center.x - viewPositionMovingStep, word_l.center.y);
                break;
            case 6:
                word_l.center = CGPointMake(word_l.center.x - viewPositionMovingStep,- viewPositionMovingStep);
                break;
            case 7:
                word_l.center = CGPointMake(word_l.center.x, word_l.center.y - viewPositionMovingStep);
                break;
            case 8:
                word_l.center = CGPointMake(word_l.center.x + viewPositionMovingStep, word_l.center.y - viewPositionMovingStep);
                break;
            default:
                break;
      }
      if () {
            word_l.center = initPoint;
      } else {
            viewPositionMovingStep = 0;
            return;
      }
    }
}
</code></pre>

<p>原因是:
使用transform属性时,不能通过改变frame来改变位置,必须使用center属性。</p>

<p>所以这部分已修复,但留下了与旋转 View 交叉点相关的问题,检查器无法正常工作,但在其他项目中单独测试它做得很好。</p>

<p>他来了:</p>

<pre><code>- (BOOL)view:(UIView *)view1 intersectsWith:(UIView *)view2
{
    CGPoint poly1;
    CGRect bounds1 = view1.bounds;
    poly1 = ;
    poly1 = ;
    poly1 = ;
    poly1 = ;

    CGPoint poly2;
    CGRect bounds2 = view2.bounds;
    poly2 = ;
    poly2 = ;
    poly2 = ;
    poly2 = ;

    CGPoint ctl2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, ctl2)){
      return YES;
    }
    CGPoint ctr2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, ctr2)){
      return YES;
    }
    CGPoint cbr2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, cbr2)){
      return YES;
    }
    CGPoint cbl2 = fromView:nil];
    if (CGRectContainsPoint(view1.bounds, cbl2)){
      return YES;
    }
    CGPoint ctl1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, ctl1)){
      return YES;
    }
    CGPoint ctr1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, ctr1)){
      return YES;
    }
    CGPoint cbr1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, cbr1)){
      return YES;
    }
    CGPoint cbl1 = fromView:nil];
    if (CGRectContainsPoint(view2.bounds, cbl1)){
      return YES;
    }
    return NO;
}
</code></pre>

<p>有人想法吗?</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 旋转后随机放置的 UILabel 大小错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38989888/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38989888/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 旋转后随机放置的 UILabel 大小错误