使用UIImageView播放动画

来源:互联网 发布:狮虎兽可以繁殖吗 知乎 编辑:程序博客网 时间:2024/04/30 13:17

问题:如何使用UIImageView播放动画,并停留在之后一张图片 

思路:

除了把动画所需要的几张图片赋值给 animationImages 之外,
多加一步 ,把最后一张图片赋值给UIImageView的  Image就好了。
让后就开始Animations。
 

 

代码如下:

 

              UIImageView *fishAni=[[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

复制代码
    //将指定的图片载入 animationImages
    fishAni.animationImages=[NSArray arrayWithObjects:
                             [UIImage imageNamed:@"13.png"],
                             [UIImage imageNamed:@"12.png"],
                             [UIImage imageNamed:@"11.png"],
                             [UIImage imageNamed:@"10.png"],nil ];
    [fishAni setImage:[UIImage imageNamed:@"10.png"]];
    
    //设定动画的播放时间
    fishAni.animationDuration=1.0;
    //设定重复播放次数
    fishAni.animationRepeatCount=1;
    
    //开始播放动画
    [fishAni startAnimating];
    
    [self.view addSubview:fishAni];

 

 当播放器播放完后,要执行的事件,可以添加如下代码:

    NSInteger AnimationNtimer =1;

NSTimer *animationTimer = [NSTimer scheduledTimerWithTimeInterval: AnimationNtimer target:self selector:@selector(ArrowAnimationPlay:) userInfo:nil repeats: NO];

 播放结束后的事件。

-(void)ArrowAnimationPlay:(NSTimer *) timer{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"wtq" message:@"I have a message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}

颜色变换:

做一个UIImage的category,直接上代码:

  1. - (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor   
  2.                            shadeAlpha1:(CGFloat)alpha1   
  3.                            shadeAlpha2:(CGFloat)alpha2  
  4.                            shadeAlpha3:(CGFloat)alpha3   
  5.                            shadowColor:(UIColor *)shadowColor   
  6.                           shadowOffset:(CGSize)shadowOffset   
  7.                             shadowBlur:(CGFloat)shadowBlur {   
  8.     UIImage *image = self;  
  9.     CGColorRef cgColor = [bgColor CGColor];  
  10.     CGColorRef cgShadowColor = [shadowColor CGColor];  
  11.     CGFloat components[16] = {1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3};  
  12.     CGFloat locations[4] = {0,0.5,0.6,1};  
  13.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();    
  14.     CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)4);  
  15.     CGRect contextRect;  
  16.     contextRect.origin.x = 0.0f;  
  17.     contextRect.origin.y = 0.0f;  
  18.     contextRect.size = [image size];  
  19.     //contextRect.size = CGSizeMake([image size].width+5,[image size].height+5);    
  20.     // Retrieve source image and begin image context  
  21.     UIImage *itemImage = image;  
  22.     CGSize itemImageSize = [itemImage size];  
  23.     CGPoint itemImagePosition;   
  24.     itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);  
  25.     itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);  
  26.     UIGraphicsBeginImageContext(contextRect.size);  
  27.     CGContextRef c = UIGraphicsGetCurrentContext();  
  28.     // Setup shadow  
  29.     CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);  
  30.     // Setup transparency layer and clip to mask  
  31.     CGContextBeginTransparencyLayer(c, NULL);  
  32.     CGContextScaleCTM(c, 1.0, -1.0);  
  33.     CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);  
  34.     // Fill and end the transparency layer  
  35.     CGContextSetFillColorWithColor(c, cgColor);       
  36.     contextRect.size.height = -contextRect.size.height;  
  37.     CGContextFillRect(c, contextRect);  
  38.     CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0);  
  39.     CGContextEndTransparencyLayer(c);  
  40.     //CGPointMake(contextRect.size.width*3.0/4.0, 0)  
  41.     // Set selected image and end context  
  42.     UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();  
  43.     UIGraphicsEndImageContext();  
  44.     CGColorSpaceRelease(colorSpace);  
  45.     CGGradientRelease(colorGradient);  
  46.     return resultImage;  
  47. }  
 

用法如下:

  1. UIImage *niceImage = [[UIImage imageNamed:@"image_name"] imageWithBackgroundColor:[UIColor colorWithRed:41.0/255.0 green:147.0/255.0 blue:239.0/255.0 alpha:1.0]   
  2.                                                                       shadeAlpha1:0.6   
  3.                                                                       shadeAlpha2:0.0   
  4.                                                                       shadeAlpha3:0.4   
  5.                                                                       shadowColor:[UIColor blackColor]   
  6.                                                                      shadowOffset:CGSizeMake(0.0f, -1.0f)    
  7.                                                                        shadowBlur:3.0];   



原创粉丝点击