效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UIActivityIndicatorView *activityIndicatorView; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)playDidPush:(UIBarButtonItem *)sender; 6 - (void)pauseDidPush:(UIBarButtonItem *)sender; 7 - (void)changeDidPush:(UIBarButtonItem *)sender; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 [self layoutUI]; 16 } 17 18 - (void)didReceiveMemoryWarning { 19 [super didReceiveMemoryWarning]; 20 // Dispose of any resources that can be recreated. 21 } 22 23 - (void)viewWillAppear:(BOOL)animated { 24 [super viewWillAppear:animated]; 25 [self.navigationController setNavigationBarHidden:NO animated:animated]; 26 [self.navigationController setToolbarHidden:NO animated:animated]; 27 } 28 29 - (void)layoutUI { 30 self.view.backgroundColor = [UIColor lightGrayColor]; 31 self.navigationItem.title = @"实现一个播放器的活动指示器"; 32 33 UIBarButtonItem *barBtnPlay = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playDidPush:)]; 34 UIBarButtonItem *barBtnPause = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(pauseDidPush:)]; 35 UIBarButtonItem *barBtnFastForward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(changeDidPush:)]; 36 [self setToolbarItems:@[barBtnPlay, barBtnPause, barBtnFastForward]]; 37 38 //设置活动指示器视图 39 _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; //设置活动指示器视图样式;默认值是UIActivityIndicatorViewStyleWhite,设置的枚举值决定了活动指示器loading图片的宽高大小 40 _activityIndicatorView.layer.borderColor = [UIColor redColor].CGColor; 41 _activityIndicatorView.layer.borderWidth = 2.0; 42 [self.view addSubview:_activityIndicatorView]; 43 44 /* 官方定义内容,仅供查看 45 typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) { 46 UIActivityIndicatorViewStyleWhiteLarge, 47 UIActivityIndicatorViewStyleWhite, 48 UIActivityIndicatorViewStyleGray, 49 }; 50 */ 51 } 52 53 - (void)playDidPush:(UIBarButtonItem *)sender { 54 if (_activityIndicatorView.activityIndicatorViewStyle == UIActivityIndicatorViewStyleWhiteLarge) { 55 _activityIndicatorView.frame = CGRectMake(0, 0, 100, 100); 56 } else { 57 _activityIndicatorView.frame = CGRectMake(0, 0, 50, 50); 58 } 59 _activityIndicatorView.center = self.view.center; 60 [_activityIndicatorView startAnimating]; 61 } 62 63 - (void)pauseDidPush:(UIBarButtonItem *)sender { 64 _activityIndicatorView.hidesWhenStopped = NO; //设置活动指示器视图是否停止后隐藏;默认值是YES 65 [_activityIndicatorView stopAnimating]; 66 } 67 68 - (void)changeDidPush:(UIBarButtonItem *)sender { 69 [self pauseDidPush:sender]; 70 if (++_activityIndicatorView.activityIndicatorViewStyle > UIActivityIndicatorViewStyleGray) { 71 _activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 72 } 73 [self playDidPush:sender]; 74 } 75 76 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
时间: 2024-12-19 22:11:40