2016-08-17 2 views
2

Имейте контейнер UIVew для нескольких UILabels, случайный сгенерированный, повернутый и помещенный в этот вид, также, если я использую углы 0, 90, 180, 270, выглядит хорошо, но если угол произвольно сгенерированных меток выглядит расширяется, и это проблема для меня, потому что слова должны быть расположены очень близко и без пересечения, это выглядит как генератор:Случайно размещенный UILabel имеет неправильный размер после вращения

- (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 < [words count]; i++) { 
     UILabel *word_l = [[UILabel alloc] init]; 
     word_l.font = [UIFont fontWithName:@"EuropeBold" size:[self getRandomNumberBetween:minSize to:(i < probableBigFontWords) ? maxSize : (maxSize/3)]]; 
     [word_l setText:words[i]]; 
     [word_l setTextColor:colors[arc4random_uniform([colors count])]]; 
     [word_l setBackgroundColor:[UIColor clearColor]]; 
     [word_l sizeToFit]; 

     CGRect fr = CGRectMake(0, 
           0, 
           word_l.frame.size.width, 
           word_l.frame.size.height); 
     word_l.frame = fr; 
     word_l.center = CGPointMake([self getRandomNumberBetween:175 to:639], [self getRandomNumberBetween:175 to:375]); 
     if (rotate) { 
      int angleType = arc4random_uniform(2); 
      if (useRange) { 
       int angle = (angleType == 1) ? [self getRandomNumberBetween:0 to:90] : [self getRandomNumberBetween:270 to:360]; 
       [word_l setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(angle))]; 
      } else { 
       [word_l setTransform:CGAffineTransformMakeRotation((angleType == 1) ? DEGREES_TO_RADIANS(270) : DEGREES_TO_RADIANS(360))]; 
      } 
     } 
     [container addSubview:word_l]; 
     while ([self viewIntersectsWithAnotherView:container chosenView:word_l]) { 
      viewPositionMovingStep++; 
      [self placeItem:container :word_l]; 
     } 
    } 
} 

- (void)placeItem:(UIView *)container :(UILabel *)word_l 
{ 
    CGRect initFr = word_l.frame; 
    for (int i = 1; i <= 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 ([self viewIntersectsWithAnotherView:container chosenView:word_l]) { 
      word_l.frame = initFr; 
     } else { 
      viewPositionMovingStep = 0; 
      return; 
     } 
    } 
} 

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

- (BOOL)view:(UIView *)view1 intersectsWith:(UIView *)view2 
{ 
    CGPoint poly1[4]; 
    CGRect bounds1 = view1.bounds; 
    poly1[0] = [view1 convertPoint:bounds1.origin toView:nil]; 
    poly1[1] = [view1 convertPoint:CGPointMake(bounds1.origin.x + bounds1.size.width, bounds1.origin.y) toView:nil]; 
    poly1[2] = [view1 convertPoint:CGPointMake(bounds1.origin.x + bounds1.size.width, bounds1.origin.y + bounds1.size.height) toView:nil]; 
    poly1[3] = [view1 convertPoint:CGPointMake(bounds1.origin.x, bounds1.origin.y + bounds1.size.height) toView:nil]; 

    CGPoint poly2[4]; 
    CGRect bounds2 = view2.bounds; 
    poly2[0] = [view2 convertPoint:bounds2.origin toView:nil]; 
    poly2[1] = [view2 convertPoint:CGPointMake(bounds2.origin.x + bounds2.size.width, bounds2.origin.y) toView:nil]; 
    poly2[2] = [view2 convertPoint:CGPointMake(bounds2.origin.x + bounds2.size.width, bounds2.origin.y + bounds2.size.height) toView:nil]; 
    poly2[3] = [view2 convertPoint:CGPointMake(bounds2.origin.x, bounds2.origin.y + bounds2.size.height) toView:nil]; 

    CGPoint ctl2 = [view1 convertPoint:poly2[0] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, ctl2)){ 
     return YES; 
    } 
    CGPoint ctr2 = [view1 convertPoint:poly2[1] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, ctr2)){ 
     return YES; 
    } 
    CGPoint cbr2 = [view1 convertPoint:poly2[2] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, cbr2)){ 
     return YES; 
    } 
    CGPoint cbl2 = [view1 convertPoint:poly2[3] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, cbl2)){ 
     return YES; 
    } 
    CGPoint ctl1 = [view2 convertPoint:poly1[0] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, ctl1)){ 
     return YES; 
    } 
    CGPoint ctr1 = [view2 convertPoint:poly1[1] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, ctr1)){ 
     return YES; 
    } 
    CGPoint cbr1 = [view2 convertPoint:poly1[2] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, cbr1)){ 
     return YES; 
    } 
    CGPoint cbl1 = [view2 convertPoint:poly1[3] 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); 
} 

это как выглядит правильно помещены надписи:

well transformed subviews

и этот пример короткое слово со случайными сгенерированными углами (диапазон 270-360, 0-90):

enter image description here

как вы уже заметили, текст не показывая там, этикетки выглядит расширено и пересечение проверка не работает в этом случае , также если функция вызвана от viewDidLoad или viewDidApper, эта шашки вообще не работает, мне действительно нужна помощь, какие-нибудь идеи? Спасибо!

+0

Я думаю, случайные значения сталкиваются, когда вы проверяете пересечение. но все еще не в состоянии понять, почему этикетка не отображается. –

+0

@Mahesh Agrawala, но как насчет ширины ярлыка? они выглядят слишком длинными, даже если содержат 3-4 символа – Eugene

+0

, вы можете установить тег для каждой метки и сбросить положение вращения, которое используется для «label.transform = CGAffineTransformIdentity;» и сбросьте метку фрейма –

ответ

0

изменение размеров проблема была решена следующим образом:

- (void)placeItem:(UIView *)container :(UILabel *)word_l 
{ 
    CGPoint initPoint = word_l.center; 
    for (int i = 1; i <= 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 ([self viewIntersectsWithAnotherView:container chosenView:word_l]) { 
      word_l.center = initPoint; 
     } else { 
      viewPositionMovingStep = 0; 
      return; 
     } 
    } 
} 

причина: При использовании свойства преобразования вы не можете изменить положение путем изменения кадра, вы должны использовать свойство центра.

Таким образом, эта часть была исправлена, но проблема слева связана с поворотным видом пересечения, проверка не работает должным образом, но тестирует ее отдельно в другом проекте, который преуспевает.

Там он:

- (BOOL)view:(UIView *)view1 intersectsWith:(UIView *)view2 
{ 
    CGPoint poly1[4]; 
    CGRect bounds1 = view1.bounds; 
    poly1[0] = [view1 convertPoint:bounds1.origin toView:nil]; 
    poly1[1] = [view1 convertPoint:CGPointMake(bounds1.origin.x + bounds1.size.width, bounds1.origin.y) toView:nil]; 
    poly1[2] = [view1 convertPoint:CGPointMake(bounds1.origin.x + bounds1.size.width, bounds1.origin.y + bounds1.size.height) toView:nil]; 
    poly1[3] = [view1 convertPoint:CGPointMake(bounds1.origin.x, bounds1.origin.y + bounds1.size.height) toView:nil]; 

    CGPoint poly2[4]; 
    CGRect bounds2 = view2.bounds; 
    poly2[0] = [view2 convertPoint:bounds2.origin toView:nil]; 
    poly2[1] = [view2 convertPoint:CGPointMake(bounds2.origin.x + bounds2.size.width, bounds2.origin.y) toView:nil]; 
    poly2[2] = [view2 convertPoint:CGPointMake(bounds2.origin.x + bounds2.size.width, bounds2.origin.y + bounds2.size.height) toView:nil]; 
    poly2[3] = [view2 convertPoint:CGPointMake(bounds2.origin.x, bounds2.origin.y + bounds2.size.height) toView:nil]; 

    CGPoint ctl2 = [view1 convertPoint:poly2[0] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, ctl2)){ 
     return YES; 
    } 
    CGPoint ctr2 = [view1 convertPoint:poly2[1] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, ctr2)){ 
     return YES; 
    } 
    CGPoint cbr2 = [view1 convertPoint:poly2[2] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, cbr2)){ 
     return YES; 
    } 
    CGPoint cbl2 = [view1 convertPoint:poly2[3] fromView:nil]; 
    if (CGRectContainsPoint(view1.bounds, cbl2)){ 
     return YES; 
    } 
    CGPoint ctl1 = [view2 convertPoint:poly1[0] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, ctl1)){ 
     return YES; 
    } 
    CGPoint ctr1 = [view2 convertPoint:poly1[1] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, ctr1)){ 
     return YES; 
    } 
    CGPoint cbr1 = [view2 convertPoint:poly1[2] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, cbr1)){ 
     return YES; 
    } 
    CGPoint cbl1 = [view2 convertPoint:poly1[3] fromView:nil]; 
    if (CGRectContainsPoint(view2.bounds, cbl1)){ 
     return YES; 
    } 
    return NO; 
} 

кто-то идеи?

Смежные вопросы