如何限制animateWithDuration中的移动

时间:2012-10-26 19:16:18

标签: iphone xcode animation

我正在创建应用程序,其中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);

}

感谢您的任何建议!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;