【问题标题】:Replacing static UIImageView with animation用动画替换静态 UIImageView
【发布时间】:2012-06-11 11:19:03
【问题描述】:

我有一个UImageView,我在界面生成器中设置了一个来自我的资源的 png(一双眼睛)。然后我想用眨眼的动画替换这个图像(在特定时间后)。

这是我在viewWillAppear中调用的代码:

NSString *fileName; 
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    for(int i = 1; i < 12; i++) {
        fileName = [NSString stringWithFormat:@"HDBlinkPage1/hd_eyes_blinking%d.png", i];
        [imageArray addObject:[UIImage imageNamed:fileName]];
    }
    imgHDBlink.userInteractionEnabled = YES;
    imgHDBlink.animationImages = imageArray;
    imgHDBlink.animationDuration = 0.9;
    imgHDBlink.animationRepeatCount = 1;
    imgHDBlink.contentMode = UIViewContentModeScaleToFill;
    //[self.view addSubview:imgHDBlink];
    [imgHDBlink startAnimating];

在 viewWillAppear 中,我使用 NSTimer 每 5 秒触发一次动画:

[NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(blinkAnimation)
                                   userInfo:nil
                                    repeats:YES];

问题是,当我运行应用程序时,我根本看不到初始静态图像。我只是每 5 秒看到一次动画,但在这些动画之间没有睁开眼睛的图像。任何人都可以帮我解决这个问题或指出正确的方向吗?谢谢。

【问题讨论】:

    标签: iphone objective-c ios animation uiimageview


    【解决方案1】:

    在 5.0 秒后添加动画图像。来自 UIImageView 文档:

    数组必须包含 UIImage 对象。您可以在数组中多次使用相同的图像对象。将此属性设置为 nil 以外的值会隐藏由 image 属性表示的图像。该属性的值默认为 nil。

    如果你事先设置了animationImages数组,它不会显示图像。

    编辑: (全部使用 ARC)

    - (void) viewDidLoad {
      [super viewDidLoad];
    
      //Initialize self.imgHDBlink
    }
    
    - (void) viewDidAppear: (BOOL) animated {
        [super viewDidAppear: animated];
    
        self.imgHDBlink.image = [UIImage imageNamed: @"static_image"];
    
        [NSTimer scheduledTimerWithTimeInterval: 5.0
                                         target: self
                                       selector: @selector(blinkAnimation:)
                                       userInfo: nil
                                        repeats: YES];
    }
    
    - (void) blinkAnimation: (NSTimer*) timer {
    
        self.imgHDBlink.animationImages = [NSArray array];  //Actually add your images here
        [self.imgHDBlink startAnimating];
    
        [self.imgHDBlink performSelector: @selector(setAnimationImages:) withObject: nil afterDelay: self.imgHDBlink.animationDuration];
    }
    
    
    
    //Remember this to stop crashes if we are dealloced
    - (void) dealloc {
        [NSObject cancelPreviousPerformRequestsWithTarget: self 
                                                 selector: @selector(blinkAnimation:) 
                                                   object: nil];
    }
    

    【讨论】:

    • 那么,你能再让我运行一下吗?我的印象是我在 5 秒后添加了动画图像?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多