ios 自訂載入動畫效果,ios自訂載入動畫
在開發過程中,可能會遇到各種不同的情境需要等待載入成功後才能顯示資料。以下是自訂的一個動畫載入view效果。
在UIViewController的中載入等到效果,如下
- (void)viewDidLoad { [super viewDidLoad]; //將view背景顏色變更為黃色 self.view.backgroundColor = [UIColor yellowColor]; //在self.view上載入提示框 [[BIDActivityNote sharedInstance] AddActivityView:self.view]; //延時3分鐘後移除提示框 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[BIDActivityNote sharedInstance] RemoveActivityView]; });}
BIDActivityNote.h 實現代碼
//// BIDActivityNote.h// MobileShop//// Created by eJiupi on 15-7-23.// Copyright (c) 2014年 xujinzhong. All rights reserved.//#import <Foundation/Foundation.h>@interface BIDActivityNote : NSObject+ (BIDActivityNote*)sharedInstance;- (void)AddActivityView:(UIView*)subView;- (void)RemoveActivityView;@end
BIDActivityNote.m 代碼實現效果:
//// BIDActivityNote.m// MobileShop//// Created by eJiupi on 15-7-23.// Copyright (c) 2014年 xujinzhong. All rights reserved.//#import "BIDActivityNote.h"@interface BIDActivityNote ()@property (strong, nonatomic) UIView *subView;@property (strong, nonatomic) UIActivityIndicatorView *act;@end@implementation BIDActivityNote+ (BIDActivityNote*)sharedInstance{ static BIDActivityNote* instance = nil; if (instance == nil) { instance = [[BIDActivityNote alloc] init]; } return instance;}- (id)init{ self = [super init]; if (self) { NSInteger w = [UIScreen mainScreen].bounds.size.width; NSInteger h = [UIScreen mainScreen].bounds.size.height; self.subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)]; self.subView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; //只能設定中心,不能設定大小 self.act.center = CGPointMake(w/2.f, h/2.f); //設定活動指標的顏色 self.act.color=[UIColor whiteColor]; [self.act startAnimating]; // 開始旋轉 [self.act stopAnimating]; // 結束旋轉 //[self.act setHidesWhenStopped:YES]; //當旋轉結束時隱藏 [self.subView addSubview:self.act]; } return self;}- (void)AddActivityView:(UIView*)subView{ //啟動 [self.act startAnimating]; [subView addSubview:self.subView]; //實現動畫效果 self.subView.transform = CGAffineTransformScale(self.subView.transform, 0, 0); [UIView animateWithDuration:2 animations:^{ self.subView.transform = CGAffineTransformIdentity; }]; }- (void)RemoveActivityView{ [UIView animateWithDuration:0.7 animations:^{ self.subView.transform = CGAffineTransformScale(self.subView.transform, 0, 0); } completion:^(BOOL bfinished){ if (bfinished) { //停止 [self.act stopAnimating]; [self.subView removeFromSuperview]; } }];}@end