简体   繁体   English

iOS:使对象阵列在屏幕上移动

[英]iOS: make array of objects move across the screen

I am trying to make multiple UILabels (using a for loop and inserting them into an array) move horizontally across the screen. 我试图使多个UILabel(使用for循环并将其插入到数组中)在屏幕上水平移动。 I have the labels, I have the array, but I cannot figure out how to use animations to get them to move. 我有标签,有数组,但是我无法弄清楚如何使用动画来移动它们。 I tried using [self.array setValue @0 forKey:@"XPOSITION"]; 我尝试使用[self.array setValue @0 forKey:@"XPOSITION"]; but I do not know how to write the x position. 但我不知道如何写x位置。 Is there no real way to change the x position in an array? 有没有真正的方法来更改数组中的x位置? Will I have to make each one its own individual thing and use object.frame = CGRectMake(newx,y,w,h); 我是否必须让每个人都有自己的东西并使用object.frame = CGRectMake(newx,y,w,h); which seems a bit tedious? 这似乎有点乏味? Thank you in advance! 先感谢您!

Following Aderis' advice about setting the center, you have to use a for loop to set the center of each label. 按照Aderis关于设置中心的建议,您必须使用for循环来设置每个标签的中心。

for (UILabel *label in self.array) {
    CGPoint oldCenter = label.center;
    label.center = CGPoint(oldCenter.x + 20, oldCenter.y);
}

Where you would change 20 to be whatever value you actually wanted to change the x coordinate by. 您将20更改为实际要更改x坐标所依据的任何值。

If you want to have it animated, you can use this: 如果要使其具有动画效果,可以使用以下方法:

[UIView animateWithDuration:0.2 animations:^{
    for (UILabel *label in self.array) {
        CGPoint oldCenter = label.center;
        label.center = CGPoint(oldCenter.x + 20, oldCenter.y);
    }
}];

Where you would replace 0.2 with the length in seconds you want the animation to last. 您将希望动画持续的秒数替换为0.2。

You should try using the builtin animation capabilities of UIView . 您应该尝试使用UIView的内置动画功能。 You can do the following code: 您可以执行以下代码:

[UIView beginAnimations:nil context:nil];

for (UIView* view in myArray)
{
    [view setCenter:thisViewsTargetCenterPoint];
}

[UIView commitAnimations];

In the for loop, you should do all the UI positioning changes you want, and once you call commitAnimations , UIKit will do all the position changes through animations. 在for循环中,您应该进行所需的所有UI定位更改,并且一旦调用commitAnimations ,UIKit将通过动画进行所有位置更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM