Recently a friend asked me like The voice play of the Horn animation and interface image loading loading interface is how to achieve, is not a GIF Picture It! My answer, of course, is no, of course, not to exclude someone with a GIF picture Ah! Here I will list three ways to achieve loading animation effect.
Method One: Using Uiimageview's own method to implement, this is also my recommended implementation method.
Nsmutablearray *array = [[Nsmutablearray alloc] initwithobjects:[uiimage imagenamed:@ "1.png"],[ UIImage imagenamed:@ "2.png"],[uiimage imagenamed:@ "3.png"],[uiimage imagenamed:@ "4.png"] , [UIImage imagenamed:@ "5.png"], nil nil];
- Uiimageview *imageview = [[Uiimageview alloc] initwithframe:cgrectmake (100, 200, 80, 30)];
- ImageView. animationimages = array; //Animation image array
- ImageView. animationduration = 2; //The length of time required to perform a full animation
- Gifimageview.animationrepeatcount = 0; Animation repeat 0 means unlimited, default is 0
- [ImageView startanimating];
- [self. View Addsubview:imageview];
Method Two: Use Uiimageview+nstimer (timer) to achieve, if not guessed wrong, the first method of internal implementation is also using the timer, but our second method is to DIY a self-owned
Control and expand what you want to extend.
- #import <UIKit/UIKit.h>
- @interface Imageanimation:uiimageview
- @property (Strong, nonatomic) Nstimer *animation_timer;
- @property (Strong, nonatomic) Nsmutablearray *imagearray;
- @property (assign) int currentindex;
- @property (assign) float interval;
- -(void) startloading;
- -(void) stoploading;
- -(void) Initloadingview: (nsmutablearray *) Imagearray timeinterval: (float) time;
- @end
#import "ImageAnimation.h"
- @implementation Imageanimation
- @synthesize Animation_timer = _animation_timer;
- @synthesize Imagearray = _imagearray;
- @synthesize currentindex = _currentindex;
- @synthesize interval = _interval;
- -(ID) initWithFrame: (CGRect) Frame
- {
- Self = [super Initwithframe:frame];
- if (self) {
- //initialization code
- }
- return self ;
- }
- /*
- Only override Drawrect:if perform custom drawing.
- An empty implementation adversely affects performance during animation.
- -(void) DrawRect: (cgrect) rect
- {
- Drawing Code
- }
- */
- -(void) Initloadingview: (nsmutablearray *) Imagearray timeinterval: (float) time{
- self. Imagearray = Imagearray;
- self. Interval = time;
- self. Animation_timer = [Nstimer- scheduledtimerwithtimeinterval: Self. Interval target: Self selector:@selector (startloading) userInfo: nil repeats:YES];
- }
- Start loading
- -(void) startloading{
- if (! Self. Imagearray | | Self . Imagearray. Count < 1) {
- [self. Animation_timer invalidate];
- return;
- }
- self. Currentindex = (self. Currentindex +1)%self. Imagearray. Count;
- Self. image = [UIImage imagenamed:[-imagearray objectatindex: Self. Currentindex]];
- }
- End loading
- -(void) stoploading{
- [self. Animation_timer invalidate];
- }
- @end
Method Three: Use UIWebView to load GIF pictures, unless you want to use WebView, otherwise do not use this way to implement
- NSData *gif = [NSData datawithcontentsoffile: [[NSBundle Mainbundle] pathforresource:@ "1" OfType :@ "GIF"];
- View Generation
- UIWebView *webview = [[UIWebView alloc] initwithframe:cgrectmake (100, 100, 7 0, 30)];
- WebView. userinteractionenabled = NO; User is not interactive
- [WebView loaddata:gif MIMEType:@ "Image/gif" textencodingname: nil BaseURL: nil];
- [self. View Addsubview:webview];
The above is three ways to achieve the process of playing animation, remember to use the time to pay attention to the release of memory, otherwise the cost will be very large! If you have any questions, or a better way, you are welcome to communicate with me.
iOS development Simple implementation loading animation effect