我正在创建 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);
}
感谢您的任何建议!