iOS 菊花效果

该博客介绍了如何在iOS应用中使用MBProgressHUD库来展示菊花加载效果。首先,需要引入MBProgressHUD头文件并创建按钮和标签。然后,在按钮点击事件中初始化MBProgressHUD,设置其颜色和是否遮罩背景,并在新线程中执行任务。通过代理方法控制菊花显示和隐藏,实现加载动画。同时,展示了如何在加载完成时跳转到下一个页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先下载第三方MBProgressHUD并且引入MBProgressHUD.和MBProgressHUD.m:


//  RootViewController.m

//  MBProgressDemo

//

//  Created by Dubai on 15/4/13.

//  Copyright (c) 2015 Dubai. All rights reserved.

//


#import "RootViewController.h"


#import "MBProgressHUD.h"


#import "SecondViewController.h"//第二个页面



@interface RootViewController ()<MBProgressHUDDelegate>



@property (strong, nonatomic) UIButton *changeBtn;//改变颜色


@property (strong, nonatomic) UIButton *playBtn;//能否有交互


@property (strong, nonatomic) UILabel *labelOne;


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    

    self.changeBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];

    self.changeBtn.frame = CGRectMake(60, 100, 60, 40);

    [self.changeBtn setTitle:@"改变" forState:(UIControlStateNormal)];

    [self.changeBtn addTarget:self action:@selector(didChangeBtnActionL:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:self.changeBtn];

    

    

    self.labelOne = [[UILabel alloc] initWithFrame:CGRectMake(60, 400, 60, 40)];

    self.labelOne.text = @"hello";

    [self.view addSubview:self.labelOne];

    

    self.playBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];

    self.playBtn.frame = CGRectMake(100, 300, 60, 40);

    [self.playBtn setTitle:@"123" forState:(UIControlStateNormal)];

    [self.playBtn addTarget:self action:@selector(didplayBrtn) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:self.playBtn];

    

}



- (void)didChangeBtnActionL:(UIButton *)sender

{


//    _HUD = [[MBProgressHUD alloc] initWithFrame:CGRectMake(60, 100, 60, 60)];

//    [self.view addSubview:_HUD];

//    

//    // Set the hud to display with a color

//    _HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];

//    

//    _HUD.delegate = self;

//    [_HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];

//


    

   _HUD = [[MBProgressHUD alloc] initWithFrame:CGRectMake(60, 100, 60, 60)];

    [self.view addSubview:_HUD];

    

    _HUD.dimBackground = YES;

    

    // Regiser for HUD callbacks so we can remove it from the window at the right time

    _HUD.delegate = self;

    

    // Show the HUD while the provided method executes in a new thread

    [_HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];


    


}


- (void)myTask

{



    sleep(3);





}


- (void)didplayBrtn

{



    NSLog(@"heloo1");

 

    SecondViewController *secondVC = [[SecondViewController alloc] init];

    [self.navigationController pushViewController:secondVC animated:YES];


}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值