我有一个名为platform1
,platform2
,platform3
等的UIImageView。我有一个名为int
的{{1}}来自一个方法(method1)另一种方法(方法2)。
方法2(内容对我的问题没用):
platformNumber
如何更改它,以便CGPoint origin = platform1.center;
CGPoint target = CGPointMake(platform1.center.x, platform1.center.y+10);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce.duration = 0.1;
bounce.fromValue = [NSNumber numberWithInt:origin.y];
bounce.toValue = [NSNumber numberWithInt:target.y];
bounce.repeatCount = 1;
bounce.autoreverses = YES;
[platform1.layer addAnimation:bounce forKey:@"position"];
。center.x出现时,它会更改为platform1
,具体取决于传递的platform[platformNumber]
值?
谢谢,非常感谢任何帮助!
答案 0 :(得分:0)
提出KVC的答案根本不具备可扩展性;请不要这样做。你不能在运行时声明一个变量,因此,无论你有多少图片视图都可以附加一个属性,它也会被卡住。
您需要构建一个图像视图数组并迭代它们。我再说一遍,KVC不打算这样做。
NSMutableArray *imageviews = [[NSMutableArray alloc] init];
//...
[imageViews addObject:platform1];
[imageViews addObject:platform2];
[imageViews addObject:platform3];
//...
然后迭代数组:
foreach (UIImageView *imgView in _imageViews) {
CGPoint origin = imgView.center;
CGPoint target = CGPointMake(imgView.center.x, imgView.center.y+10);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce.duration = 0.1;
bounce.fromValue = [NSNumber numberWithInt:origin.y];
bounce.toValue = [NSNumber numberWithInt:target.y];
bounce.repeatCount = 1;
bounce.autoreverses = YES;
[imgView.layer addAnimation:bounce forKey:@"position"];
}
int index = 3;
_imageViews[index]
在您的示例中返回platform2(数组从0开始,因此减去一个)
答案 1 :(得分:0)
或者你也可以创建一个函数
-(UIImageView*)getPlatformForNumber:(int)number {
switch(number)
{
case 1:
return platform1;
case 2:
return platform2;
...
}
// should not come here
return nil;
}
并在必要时调用该函数。有时候这个解决方案会派上用场 - 但davbryn解决方案中提出的阵列当然更具可扩展性。