Objective-C: setValue: value forKey: xposition

| | August 10, 2015

I am trying to create an “animation” effect for some objects in an NSMutableArray. I have it set up so every 2 seconds, the objects change their x value (I do not want to do an actual animation so please do not suggest one!):

- (void)jumpLeft {
    [self performSelector:@selector(jumpRight) withObject:nil afterDelay: 2.0];

    for (int i = 0; i < [self.jumpArray count]; i++) {
        [self.jumpArray[i] setValue:@(30*i) forKey: "XPOSITION"];
    }
}

- (void)jumpRight {
    [self performSelector:@selector(jumpLeft) withObject:nil afterDelay: 2.0];

    for (int i = 0; i < [self.jumpArray count]; i++) {
        [self.jumpArray[i] setValue:@(20*i) forKey: "XPOSITION"];
    }
}

Assume it continues throughout the game. My question is, what do I enter to make the x position change? I tried looking at what people have said, and I have not found anything. I also tried doing self.jumpArray[i].center = … but it says “Property not found on object of type ‘id’. Any suggestions?

Anything helps! Thank you in advance!

One Response to “Objective-C: setValue: value forKey: xposition”

  1. Assuming self.jumpArray is an array of UIView objects, you can set the center using the setter method instead of using the property syntax:

    [self.jumpArray[i] setCenter:somePointValue];
    

    Another option is to make your code more readable:

    UIView *view = self.jumpArray[i];
    view.center = somePointValue;
    

    You can also use fast enumeration:

    for (UIView *view in self.jumpArray) {
        view.center = somePointValue;
    }
    

Leave a Reply