1

我正在创建 UILabel 对象将在屏幕上移动的应用程序。我的问题是如何限制标签越过特定边界。例如,如果标签有一条消息,我想让整个消​​息可见,而不仅仅是它的第一部分。这是代码:

#define kHeight     320.0
#define kWidth          400.0
#define kTransitionDuration 1.50
#define kTopPlacement       80.0

- (void)myMover {


for (UIView *view in self.view.subviews) {
      if( [view isKindOfClass:[UILabel class]]){
    [UIView animateWithDuration:4.0 animations:^{  

            //set the point from where the move will start
            [self setRandomLocationForLabel:view];

        }];
    }

}

}


- (void) setRandomLocationForView:(UIView *)view
 {
  [view sizeToFit];      
   CGRect messageViewBounds = CGRectMake(round((self.view.bounds.size.width - kWidth) / 2.0),
                          200, kWidth, kHeight);

CGRect newFrame = CGRectMake( 0, 300, 100 , 20 );
while (view.frame.size.width > kWidth) {

    newFrame.size.width /= 2;
    newFrame.size.height /= 2;

}


view.frame = newFrame;

CGFloat x = (CGFloat) (arc4random() % (int) messageViewBounds.size.width + view.frame.size.width/2);
CGFloat y = (CGFloat) (arc4random() % (int) messageViewBounds.size.height + view.frame.size.height/2);


view.center = CGPointMake (x,y);

}

感谢您的任何建议!

4

2 回答 2

1

我的问题是如何限制标签越过特定边界。例如,如果标签有一条消息,我想让整个消​​息可见,而不仅仅是它的第一部分。

在确定标签应落在的随机位置时,您需要考虑标签的长度/宽度和高度。因此,您的随机选择应该落在

CGSize labelSize = [messageString sizeWithFont:messageString.font 
                             constrainedToSize:maximumLabelSize 
                                 lineBreakMode:messageString.lineBreakMode];

CGFloat minX = 0;
CGFloat minY = 0;
CGFloat maxX = self.view.frame.size.width - labelSize.width;
CGFloat maxY = self.view.frame.size.height - labelSize.height;

// Use minX/Y and maxX/Y in your random co-ordinate algorithm
于 2012-10-26T20:42:55.640 回答
1

既然你有视图,你就知道它的大小。通过了解宽度,您知道需要将视图中心从左侧或右侧(宽度的一半)定位多远。通过了解高度,您就知道如何从顶部或底部(高度的一半)。

现在,您可以通过获取完整视图来计算仅包含该视图的有效中心点的矩形,并创建一个插入矩形,在左右两侧减去一半的视图宽度,在顶部减去一半的视图高度和底部。

CGSize viewSize = view.bounds.size; // The view you are positioning
CGRect rectOfValidCenters = CGRectInset(self.view.bounds, // The view you are placing it in
                                        viewSize.width/2.0, // subtract the width/2 from left and right  
                                        viewSize.height/2.0); // subtract the height/2 form top and bottom
CGFloat randomX = // generate random value from 0.0 to 1.0
CGFloat randomY = // generate random value from 0.0 to 1.0

// Random valid center point is: 
//  ( minX + randomX * width , minY + randomY * height)
//
// if x and y are zero then the view's center will be in the upper left
// of the rect of valid centers (making its upper left corner be in the
// top left of the view it's placed in).
// if x and y are one then the view's center will be in the lower right
// of the rect of valid centers (making its lower right corner be in the
// lower right of the view it's placed in).
CGPoint randomValidPoint = CGPointMake(CGRectGetMinX(rectOfValidCenters) + randomX * CGRectGetWidth(rectOfValidCenters),
                                       CGRectGetMinY(rectOfValidCenters) + randomY * CGRectGetHeight(rectOfValidCenters));
view.center = randomValidPoint;
于 2012-10-26T21:02:29.993 回答