UI总结-UIControl和UIControl的一些重要子类

        UI总结-UIControl和UIControl的一些重要子类

今天写了一个简单的播放器,来体现UIcontrol和UIcontrol子类控件的一些作业:

#import "ViewController.h"

#import

@interface ViewController ()

@property(nonatomic, retain)UIImageView *imagev;

@property(nonatomic, retain)AVAudioPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

//UIControl对象采用了一种事件处理机制,将之前写到的事件转化成简单的操作,这样就无需关心用户访问空间的具体方式.

//UIControl是UIView的子类,但它是UIButton,UISwith,UITextfield等控件的父类,它本身包含了一些属性,但是不能直接使用UIControl类,它只是定义子类都使用的方法.

UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];

control.backgroundColor = [UIColor redColor];

[self.view addSubview:control];

[control release];

//开关按钮

UISwitch *switchOpen = [[UISwitch alloc]initWithFrame:CGRectMake(150, 0, 150, 50)];

[self.view addSubview:switchOpen];

[switchOpen release];

switchOpen.onTintColor = [UIColor blueColor];

switchOpen.tintColor = [UIColor greenColor];

[switchOpen addTarget:self action:@selector(touch:) forControlEvents:UIControlEventValueChanged];

//滑块控件

UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 60, 300, 50)];

slider.backgroundColor = [UIColor orangeColor];

[self.view addSubview:slider];

[slider release];

[slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];

slider.minimumValue = 0;

slider.maximumValue = 235.076;

slider.minimumTrackTintColor = [UIColor blueColor];

slider.maximumTrackTintColor = [UIColor redColor];

slider.thumbTintColor = [UIColor greenColor];

slider.tag = 1000;

//分段控制器

UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"红", @"蓝", @"绿"]];

seg.frame = CGRectMake(0, 120, 300, 50);

[self.view addSubview:seg];

[seg release];

seg.backgroundColor = [UIColor greenColor];

seg.layer.cornerRadius = 10;

[seg addTarget:self action:@selector(seg:) forControlEvents:UIControlEventValueChanged];

//步进控件

UIStepper *step = [[UIStepper alloc]initWithFrame:CGRectMake(0, 180, 100, 50)];

step.backgroundColor = [UIColor grayColor];

[self.view addSubview:step];

[step release];

[step addTarget:self action:@selector(step:) forControlEvents:UIControlEventValueChanged];

//日期选择器

UIDatePicker *date = [[UIDatePicker alloc]initWithFrame:CGRectMake(120, 180, 280, 50)];

[self.view addSubview:date];

date.backgroundColor = [UIColor grayColor];

[date release];

//寻找本地文件路径

NSString *path = [[NSBundle mainBundle]pathForResource:@"童年" ofType:@"mp3"];

NSURL *url = [NSURL fileURLWithPath:path];

//定义播放器

self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

[self.player play ];

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];

button.backgroundColor = [UIColor grayColor];

button.frame = CGRectMake(100, 240, 50, 50);

button.layer.cornerRadius = 25;

[self.view addSubview:button];

[button release];

[button setTitle:@"暂停" forState:UIControlStateNormal];

[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

//定时器

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];

//进度条

UIProgressView *pro = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 250, 400, 20)];

pro.progressTintColor = [UIColor orangeColor];

[self.view addSubview:pro];

[pro release];

pro.progress = self.player.currentTime;

}

#pragma mark - switch的点击方法

- (void)touch:(UISwitch *)switchOpen{

NSLog(@"%d",switchOpen.isOn);

if (switchOpen.isOn == 0) {

self.player.volume = 0;

}else{

self.player.volume = 3;

}

}

#pragma mark - slider的点击方法

- (void)slider:(UISlider *)slider{

self.player.currentTime = slider.value;

}

#pragma mark - SegmentedControl的点击方法

-(void)seg:(UISegmentedControl *)seg{

NSLog(@"%ld",seg.selectedSegmentIndex);

switch (seg.selectedSegmentIndex ) {

case 0:

self.view.backgroundColor = [UIColor redColor];

break;

case 1:

self.view.backgroundColor = [UIColor blueColor];

break;

case 2:

self.view.backgroundColor = [UIColor greenColor];

break;

default:

break;

}

}

#pragma mark -stepper的点击方法

-(void)step:(UIStepper *)step{

//用step控制播放器的音量

step.minimumValue = 0;

step.maximumValue = 10;

self.player.volume = step.value;

}

-(void)changeTime{

UISlider *slider = (UISlider *)[self.view viewWithTag:1000];

slider.value = self.player.currentTime;

}

-(void)click:(UIButton *)button{

if ([button.currentTitle isEqualToString:@"播放"]) {

[self.player play];

[button setTitle:@"暂停" forState:UIControlStateNormal];

}else if([button.currentTitle isEqualToString:@"暂停"]){

[self.player pause];

[button setTitle:@"播放" forState:UIControlStateNormal];

}

}

你可能感兴趣的:(UI总结-UIControl和UIControl的一些重要子类)