开发者

simple UIView animation chain not working

开发者 https://www.devze.com 2023-01-25 11:14 出处:网络
I\'m trying to slide a UIImageView into a UIView, and fade it in at the same time.And then I want it to fade and slide out.

I'm trying to slide a UIImageView into a UIView, and fade it in at the same time. And then I want it to fade and slide out. The code below should do this, but it doesn't. Instead, the leftArrow view is at full alpha when it slides in. So the first animation is skipping the alpha fade-in. Then the second animation just fades it out, it does not move the leftArrow view. I've tried lots of variations (using bounds instead of frames, doing the animation in a class method (as opposed to an instance method), and I cannot determine why the view seems to arbitrarily pick one thing to do rather than the other, or both. Maybe I just need fresh eyes. TIA, Mike

- (void) flashHorizontalArrowsForView: (UIView *)view {
DebugLog(@" flashHorizontalArrows");


    float duration = 1.5f;

    // create the views
    UIImageView *leftArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftCouponDetailArrow.png"]];
    leftArrow.frame = CGRectMake(0 -leftArrow.image.size.width, 
                                 HORIZONTAL_ARROW_START_Y, 
                                 leftArrow.image.size.width, 
                                 leftArrow.image.size.height);

    [view addSubview:leftArrow];
    leftArrow.alpha =  0.0f;

    // animate them in...
    [UIView beginAnimations:@"foo" context:lef开发者_开发知识库tArrow];
    [UIView setAnimationDelay:    0.0f ];       // in seconds   
    [UIView setAnimationDuration: duration ];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDidStopSelector:@selector(animationReallyDidStop:finished:context:)];
    [UIView setAnimationDelegate:self];

    leftArrow.alpha = 1.0f;

    CGRect LFrame = leftArrow.frame;
    LFrame.origin.x += LFrame.size.width;  // move it in from the left.
    leftArrow.frame = LFrame;

    [UIView commitAnimations];

    // and animate them out
    // delay enough for the first one to finish  
    [UIView beginAnimations:@"bar" context:leftArrow];
    [UIView setAnimationDelay:    duration+0.05 ];       // in seconds  
    [UIView setAnimationDuration: duration ];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDidStopSelector:@selector(animationReallyDidStop:finished:context:)];
    [UIView setAnimationDelegate:self];
    leftArrow.alpha  = 0.0f;

    CGRect LLFrame = leftArrow.bounds;
    LLFrame.origin.x -= LLFrame.size.width;  // move it off to the left.
    leftArrow.bounds = LLFrame;

    [UIView commitAnimations];

}


Solution is simple - when the first animation finishes, have it call another method, like horizontal2, and in horizontal2, do the animate_them_out part.

- (void) flashHorizontalArrowsForView: (UIView *)view{
DebugLog(@" flashHorizontalArrows");
self.theView = view;

float duration = 1.5f;

// create the views
UIImageView *left = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftCouponDetailArrow.png"]];
self.leftArrow = left;
[left release];
leftArrow.tag = LEFT_ARROW_TAG;
leftArrow.frame = CGRectMake(0 -leftArrow.image.size.width, 
                             HORIZONTAL_ARROW_START_Y, 
                             leftArrow.image.size.width, 
                             leftArrow.image.size.height);

[theView addSubview:leftArrow];
leftArrow.alpha =  0.0f;

UIImageView *right = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightCouponDetailArrow.png"]];
self.rightArrow = right;
[right release];
rightArrow.tag = RIGHT_ARROW_TAG;
rightArrow.frame = CGRectMake(view.frame.size.width, // -leftArrow.image.size.width, 
                             HORIZONTAL_ARROW_START_Y, 
                             leftArrow.image.size.width, 
                             leftArrow.image.size.height);

[theView addSubview:rightArrow];
rightArrow.alpha =  0.0f;

// --------------------------------------------
// animate them in...
[UIView beginAnimations:@"foo" context:nil];
[UIView setAnimationDelay:    0.0f ];       // in seconds   
[UIView setAnimationDuration: duration ];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDidStopSelector:@selector(horizontalPart2)];
[UIView setAnimationDelegate:self];

leftArrow.alpha  = 1.0f;
rightArrow.alpha = 1.0f;

CGRect LFrame = leftArrow.frame;
LFrame.origin.x += LFrame.size.width;  // move it in from the left.
leftArrow.frame = LFrame;

CGRect RFrame = rightArrow.frame;
RFrame.origin.x -= RFrame.size.width;  // move it in from the right.
rightArrow.frame = RFrame;
[UIView commitAnimations];

}

 - (void) horizontalPart2 {

// --------------------------------------------
// and animate them out
// delay enough for the first one to finish  
[UIView beginAnimations:@"bar" context:nil];
[UIView setAnimationDelay:    1.0f ];       // in seconds   
[UIView setAnimationDuration: 3.0f ];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDidStopSelector:@selector(horizontalAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];


CGRect LLFrame = leftArrow.frame;
LLFrame.origin.x -= LLFrame.size.width;  // move it off to the left.  // THIS IS NOT HAPPENING.
leftArrow.frame = LLFrame;

CGRect RFrame = rightArrow.frame;
RFrame.origin.x += RFrame.size.width;
rightArrow.frame = RFrame;

leftArrow.alpha   = 0.0f;
rightArrow.alpha  = 0.0f;

[UIView commitAnimations]; 

}

0

精彩评论

暂无评论...
验证码 换一张
取 消