【问题标题】:How can i make random UILabel and ImageView in a UIView?如何在 UIView 中制作随机的 UILabel 和 ImageView?
【发布时间】:2015-02-23 09:04:49
【问题描述】:

我想在 UIView 中制作具有特定 CGAffineTransform 和 UIImageView 的 UILabel。到目前为止,我已经尝试在自我中进行随机视图,但问题是一些视图在屏幕之外。这是我到目前为止完成的代码。

-(void)generateView {
   float viewWidth = self.view.frame.size.width;
   float viewHeight = self.view.frame.size.height;

   int numViews = 0;
   while (numViews < 2) {
      BOOL goodView = YES;
      UIView *candidateView = [[UIView alloc] initWithFrame:CGRectMake(arc4random() % (int)viewWidth, arc4random() % (int)viewHeight, 200, 100)];
      candidateView.backgroundColor = [UIColor redColor];
      for (UIView *placedView in self.view.subviews) {
         if (CGRectIntersectsRect(CGRectInset(candidateView.frame, -10, -10), placedView.frame)) {
            goodView = NO;
            break;
         }
      }
      if (goodView) {
         [self.view addSubview:candidateView];
         numViews += 1;
      }
   }
}

现在首先我的问题是如何让 self.view 中的所有视图在 self.view 中出现哪个框架,其次如果我想用特定的 CGAffineTransform 制作 UIView 那么我该怎么做呢?

【问题讨论】:

    标签: ios objective-c iphone uiview


    【解决方案1】:

    第一个问题的答案:

    UIView *candidateView = [[UIView alloc] initWithFrame:CGRectMake(arc4random() % ((int)viewWidth - 200), arc4random() % ((int)viewHeight - 100), 200, 100)];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2018-02-14
      • 2018-08-28
      相关资源
      最近更新 更多