EO Games and Apps
EO Games and Apps

Reputation: 3078

Resize an image but never came back to original size

I'm trying to resize an image (tiny and return to original size). This code is working for resize the image but never go back to the original size. I thin I miss something in the endItem1Animation code. Any suggestions? Thanks

if (CGRectIntersectsRect(image.frame, item1.frame)) {
    item1.hidden = YES;

    // Item 1 Animation (Go Tiny)

    [UIView beginAnimations:NULL context:NULL];
    [UIView setAnimationsEnabled:YES];
    [UIView setAnimationDelay:0];
    [UIView setAnimationDuration:0];
    [UIView setAnimationRepeatCount:0];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatAutoreverses:NO];

    CGRect newRect = image.frame;
    newRect.size = CGSizeMake(25,25);
    image.frame = newRect;
    [UIView commitAnimations];

    [self endItem1Animation];

This is the endItem1Animation code: // Item 1 End Animation (Return to Original Size)

    [UIView beginAnimations:NULL context:NULL];
    [UIView setAnimationsEnabled:YES];
    [UIView setAnimationDelay:0];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationRepeatCount:0];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatAutoreverses:NO];

    CGRect newRect = image.frame;
    newRect.size = CGSizeMake(50,50);
    image.frame = newRect;
    [UIView commitAnimations];

Upvotes: 0

Views: 96

Answers (2)

EO Games and Apps
EO Games and Apps

Reputation: 3078

I fix this issue running a selector at the end of the code. Now my code is working. Here is my final code, I Hope to help someone. Thank you for your suggestions.

if (CGRectIntersectsRect(image.frame, item1.frame)) { item1.hidden = YES;

// Item 1 Animation (Go Tiny)

[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];

CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];

[self performSelector:@selector(endItem1Animation) withObject:nil afterDelay:8.0];

Upvotes: 0

NigelG
NigelG

Reputation: 651

The problem is that when you call beginAnimations / endAnimation, this does not block the main thread from running, so calling [self endItem1Animation] sets the second animation which will prevent the first animation from running.

You'll want to use the UIView animation block method instead, which lets you supply a completion handler when the animation completes.

[UIView animateWithDuration:0.5 animations:^{
    CGRect newRect = image.frame;
    newRect.size = CGSizeMake(25,25);
    image.frame = newRect;
} completion:^(BOOL finished) {
    [self endItem1Animation];
}];

Another, more straightforward issue, is that the duration for the first animation is zero.

Upvotes: 1

Related Questions