Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在动画持续时间内限制移动_Iphone_Xcode_Animation - Fatal编程技术网

Iphone 如何在动画持续时间内限制移动

Iphone 如何在动画持续时间内限制移动,iphone,xcode,animation,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 s

我正在创建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);

}
谢谢你的建议

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

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

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

既然你有这个视图,你就知道它的大小了。通过知道宽度,您可以知道从左侧或右侧(宽度的一半)定位视图中心所需的距离。通过了解高度,您可以知道如何从顶部或底部(高度的一半)开始测量

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

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;

非常感谢您的回复!