如何在UIView中制作随机UILabel和ImageView?

时间:2015-02-22 13:26:51

标签: ios objective-c iphone uiview

我想在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,那我该如何制作呢?

1 个答案:

答案 0 :(得分:0)

回答第一个问题:

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