简体   繁体   English

用动画替换静态UIImageView

[英]Replacing static UIImageView with animation

I have a UImageView which i have set up in interface builder with a png (pair of eyes) from my resources. 我有一个UImageView ,它是在我的资源中用png(双眼)在界面生成器中设置的。 I then want to replace this image (after a specific amount of time) with an animation of eyes blinking. 然后,我想用眨眼的动画替换此图像(在特定的时间后)。

This is the code i have used which is called in viewWillAppear : 这是我使用过的在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];

In viewWillAppear I use an NSTimer to trigger the animation every 5 seconds: 在viewWillAppear中,我使用NSTimer每5秒触发一次动画:

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

The problem is, when i run the application i don't see the initial static image at all. 问题是,当我运行该应用程序时,我根本看不到初始静态图像。 I just see the animation every 5 seconds but no image of the open eyes in between these animations. 我仅每5秒看到一次动画,但是在这些动画之间没有睁开眼睛的图像。 Can anyone please help me resolve this issue or point me in the right direction? 谁能帮我解决这个问题或为我指明正确的方向? Thanks. 谢谢。

Add the animation images after 5.0 seconds. 5.0秒后添加动画图像。 From the UIImageView docs: 从UIImageView文档:

The array must contain UIImage objects. 该数组必须包含UIImage对象。 You may use the same image object more than once in the array. 您可以在数组中多次使用同一个图像对象。 Setting this property to a value other than nil hides the image represented by the image property. 将此属性设置为nil以外的其他值将隐藏由image属性表示的图像。 The value of this property is nil by default. 默认情况下,此属性的值为nil。

If you set the animationImages array beforehand, it won't display the image. 如果您事先设置了animationImages数组,它将不会显示图像。

EDIT: (All using ARC) 编辑:(全部使用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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM