Results 1 to 3 of 3

Your favorite Apple, iPhone, iPad, iOS, Jailbreak, and Cydia site.

Thread: A question about objects

  1. #1
    Talking A question about objects
    Hi guys! I am a newbie regarding to objective-c programming. I come from programming in other languages and I have a question for you... so please be nice to me, as I may say something fool about objective-c.

    What I want to do is:

    1) create an image moving from left to right. When the image reaches the right it vanishes and appears on the left. Like a repetitive animation. While the image is moving I want to be able to detect any click on that image. If the image is clicked it must vanish. Any image created must be added to a MutableArray called MyArray.

    What code I have right now...

    -(void)awakeFromNib {
    [self drawImage];

    - (void)drawImage {

    UIImageView *picture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]]; = CGPointMake(130.0, 0.0);

    picture.userInteractionEnabled = YES;
    [self addSubview: picture];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatCount:1e100f];

    CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 450.0);

    picture.transform = transform;
    [UIView commitAnimations];
    [myArray addObjecticture];

    The last line on the previous code, in therory adds the image object created to myArray. So, after creating N images, I suppose I have an array pointing to all images I have created. Now my problems begin...

    Inspecting myArray with the debugger, I see it contains 0 objects, even if I add 10, 20, 100 objects.

    Look at the following code I use to detect the touch

    - (void)touchesBeganNSSet *)touches withEventUIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    for (UIImageView *anImage in myArray) {
    if (CGRectContainsPoint([anImage frame], [touch locationInView:self])) {
    NSLog(@"an image was touched");
    [anImage release];

    the problem with this code is that the inner IF is never reached as I think myArray is empty...

    So, here comes the question...

    Why is myArray empty and what am I doing wrong?

    thanks for any help.

  2. #2
    Where did you initialize the variable myArray?
    Where did you declare it?
    Starlight Computer Wizardry
    Pocket-sized Development
    Follow me on twitter: @NetMage

  3. #3
    Default good question
    Good question of yours! That was the problem!
    Now it is working!

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts