⼀、UISegmentedControl的使⽤html
分段控件。每一個segment都能被點擊,至關於集成了若干個button。 一般咱們會點擊不一樣的segment來切換不一樣的view。ios
一、常⽤⽅法和屬性app
initWithItems: //UISegmentedControl獨有的初始化⽅法,⽤來建立多個分段ide
setTitle: forSegmentAtIndex: //爲指定下標的分段設置titleatom
selectedSegmentAtIndex //(property)被選中的segmentspa
tintColor //(property)segmentedControl條的顏⾊(含每一個segment的顏⾊).net
addTarget: action: forControlEvents://給UISegmentedControl添加事件,code
controlEvent //爲UIControlEventValueChanged。orm
二、使用視頻
RootViewController.m
#import "RootViewController.h"
#import "MessageViewController.h"
#import "PhoneViewController.h"
@interface RootViewController ()
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) MessageViewController * messageVC;
@property (nonatomic, retain) PhoneViewController * phoneVC;
@end
@implementation RootViewController
- (void)dealloc
{
[_segmentedControl release];
[_messageVC release];
[_phoneVC release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
#pragma mark -- 添加子視圖控制器 --
[self createChildViewControllers];
#pragma mark -- UISegmentedControl --
[self createSegmentedControl];
}
- (void)createChildViewControllers
{
MessageViewController * messageVC = [[MessageViewController alloc] init];
PhoneViewController * phoneVC = [[PhoneViewController alloc] init];
self.phoneVC = phoneVC;
self.messageVC = messageVC;
[self addChildViewController:messageVC];
[self addChildViewController:phoneVC];
[messageVC release];
[phoneVC release];
[self.view addSubview:messageVC.view];
}
- (void)createSegmentedControl
{
UIImage *image
= [UIImage imageNamed:@"bank_icon_ccb.png"];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"電話",image]];
self.segmentedControl.frame = CGRectMake(100, 100, 150, 30);
self.segmentedControl.selectedSegmentIndex = 0;
//改變顏色
_segmentedControl.tintColor = [UIColor redColor];
[self.view addSubview:self.segmentedControl];
[self.segmentedControl release];
//添加事件
[_segmentedControl addTarget:self action:@selector(exchangeView:) forControlEvents:UIControlEventValueChanged];
[self.segmentedControl release];
}
- (void)exchangeView:(UISegmentedControl *)SC
{
NSLog(@"%ld", (long)SC.selectedSegmentIndex);
switch (SC.selectedSegmentIndex) {
case 0: {
if (!self.messageVC.view.superview) {
[_phoneVC.view removeFromSuperview];
[self.view addSubview:_messageVC.view];
}
break;
}
case 1:
if (!_phoneVC.view.superview) {
[_messageVC.view removeFromSuperview];
[self.view addSubview:_phoneVC.view];
}
break;
default:
break;
}
[self.view bringSubviewToFront:SC];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
⼆、UISlider的使⽤
滑塊控件。 一般⽤於控制視頻播放進度,控制⾳量等。 它也是繼承於UIControl,滑塊提供了⼀系列連續的值,滑塊停在不 同的位置,獲取到滑塊上的值也不一樣。
一、UISlider常⽤屬性
minimumValue //設置滑塊的最⼩值
maximumValue //設置滑塊的最⼤值
value //設置滑塊的當前值
minimumTrackTinkColor //定義劃過區域的顏⾊
addTarget: action: forControlEvents: //給UISlider添加事件,
controlEvent //爲UIControlEventValueChanged。
二、UISlider使用
下面是UISlider與UIImageView的綜合使用。用UISlider控制動圖的播放速度(關於UIImageView的介紹請看前一篇)
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, retain) UISlider *slider;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
#pragma mark -- 建立UIImageView --
[self createImageView];
#pragma mark -- 建立UISlider --
[self createSlider];
}
- (void)createSlider
{
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 400, 267, 30)];
[self.view addSubview:_slider];
[_slider release];
_slider.minimumValue = 1;
_slider.maximumValue = 5; //最大值必須大於最小值
_slider.minimumTrackTintColor = [UIColor redColor];
_slider.maximumTrackTintColor = [UIColor blueColor];
//設置滑塊
[_slider setThumbImage:[UIImage imageNamed:@"greenbird.png"] forState:UIControlStateNormal];
[_slider setThumbImage:[UIImage imageNamed:@"bluebird.png"] forState:UIControlStateHighlighted];
[_slider addTarget:self action:@selector(changeSpeed:) forControlEvents:UIControlEventValueChanged];
}
- (void)changeSpeed:(UISlider *)slider
{
if (slider.value==slider.minimumValue) {
[self.imgView stopAnimating];
} else if(slider.value != slider.maximumValue) {
self.imgView.animationDuration = slider.maximumValue-slider.value;
[self.imgView startAnimating];
} else {
self.imgView.animationDuration = 0.0001;
[self.imgView startAnimating];
}
}
- (void)createImageView
{
self.imgView = [[UIImageView alloc] init];
_imgView.frame = CGRectMake(50, 100, 250, 250);
[self.view addSubview:_imgView];
[_imgView release];
NSMutableArray * imagesArray = [NSMutableArray array];
for (int i = 1; i<28; i++) {
[imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"Z6CLNZ]T)N8I}L9A))7TRD9-%d(被拖移).tiff", i]]];
}
_imgView.animationImages = imagesArray;
_imgView.image = _imgView.animationImages[0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
3、UIAlertController
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain) UITextField * textField;
@property (nonatomic, retain) UIButton *button;
@end
@implementation RootViewController
- (void)dealloc
{
[_textField release];
[_button release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"請輸入密碼";
[self.view addSubview:_textField];
[_textField release];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(150, 300, 60, 30);
[_button setTitle:@"登錄" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
NSLog(@"%lu", self.retainCount);
}
- (void)login:(UIButton *)button
{
NSString * infoStr;
if ([_textField.text isEqualToString:@"123"]) {
infoStr = @"登陸成功";
#pragma mark -- UIAlertView -- //UIAlertView
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"登陸成功" message:infoStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];
[alertView show];
[alertView release];
} else {
infoStr = @"登陸失敗";
#pragma mark -- UIAlertController -- //UIAlertController
UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"登陸信息" message:infoStr preferredStyle:UIAlertControllerStyleAlert];
//避免循環引用
//在block裏用到self,將self賦值給__block修飾的變量再使用
__block RootViewController *rootVC = self;
//在block裏用到實例變量(非屬性),用以下方式
// __block UITextField *textField = _textField;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[rootVC.textField resignFirstResponder];
}];
UIAlertAction *reloginAction = [UIAlertAction actionWithTitle:@"從新登陸" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
rootVC.textField.text = @"";
}];
[alertC addAction:cancelAction];
[alertC addAction:reloginAction];
[self presentViewController:alertC animated:YES completion:nil];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", _textField.text);
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"canceled");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
4、UIControl的做⽤
UIControl是全部控制控件(⽐如UIButton、UISlider、 UISegmentedControl等)的基類。 只要跟控制有關的控件都是繼承於該類。
一、UIControl的核⼼功能
爲控制控件經過addTarget: action: forControlEvents: ⽅法來添加事件。
經過removeTarget: action: forControlEvents: 來移除事件。
二、UIControl的繼承關係
UIControl
iOS UIKit_Framework: