SwithAndActivity 选择开关和活动指示

//
//  ViewController.m
//  SwithAndActivity
//
//  Created by qianfeng on 15/9/21.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createSwitch];    //选择开关
    
    [self createActivityIndicator]; //活动指示
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 开关
- (void)createSwitch
{
    UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 100, 50)];
    
    [self.view addSubview:sw];
    
    //设置属性
    sw.on = YES;
    
    //on时颜色
    sw.onTintColor = [UIColor orangeColor];
    sw.tintColor = [UIColor blueColor];
    sw.thumbTintColor = [UIColor purpleColor];
    
    //注册事件
    [sw addTarget:self action:@selector(onSwitchChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)onSwitchChange:(UISwitch *)sw
{
    NSLog(@"sw = %d",sw.on);
    
    UIActivityIndicatorView *ai = (id)[self.view viewWithTag:10];
    
    if( sw.on == YES){
        [ai startAnimating];
    }else{
        [ai stopAnimating];
        ai.hidesWhenStopped = NO;
    }
}

#pragma mark - 活动指示视图
- (void)createActivityIndicator
{
    //一般类型视图
    UIActivityIndicatorView *ai = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    ai.frame = CGRectMake(100, 200, 200, 50);
    
    [self.view addSubview:ai];
    
    ai.tag = 10;
    
    //显示出来
//    [ai startAnimating];

}
@end

//实现了在选择开关关闭的时候活动指示动作,选择开关开的时候,活动指示隐藏。

 

posted @ 2015-09-21 21:46  阿凡提王  阅读(213)  评论(0)    收藏  举报