2

I have two UIImageViews named image1 and image2. What I want to do is: when image1 and image2 collide, image1 should grow larger( with a scale animation.) E.g. if image1 collides with image2 ten times , image1 should become 10 times bigger.

I try to use scale animation and CGRectIntersectsRect for the collision, then a timer for the collision but the image become larger just once time and and remains at that size after all subsequent collisions.

Here is my code :

-(void)collision {

    if(CGRectIntersectsRect(imageView.frame,centre.frame)){

        scale=scale+1;
        imageView.alpha=0.5;

        [imageView removeFromSuperview];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0f];
        centre.transform = CGAffineTransformScale(centre.transform, scale, scale);
        [UIView commitAnimations];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];
}

(TechZen says -- Author originally made a point that he is native French speaker and that his English is bit rough.)

TechZen
  • 64,370
  • 15
  • 118
  • 145
bernard langue
  • 147
  • 1
  • 10

1 Answers1

0

Transforms are not accumulative e.g. each transform operates on the original matrix and not the matrix returned by the last transform. That is why you image1 object transform once and then stays there. It is the exact same transform starting at the beginning over and over again.

Instead of trying to accumulate or sum the transforms, you need to accumulate or sum the scale variable. Make the scale variable a property of the controller object that holds the method and then progressively increment the value to make image1 larger.

Something like:

-(void)collision {

    if(CGRectIntersectsRect(imageView.frame,centre.frame)){

        self.scale=self.scale+1; // scale is no longer a local value and will increment
        imageView.alpha=0.5;

        [imageView removeFromSuperview];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0f];
        centre.transform = CGAffineTransformScale(centre.transform, scale, scale);
        [UIView commitAnimations];
    }
}

Update:

To convert a local variable i.e. one defined in a single method, you move the definition to header (.h) file like so:

@implementation MyViewController:NSViewController{
//... various definitions
}
@property NSInteger *scale;

... then in the implementation file (.m) add the synthesize directive:

@synthesize scale;

Now you can use self.scale to refer to scale property anywhere in the class and it will be preserved. Each time you call:

self.scale=self.scale + 1;

... the value will increment and will be saved so that it is available for the next collision.

Each time you call collision the scale value will change by one e.g.

centre.transform = CGAffineTransformScale(centre.transform, 1, 1);
...
centre.transform = CGAffineTransformScale(centre.transform, 2, 2);
....
centre.transform = CGAffineTransformScale(centre.transform, 3, 3);
//... and so on

... the CGAffineTransformScale will increasing transform the imageview to a large size.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • I don't really understand " Make the scale variable a property of the controller object that holds the method" can you explain please ? – bernard langue Jul 02 '11 at 09:05
  • there is a big problem : when centre collide with imageView, in one second imageView grows up exponetionaly very quickly and disappear – bernard langue Jul 02 '11 at 16:10