UIImageView + animatedGIF攻击动画

时间:2014-11-18 04:44:13

标签: ios animation uiimageview animated-gif

我已存储了号码。数组中的gif文件在uiimageview中执行动画,但是gif文件有时间没有动画。

有人可以向我解释一下我没有抓到的是什么吗? 感谢您的帮助。

-(void)viewDidLoad
{

   [super viewDidLoad];
   NSArray *stgImageArray = [[NSArray alloc]initWithObjects:@"baby1",@"ball1",@"balloon1",@"bell1",@"boypeeing1",@"bugle1",
"carsta1",@"chicken1",@"coughing1",@"dish1",@"doorbell1",@"drum1",@"eggfry1",
@"fireburn",@"footstep1",@"girlskip1",@"hairdry1",@"hammer1",
@"handsclap1",@"horn1",@"horserun1",@"keysjing1",@"knocking1",@"lion1",@"phonering1",
@"radio1",@"shower1",@"toilet1",@"vacuum1",@"water1",nil];

[self LoadBottomScrollView];
}

-(void)LoadBottomScrollView
{
[scrollView1.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

int count = 30;
NSLog(@"count = %d",count);
for(int k = 1; k <= count; k++)
{
    imageName = [NSString stringWithFormat:thumbImage,k];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    button.frame = CGRectMake(((k-1)*kScrollObjWidth), 
    0, kScrollObjWidth,kScrollObjHeight);
    [button setBackgroundImage:
    [UIImage imageNamed:imageName]forState:UIControlStateNormal];
    [button setTitle:[stgTextArray objectAtIndex:k-1] forState:UIControlStateNormal];
    button.titleEdgeInsets = UIEdgeInsetsMake(145, 0, 0, 0);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
    [button addTarget:self action:
    @selector(BottomButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = k;
    [scrollView1 addSubview:button];

}
//count*146
scrollView1.contentSize = CGSizeMake(kNumImages*kScrollObjWidth, scrollView1.frame.size.height);
}

-(void)BottomButtonAction:(id)sender
{

if ([sender tag] == 1)
{
    if (checkCount == YES)
    {
        counter = 0;
    }
}
if ([sender tag] == 2)
{
    if (checkCount == YES)
    {
        counter = 1;
    }
}
if ([sender tag] == 3)
{
    if (checkCount == YES)
    {
        counter = 2;
    }
}
if ([sender tag] == 4)
{
    if (checkCount == YES)
    {
        counter = 3;
    }
}
if ([sender tag] == 5)
{
    if (checkCount == YES)
    {
        counter = 4;
    }
}
if ([sender tag] == 6)
{
    if (checkCount == YES)
    {
        counter = 5;
    }
}
if ([sender tag] == 7)
{
    if (checkCount == YES)
    {
        counter = 6;
    }
}
if ([sender tag] == 8)
{
    if (checkCount == YES)
    {
        counter = 7;
    }
}
if ([sender tag] == 9)
{
    if (checkCount == YES)
    {
        counter = 8;
    }
}
if ([sender tag] == 10)
{
    if (checkCount == YES)
    {
        counter = 9;
    }
}

if ([sender tag] == 11)
{
    if (checkCount == YES)
    {
        counter = 10;
    }
}
if ([sender tag] == 12)
{
    if (checkCount == YES)
    {
        counter = 11;
    }
}
if ([sender tag] == 13)
{
    if (checkCount == YES)
    {
        counter = 12;
    }
}
if ([sender tag] == 14)
{
    if (checkCount == YES)
    {
        counter = 13;
    }
}
if ([sender tag] == 15)
{
    if (checkCount == YES)
    {
        counter = 14;
    }
}
if ([sender tag] == 16)
{
    if (checkCount == YES)
    {
        counter = 15;
    }
}
if ([sender tag] == 17)
{
    if (checkCount == YES)
    {
        counter = 16;
    }
}
if ([sender tag] == 18)
{
    if (checkCount == YES)
    {
        counter = 17;
    }
}
if ([sender tag] == 19)
{
    if (checkCount == YES)
    {
        counter = 18;
    }
}
if ([sender tag] == 20)
{
    if (checkCount == YES)
    {
        counter = 19;
    }
}

if ([sender tag] == 21)
{
    if (checkCount == YES)
    {
        counter = 20;
    }
}
if ([sender tag] == 22)
{
    if (checkCount == YES)
    {
        counter = 21;
    }
}
if ([sender tag] == 23)
{
    if (checkCount == YES)
    {
        counter = 22;
    }
}
if ([sender tag] == 24)
{
    if (checkCount == YES)
    {
        counter = 23;
    }
}
if ([sender tag] == 25)
{
    if (checkCount == YES)
    {
        counter = 24;
    }
}
if ([sender tag] == 26)
{
    if (checkCount == YES)
    {
        counter = 25;
    }
}
if ([sender tag] == 27)
{
    if (checkCount == YES)
    {
        counter = 26;
    }
}
if ([sender tag] == 28)
{
    if (checkCount == YES)
    {
        counter = 27;
    }
}
if ([sender tag] == 29)
{
    if (checkCount == YES)
    {
        counter = 28;
    }
}
if ([sender tag] == 30)
{
    if (checkCount == YES)
    {
        counter = 29;
    }
}

[self action:sender];
}
-(void) action:(id)sender
{
double delayInSeconds = 0.0;
[stgImageView stopAnimating];

    if(previoussender==[sender tag])
    {
        if(individualcount != numberofrepeatcount)
        {
            return;
        }
    }

if (player != nil) {
    [player stop];
    player = nil;
}

if(Isautoplay)
{
    if(counter== [stgImageArray count])
    {
        counter=0;
    }
    NSLog(@"counter = %d",counter);
}

if (stgImageView !=nil) {
    stgImageView.image = nil;
}

strImagePath = [stgImageArray objectAtIndex:counter];
[stgImageView setImage:[UIImage imageNamed:
[NSString stringWithFormat:@"%@_%d.png",strImagePath,index]]];

stgText.text= [NSString stringWithFormat:@"%@", [stgTextArray objectAtIndex:counter]];

if ([[[NSUserDefaults standardUserDefaults] 
valueForKey:@"soundSwitch"] isEqualToString:@"2"]) {


    NSString *soundpath = [[NSBundle mainBundle] pathForResource:[stgTextArray objectAtIndex:counter] ofType:@"wav"];
    [self audioPlayer:[NSURL fileURLWithPath:soundpath]];
    delayInSeconds = 1.4;

}
else
{
    delayInSeconds = 0.0;
}


dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){


    animationcount = 0;
    NSString *str = [aniImageCount objectAtIndex:counter];
    totalDuration = [str floatValue];
    numberofrepeatcount = 3;
    individualcount = 1;
    str = [aniDuration objectAtIndex:counter];
    countes = [str floatValue];

    if (stgImageView !=nil) {
        stgImageView.image = nil;
    }

    NSURL *url = [[NSBundle mainBundle] URLForResource:
   [stgImageArray objectAtIndex:counter] withExtension:@"gif"];
    stgImageView.image = [UIImage animatedImageWithAnimatedGIFData:
   [NSData dataWithContentsOfURL:url]];

    NSString *soundpath = [[NSBundle mainBundle] pathForResource:
   [aniSoundList objectAtIndex:counter] ofType:@"wav"];
    [self audioPlayer:[NSURL fileURLWithPath:soundpath]];
});

if(!Isautoplay)
{
    previoussender = [sender tag];
}
}

1 个答案:

答案 0 :(得分:0)

试试这个:

+ (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)data duration:(NSTimeInterval)duration {
    return animatedImageWithAnimatedGIFReleasingImageSource(CGImageSourceCreateWithData(toCF data, NULL));
}


static UIImage *animatedImageWithAnimatedGIFReleasingImageSource(CGImageSourceRef source) {
    UIImage *image = animatedImageWithAnimatedGIFImageSource(source);
    CFRelease(source);
    return image;
}



static UIImage *animatedImageWithAnimatedGIFImageSource(CGImageSourceRef source) {
//replace duration with ImageIO function
if (!source)
    return nil;

size_t count = CGImageSourceGetCount(source);
NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
NSTimeInterval time_duration = 0;
for (size_t i = 0; i < count; ++i) {
    CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, i, NULL);
    if (!cgImage)
        return nil;

    //get duration with ImageIO but not libgungif
    CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(source, i, NULL);
    if (properties) {
        CFDictionaryRef gifProperties = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
        if (gifProperties) {
            CFNumberRef number = CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFDelayTime);
            // Even though the GIF stores the delay as an integer number of centiseconds, ImageIO “helpfully” converts that to seconds for us.
            double delay = 0.f;
            CFNumberGetValue(number, kCFNumberDoubleType, &delay);
            delay = lrint(delay * 100.f);
            if (delay > 5.f) {
                time_duration += delay;
            }
            else {
                time_duration += 5.f;
            }
        }
        CFRelease(properties);
    }

    [images addObject:[UIImage imageWithCGImage:cgImage]];
    CGImageRelease(cgImage);
}

return [UIImage animatedImageWithImages:images duration:(time_duration / 100.f)];
}