【问题标题】:How To Change a Variable Name Depending on Another Variable?如何根据另一个变量更改变量名称?
【发布时间】:2014-02-19 19:08:14
【问题描述】:

我有一个名为 platform1platform2platform3 等的 UIImageView。我有一个名为 platformNumberint 从一种方法(方法 1)进入另一种方法(方法 2)。

方法2(内容对我的问题没有用):

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"];

如何更改它,以便无论在哪里出现platform1.center.x,它都会根据传递的platformNumber 值更改为platform[platformNumber]

谢谢,任何帮助将不胜感激!

【问题讨论】:

  • 如果它们是对象的属性,则使用valueForKey,或者在开头进行切换:UIView*aPlateform; switch(thePlaftormNumber){case 1:aPlateform=plaform1;break;etc.}

标签: iphone variables methods int


【解决方案1】:

提出 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 开始,因此减一)

【讨论】:

  • 我的 method1 有一个 if 语句,“如果发生这种情况,则 platformNumber = 1”或“如果发生这种情况,则 platformNumber = 2”,然后我的 method2 读取 platformNumber 并相应地更改 method2。这还能用吗?
  • 该数组“连续”保存所有图像视图。所以 if (x) platformNumber =1 { UIImageView *img = _imageViews[0];} 请记住,数组从第 0 个索引开始索引,而我们人类从 1 开始。所以 platform1 在位置 0,platform2 在 1 等
  • 感谢 Davbryn 的帮助,不幸的是,我对 iOS 开发和编程还很陌生(但我已经了解了数组是什么),并且不太了解您的解决方案中发生了什么。如果您有空闲时间,我可以将此讨论转移到聊天吗?我真的很感激:)
  • 在我的第二种方法中,我做了:'-(void)method2:(int *)levelNo { //ARRAY NSMutableArray *imageViews = [[NSMutableArray alloc] init]; [imageViews addObject:platform1]; [imageViews addObject:platform2]; [imageViews addObject:platform3]; //ARRAY for (UIImageView *imgView in imageViews) { ... } int index = 3;图像视图[索引];'对于每个“如果”条件,我在第一种方法中该怎么做? Make levelNo change?
【解决方案2】:

或者你也可以创建一个函数

-(UIImageView*)getPlatformForNumber:(int)number {
    switch(number)
    {
        case 1:
            return platform1;

        case 2:
            return platform2;

        ...
    }

    // should not come here
    return nil;
}

并在必要时调用该函数。有时该解决方案会派上用场 - 但 davbryn 解决方案中提出的数组当然更具可扩展性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多