iOS development Simple implementation loading animation effect

Source: Internet
Author: User

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];

  1. Uiimageview *imageview = [[Uiimageview alloc] initwithframe:cgrectmake (100, 200,   80, 30)];
  2. ImageView. animationimages = array; //Animation image array
  3. ImageView. animationduration = 2; //The length of time required to perform a full animation
  4. Gifimageview.animationrepeatcount = 0; Animation repeat 0 means unlimited, default is 0
  5. [ImageView startanimating];
  6. [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.

  1. #import <UIKit/UIKit.h>
  2. @interface Imageanimation:uiimageview
  3. @property (Strong, nonatomic) Nstimer *animation_timer;
  4. @property (Strong, nonatomic) Nsmutablearray *imagearray;
  5. @property (assign) int currentindex;
  6. @property (assign) float interval;
  7. -(void) startloading;
  8. -(void) stoploading;
  9. -(void) Initloadingview: (nsmutablearray *) Imagearray timeinterval: (float) time;
  10. @end

#import "ImageAnimation.h"

  1. @implementation Imageanimation
  2. @synthesize Animation_timer = _animation_timer;
  3. @synthesize Imagearray = _imagearray;
  4. @synthesize currentindex = _currentindex;
  5. @synthesize interval = _interval;
  6. -(ID) initWithFrame: (CGRect) Frame
  7. {
  8. Self = [super Initwithframe:frame];
  9. if (self) {
  10. //initialization code
  11. }
  12. return self ;
  13. }
  14. /*
  15. Only override Drawrect:if perform custom drawing.
  16. An empty implementation adversely affects performance during animation.
  17. -(void) DrawRect: (cgrect) rect
  18. {
  19. Drawing Code
  20. }
  21. */
  22. -(void) Initloadingview: (nsmutablearray *) Imagearray timeinterval: (float) time{
  23. self. Imagearray = Imagearray;
  24. self. Interval = time;
  25. self. Animation_timer = [Nstimer- scheduledtimerwithtimeinterval: Self. Interval target:   Self selector:@selector (startloading) userInfo: nil repeats:YES];
  26. }
  27. Start loading
  28. -(void) startloading{
  29. if (!  Self. Imagearray | | Self . Imagearray. Count < 1) {
  30. [self. Animation_timer invalidate];
  31. return;
  32. }
  33. self. Currentindex = (self. Currentindex +1)%self. Imagearray. Count;
  34. Self. image = [UIImage imagenamed:[-imagearray objectatindex: Self.  Currentindex]];
  35. }
  36. End loading
  37. -(void) stoploading{
  38. [self. Animation_timer invalidate];
  39. }
  40. @end


Method Three: Use UIWebView to load GIF pictures, unless you want to use WebView, otherwise do not use this way to implement

  1. NSData *gif = [NSData datawithcontentsoffile: [[NSBundle Mainbundle] pathforresource:@ "1" OfType  :@ "GIF"];
  2. View Generation
  3. UIWebView *webview = [[UIWebView alloc] initwithframe:cgrectmake (100, 100, 7  0, 30)];
  4. WebView. userinteractionenabled = NO; User is not interactive
  5. [WebView loaddata:gif MIMEType:@ "Image/gif" textencodingname: nil BaseURL: nil];
  6. [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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.