【问题标题】:Objective-c animation block doesn't seem to fireObjective-c 动画块似乎没有触发
【发布时间】:2010-07-08 15:06:43
【问题描述】:

我的 UIView 子类“Card”中有一个方法可以通过将其 alpha 从 1 淡化为 0 来关闭实例。当我将卡片添加到超级视图时,我会执行类似的操作,并且它会淡入淡出。但是当我调用 fadeAway 时,它会立即消失。演示代码在我的控制器类中。这是我的 Card.h 和 Card.m


#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@class Card;

@interface Card : UIView {

 int            upperOperand;
    int         lowerOperand;
    NSString*   theOperator;
    int         theResult;
}

@property(nonatomic) int upperOperand;
@property(nonatomic) int lowerOperand;
@property(nonatomic, retain) NSString* theOperator;
@property(nonatomic) int theResult;

- (void)fadeAway;

@end

#import "Card.h"

@implementation Card

@synthesize upperOperand;
@synthesize lowerOperand;
@synthesize theOperator;
@synthesize theResult;

- (void)fadeAway {
    self.alpha = 1.0f;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:2.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    self.alpha = 0.0f;
    [UIView commitAnimations];  

    [self removeFromSuperview];
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        self.backgroundColor = [UIColor redColor];
        self.layer.cornerRadius = 15;
        self.alpha = 1.0;
        self.layer.borderColor = [[UIColor blueColor] CGColor];
        self.layer.borderWidth = 4;
    }
    return self;

   - (void)dealloc {
    [super dealloc];
}

@end

【问题讨论】:

    标签: iphone objective-c xcode animation uiview


    【解决方案1】:

    由于您要立即从其超级视图中删除 self,我认为它没有机会执行动画。

    您可能想查看setAnimationDidStopSelector:。这里有一个讨论:Animation End Callback for CALayer?

    另一个想法是延迟删除子视图。例如,

    [self performSelector:@selector(removeFromSuperview) 
        withObject:nil 
        afterDelay:2.0f];
    

    【讨论】:

    • 您是说[UIView commitAnimations] 行只是开始 动画并且它立即进入下一行并删除它?这对我来说很有意义。我以前见过 setAnimationDidStopSelector: ,但还没有尝试使用它。我认为这意味着另一种方法 - 一种用于制作动画,另一种用于在完成后移除视图,对吧?
    • 我认为 Kalle 的回答是正确的,将您的清理工作推迟到 animationDidStopselector:.
    • 没错。那,或者您可以使用与动画匹配的延迟来延迟删除。我更新了上面的答案以包含此方法的示例。
    • 再次感谢 - 我使用了 setAnimationDidStopSelector: 它就像一个魅力!让这些动画工作是非常令人满意的 - 真的装扮了程序。
    猜你喜欢
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2015-06-11
    • 2016-11-09
    相关资源
    最近更新 更多