OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器

1.进度条-UIProgressView
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progress1;
@property (weak, nonatomic) IBOutlet UIProgressView *progress3;
@property (weak, nonatomic) IBOutlet UIProgressView *progress2;
- (IBAction)tapped:(id)sender;

@end

@implementation ViewController{
  //定义一个计时器
  NSTimer* _timer;
  //定义一个变量,记录当前的进度值
  CGFloat _progVal;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  //创建可拉伸图片,指定对no.gif图片整体进行平铺
  UIImage* trackImage=[[UIImage imageNamed:@"no.png"]resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeTile];
  //创建可拉伸图片,指定对ok.gif图片整体进行平铺
  UIImage* progressImage=[[UIImage imageNamed:@"ok.png"]resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeTile];
  //自定义第三个进度条的外观
  self.progress3.trackImage=trackImage;
  self.progress3.progressImage=progressImage;
  
}

- (IBAction)tapped:(id)sender {
  _progVal=0;
  //启动计时器,控制每隔0.2秒调用一次changeprogeress方法
  _timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeProgress) userInfo:nil repeats:YES];
}
-(void)changeProgress{
  //进度值增加0.01
  _progVal+=0.01;
  if (_progVal>=1.0) {
    [_timer invalidate];
  }else{
    //同时改变三个进度条的进度值
    [self.progress1 setProgress:_progVal animated:YES];
    [self.progress2 setProgress:_progVal animated:YES];
    [self.progress3 setProgress:_progVal animated:YES];

    
  }
}
@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第1张图片

2.进度条-UIActivitydicatorView
#import "ViewController.h"

@interface ViewController ()
//四个indicator控件绑定到一个属性集合上
@property (strong, nonatomic) IBOutletCollection(UIActivityIndicatorView) NSArray *progress;

- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  self.view.backgroundColor=[UIColor grayColor];
}


- (IBAction)stop:(id)sender {
}

- (IBAction)start:(id)sender {
  for (int i=0; i<self.progress.count; i++) {
    [self.progress[i] startAnimating];
  }
}
@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第2张图片

3.拖动条-UISlider

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第3张图片

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myimage;
- (IBAction)changed:(UISlider *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  //创建可拉伸图片
  UIImage* oneImage=[[UIImage imageNamed:@"11.jpeg"]resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeTile];
  self.myimage.image=oneImage;
}



- (IBAction)changed:(UISlider *)sender {//这里参数是UISlider类
  [self.myimage setAlpha:sender.value];
}
@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第4张图片

4.警告框–UIAlertView(在IOS9中被废弃,不学)
5.警告框–UIActionSheet(在IOS9中被废弃,不学)
6.日期选择器–UIDatePicker
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *myDate;
- (IBAction)tapped:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *myTimeLabel;
@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
}
- (IBAction)tapped:(id)sender {
  //获取用户通过UIDatePicker设置的日期和时间
  NSDate* selected=[self.myDate date];
  //创建一个日期格式器
  NSDateFormatter* dateFormatter=[[NSDateFormatter alloc]init];
  //为日期格式器设置格式字符串
  [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm +0800"];
  //使用日期格式化器格式化日期时间
  NSString *destDateString=[dateFormatter stringFromDate:selected];
  //输出的文字
  NSString* message=[NSString stringWithFormat:@"您选择的时间是:%@",destDateString ];
  self.myTimeLabel.text=message;  
}
@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第5张图片

7.日期选择器–UIDatePicker–倒计时器
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *showInfo;
@property (weak, nonatomic) IBOutlet UIDatePicker *countDown;
@property (weak, nonatomic) IBOutlet UIButton *startBn;

- (IBAction)tapped:(id)sender;


@end

@implementation ViewController
{
  NSTimer* _timer;
  NSInteger _leftSeconds;
}

- (void)viewDidLoad {
  [super viewDidLoad];
}

- (IBAction)tapped:(id)sender {
  
  //获取该倒计时器的剩余时间
   _leftSeconds=self.countDown.countDownDuration;
   //禁用UIDatePicker控件和按钮
   self.countDown.enabled=NO;
   [sender setEnabled:NO];
   //初始化一个字符串
   NSString* message=[NSString stringWithFormat:@"开始倒计时?还剩下%ld秒",_leftSeconds ];
   self.showInfo.text=message;
  //启动定时器,控制间隔每隔60秒执行一次tickDown方法
  _timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(tickeDown) userInfo:nil repeats:YES];

}
-(void)tickeDown{
  //剩余时间减少60秒
  _leftSeconds-=5;
  //修改倒计时器的剩余时间
  self.countDown.countDownDuration=_leftSeconds;
  NSString* message=[NSString stringWithFormat:@"开始倒计时?还剩下%ld秒",_leftSeconds ];
  self.showInfo.text=message;
  if (_leftSeconds<=0) {
    //取消定时器
    [_timer invalidate];
    //启用倒计时控件和开始按钮控件
    self.countDown.enabled=YES;
    self.startBn.enabled=YES;
  }
}
@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第6张图片

8.选择器–UIPickerView–单列选择器
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UILabel *showInfo;

@end

@implementation ViewController{
  NSArray* _books;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // 创建并初始化nsarray对象
  _books=@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂swift讲义",@"疯狂XML讲义",@"疯狂java讲义"];
  //为UIPickerView设置DataSource和delegate
  //self.showInfo.text=_books[1];

  self.picker.dataSource = self;
  self.picker.delegate = self;
}

//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
  return 1;//表明该控件只包含一列
}
//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少个列表项
-(NSInteger)pickerView:(UIPickerView*)pickerView  numberOfRowsInComponent:(NSInteger)component{
  //由于控件只有一列,不需要管component的值
  //返回books数组的大小
  return _books.count;//表明该控件只包含一列
}


//UIPickerViewDelegate中定义的方法,该方法返回的nsstring将作为UIPickerView中指定列和列表项标题文本
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent  :(NSInteger)component{
  //由于控件只有一列,不需要管component的值
  //根据row参数返回books数组中的元素,row参数代表列表项的编号
  return _books[row];
}

//选中DataSource第几列、第几项时激发的方法
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  self.showInfo.text=_books[row];
}

@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第7张图片

9.选择器–UIPickerView–多列选择器
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UILabel *showInfo;
@property (weak, nonatomic) IBOutlet UILabel *mylist;

@end

@implementation ViewController{
  NSArray* _books;
  NSArray* _authors;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // 创建并初始化nsarray对象
  _authors=@[@"泰戈尔",@"纪伯伦",@"托尔斯泰"];
  _books=@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂swift讲义",@"疯狂XML讲义",@"疯狂java讲义"];
  //为UIPickerView设置DataSource和delegate
  //self.showInfo.text=_books[1];

  self.picker.dataSource = self;
  self.picker.delegate = self;
}

//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
  return 2;//表明该控件只包含一列
}
//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少个列表项
-(NSInteger)pickerView:(UIPickerView*)pickerView  numberOfRowsInComponent:(NSInteger)component{
  //按照列数返回各个数组中的元素个数
  @try {
    switch (component) {
      case 0:
        return _authors.count;
        break;
      case 1:
        return _books.count;
        break;
      default:
        break;
    }
  } @catch (NSException *exception) {
    NSLog(@"%@",exception);
  }
  
}


//UIPickerViewDelegate中定义的方法,该方法返回的nsstring将作为UIPickerView中指定列和列表项标题文本
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent  :(NSInteger)component{
    //按照列数返回各个数组中的元素值
  @try {
    switch (component) {
      case 0:
        return _authors[row];
        break;
      case 1:
        return _books[row];
        break;
      default:
        break;
    }
  } @catch (NSException *exception) {
    NSLog(@"%@",exception);
  }
}

//选中DataSource第几列、第几项时激发的方法
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  if (component==0) {
    self.mylist.text=@"作者";
  }else{
    self.mylist.text=@
    "图书";

  }
  self.showInfo.text=_books[row];
}

@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第8张图片

10.选择器–UIPickerView–相互依赖的多列选择器
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UILabel *showInfo;
@property (weak, nonatomic) IBOutlet UILabel *mylist;

@end

@implementation ViewController{
  NSDictionary* _books;
  NSArray* _authors;
  //保存当前选中的作者
  NSString* _selectAuthor;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // 创建并初始化nsarray对象
  _books = @{
    @"泰戈尔":@[@"飞鸟集",@"园丁集"],
    @"冯梦龙":@[@"醒世恒言",@"喻世明言",@"警世通言"],
    @"李刚":@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂JAVA讲义",@"疯狂XML讲义"]
  };
  
  //使用_authors保存_books所有key组成的NSArray排序后的结果
  _authors=[[_books allKeys] sortedArrayUsingSelector:@selector(compare:)];
  //设置默认选中的作者的_authors中的第一个元素
  _selectAuthor=_authors[0];
  //为UIPickerView设置DataSource和delegate

  self.picker.dataSource = self;
  self.picker.delegate = self;
}

//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
  return 2;//表明该控件只包含2列
}
//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少个列表项
-(NSInteger)pickerView:(UIPickerView*)pickerView  numberOfRowsInComponent:(NSInteger)component{
  //按照列数返回各个数组中的元素个数
  if (component==0) {
    return _authors.count;
  }
  //如果有其他列返回——books中_selectedAuthor对应的nsarray中元素的个数
  return [_books[_selectAuthor] count];
  
}


//UIPickerViewDelegate中定义的方法,该方法返回的nsstring将作为UIPickerView中指定列和列表项标题文本
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent  :(NSInteger)component{
    //按照列数返回各个数组中的元素值
  if (component==0) {
    //第一列由_authors集合元素决定
    return _authors[row];
  }
  //如果是其他列返回选中处的元素
  return [_books[_selectAuthor] objectAtIndex:row];
}

//选中DataSource第几列、第几项时激发的方法
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  if (component==0) {
    //改变被选中的作者
    _selectAuthor=_authors[row];
    //控制重写加载第二个列表,根据选中的走着来加载第二个列表
    [self.picker reloadComponent:1];
  }
  NSArray* temp= component==0?_authors:_books[_selectAuthor];
  NSString* tip=component==0?@"作者":@"图书";
  
  //显示
  self.mylist.text=tip;
  self.showInfo.text=temp[row];
}

@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第9张图片

11.选择器–UIPickerView–自定义选择器视图
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UIImageView *myImage;
@property (weak, nonatomic) IBOutlet UIButton *startBn;


- (IBAction)tapped:(id)sender;


@end

@implementation ViewController
{
  UIImage* _lostImage;
  UIImage* _winImage;
  //保存系统中所有图片的集合
  NSArray* _images;
  //定义两个声音
  //AVAudioPlayer * winSound;
  //AVAudioPlayer * crunchSound;

  
}

- (void)viewDidLoad {
  [super viewDidLoad];
  _lostImage=[UIImage imageNamed:@"lost.png"];
  _winImage=[UIImage imageNamed:@"win.png"];
  //依次加载6张图片,生成对应的UIImage对象
  UIImage* img11=[UIImage imageNamed:@"11.png"];
  UIImage* img12=[UIImage imageNamed:@"12.png"];
  UIImage* img13=[UIImage imageNamed:@"13.png"];
  UIImage* img14=[UIImage imageNamed:@"14.png"];
  UIImage* img15=[UIImage imageNamed:@"15.png"];
  UIImage* img16=[UIImage imageNamed:@"16.png"];
  
  //初始化——images集合,将前面的6张图片封装成_image集合
  _images=@[img11,img12,img13,img14,img15,img16];
  //禁用用户交互
  self.picker.userInteractionEnabled=NO;
  //设置数据源和委托
  self.picker.dataSource=self;
  self.picker.delegate=self;
}

//重写dataSource中的方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
  return 5;//控件包含5列
}
//该方法返回值决定该控件指定列包含多少个列表项
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
  return _images.count;
}

#define kImageTag 1
//返回delegete的方法,返回指定列和列表项的UI控件
-(UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  if (view.tag!=kImageTag) {
    view=[[UIImageView alloc]initWithImage:_images[row]];
    //为该view设置tag属性
    view.tag=kImageTag;
    //设置不允许交互
    view.userInteractionEnabled=NO;
  }
  return view;
}

//返回列表项的高度
-(CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component{
  return 40;
}
//返回列表项的宽度
-(CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component{
  return 40;
}
- (IBAction)tapped:(id)sender {
  //禁用按钮
  self.startBn.enabled=NO;
  self.myImage.image=nil;//清空见面上myImage控件的图片
  //定义一个nsmutableDictionary来记录每个随机数出现的次数
  NSMutableDictionary* result=[[NSMutableDictionary alloc]initWithCapacity:6];
  for (int i=0; i<5; i++) {
    //生成0-_images.count之间的 一个随机数
    int selectedVal=arc4random()%_images.count;
    [self.picker selectRow:selectedVal inComponent:i  animated:YES];
    if ([result objectForKey:[NSNumber numberWithInt:selectedVal]]) {
      //获取result中概随机数出现的次数
      NSInteger newCount=[result [[NSNumber numberWithInt:selectedVal]] intValue];
      //将获取到的随机数+1
      [result setObject:[NSNumber numberWithLong:(newCount+1)] forKey:[NSNumber numberWithInt:selectedVal]];
    }else{
      //s使用reslut记录随机数的出现次数为1
      [result setObject:[NSNumber numberWithInt:1] forKey:[NSNumber numberWithInt:selectedVal]];
    }
    //使用result记录随机数的最大出现次数
    NSUInteger maxOccurs=1;
    for (NSNumber* num in [result allKeys]){
      //只要热河随机数的出现次数大于maxOccurs
      if ([[result objectForKey:num] integerValue]>maxOccurs) {
        //使用maxOccurs保存概随机数出现的次数
        maxOccurs=[[result objectForKey:num]integerValue];
      }
    }
    //如果某个随机数出现的次数大于或等于3,即界面中处出现了3个相同的图片
    if (maxOccurs>=3) {
      //如果赢了延迟0.5秒执行showWin方法,显示结果
      [self performSelector:@selector(showWin) withObject:nil afterDelay:0.5];
    }else{
      //如果输了延迟0.5秒执行showlsot方法,显示结果
      [self performSelector:@selector(showlost) withObject:nil afterDelay:0.5];
    }
  }
}

-(void)showWin{
  self.myImage.image=_winImage;
  self.startBn.enabled=YES;
}
-(void)showlost{
  self.myImage.image=_lostImage;
  self.startBn.enabled=YES;
}
@end

OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第10张图片
OC学习笔记之011IOS应用开发入门--控件2进度条、拖动条、警告框、日期选择器、选择器_第11张图片

你可能感兴趣的:(#,oc学习笔记,IOS学习笔记)