ios 延迟调用 && UIImageView && UILabel && UISegmentedControl && UISwitch && UISlider
//
// ViewController.m
// UI_Lesson3
//
// Created by archerzz on 15/8/13.
// Copyright (c) 2015年 archerzz. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIAlertViewDelegate>
// 活动指示器
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
- (void)initUserInterface;
- (void)handleEvent:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initUserInterface];
}
- (void)initUserInterface {
NSLog(@"%@", [NSBundle mainBundle]);
self.view.backgroundColor = [UIColor brownColor];
#pragma mark - UIImageView 图片视图
// 1. 创建图片
UIImage *image = [UIImage imageNamed:@"1"];
// 2. 创建图片视图
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// 3. 设置属性
// 3.1 设置大小
imageView.bounds = CGRectMake(0, 0, 50, 50);
// 3.2 设置中心点
imageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), 60);
// 3.3 设置缩放方式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 3.4 设置圆角
imageView.layer.cornerRadius = 25;// 宽高的一半成为圆形
// 3.5 裁剪
imageView.clipsToBounds = YES;
// 3.6 imageView默认情况用户交互是关闭的
imageView.userInteractionEnabled = YES;
// 4. 添加到父视图
[self.view addSubview:imageView];
#pragma mark - UILabel 文本自适应
UILabel *label = ({
UILabel *label = [[UILabel alloc] init];
// 1.1 设置文本
label.text = @"啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊";
// 1.2 设置字体
label.font = [UIFont systemFontOfSize:14];
// 1.3 设置位置大小
label.frame = CGRectMake(0, 60, 150, 0);
// 1.4 设置行数
label.numberOfLines = 0; // 无穷
// 1.5 设置换行模式
label.lineBreakMode = NSLineBreakByCharWrapping;
// 1.6 自适应
[label sizeToFit];
label;
});
// 添加到父视图
[self.view addSubview:label];
#pragma mark - UISegmentedControl 分段控件
// 1. 初始化
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:@[@"红色", @"紫色"]];
// 2.1 设置中心点
segControl.center = CGPointMake(260, 100);
// 2.2 设置主色调
segControl.tintColor = [UIColor redColor];
// 2.3 添加事件 UIControlEventValueChanged
[segControl addTarget:self
action:@selector(handleEvent:)
forControlEvents:UIControlEventValueChanged];
// 3. 添加到父视图
[self.view addSubview:segControl];
#pragma mark - UISwitch 开关
// 1. 初始化
UISwitch *switchControl = [[UISwitch alloc] init];
// 2.1 设置中心点
switchControl.center = CGPointMake(260, 160);
// 2.2 设置主色调
switchControl.tintColor = [UIColor redColor];
// 2.3 设置开启后的色调
switchControl.onTintColor = [UIColor grayColor];
// 2.4 设置按钮颜色
switchControl.thumbTintColor = [UIColor orangeColor];
// 2.5 添加事件
[switchControl addTarget:self
action:@selector(handleEvent:)
forControlEvents:UIControlEventValueChanged];
// 3. 添加到父视图
[self.view addSubview:switchControl];
#pragma mark - UISlider 滑条
// 1. 初始化
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 300, 375, 30)];
// 2. 设置属性
// 2.1 设置主色调
slider.tintColor = [UIColor greenColor];
// 2.2 左侧颜色
slider.minimumTrackTintColor = [UIColor grayColor];
// 2.3 右侧颜色
slider.maximumTrackTintColor = [UIColor redColor];
// 2.4 按钮颜色
slider.thumbTintColor = [UIColor purpleColor];
// 2.5 添加事件
[slider addTarget:self
action:@selector(handleEvent:)
forControlEvents:UIControlEventValueChanged];
// 3. 添加到父视图
[self.view addSubview:slider];
#pragma mark - UIProgressView 进度条
// 1. 初始化
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 350, 375, 30)];
// 2. 设置属性
// 3. 添加到父视图
[self.view addSubview:progressView];
#pragma mark - UIActivityIndicatorView 活动指示器
self.indicatorView = ({
// 1. 初始化
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
// 2.1 设置中心点
indicatorView.center = self.view.center;
// 2.2 打开隐藏
indicatorView.hidesWhenStopped = NO;
indicatorView;
});
// 3. 添加到父视图
[self.view addSubview:self.indicatorView];
#pragma mark - 延迟调用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"请输入用户名或密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
});
#pragma mark - UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"腾讯微博" otherButtonTitles:@"新浪微博", @"人人网", nil];
[actionSheet showInView:self.view];
#pragma mark - UIAlertController 补充
}
- (void)handleEvent:(id)sender {
// 判断sender是否为UISegmentedControl的对象
if ([sender isKindOfClass:[UISegmentedControl class]]) {
// 强转
UISegmentedControl *segControl = (UISegmentedControl *)sender;
NSLog(@"index = %ld", segControl.selectedSegmentIndex);
if (segControl.selectedSegmentIndex == 1) {
self.view.backgroundColor = [UIColor purpleColor];
} else {
self.view.backgroundColor = [UIColor redColor];
}
} else if ([sender isKindOfClass:[UISwitch class]]) {
UISwitch *switchControl = (UISwitch *)sender;
if (switchControl.isOn) {
[self.indicatorView startAnimating];
} else {
[self.indicatorView stopAnimating];
}
} else if ([sender isKindOfClass:[UISlider class]]) {
UISlider *slider = (UISlider *)sender;
self.view.alpha = 1 - slider.value;
}
}
#pragma mark - UIAlertViewDelegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"取消");
} else if (buttonIndex == 1) {
NSLog(@"确定");
}
}
@end