简单的 UIView 动画链不起作用

发布于 2024-10-02 16:54:00 字数 2119 浏览 0 评论 0原文

我正在尝试将 UIImageView 滑入 UIView 中,并同时将其淡入。然后我希望它淡出并滑出。 下面的代码应该执行此操作,但事实并非如此。 相反,leftArrow 视图在滑入时处于完整的 Alpha 状态。因此第一个动画会跳过 Alpha 淡入。然后第二个动画只是淡出它,它不会移动 leftArrow 视图。我尝试了很多变体(使用边界而不是框架,在类方法中执行动画(而不是实例方法),并且我无法确定为什么视图似乎任意选择一件事而不是另一件事,或两者兼而有之。 也许我只是需要新鲜的眼光。 TIA, 迈克

- (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:leftArrow];
    [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];

}

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:leftArrow];
    [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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-10-09 16:54:00

解决方案很简单 - 当第一个动画完成时,让它调用另一个方法,例如horizo​​ntal2,并在horizo​​ntal2中执行animate_them_out部分。

- (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]; 

}

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]; 

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文