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];
UIImage* trackImage=[[UIImage imageNamed:@"no.png"]resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeTile];
UIImage* progressImage=[[UIImage imageNamed:@"ok.png"]resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeTile];
self.progress3.trackImage=trackImage;
self.progress3.progressImage=progressImage;
}
- (IBAction)tapped:(id)sender {
_progVal=0;
_timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeProgress) userInfo:nil repeats:YES];
}
-(void)changeProgress{
_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
2.进度条-UIActivitydicatorView
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutletCollection(UIActivityIndicatorView) NSArray *progress;
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
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
3.拖动条-UISlider
#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 {
[self.myimage setAlpha:sender.value];
}
@end
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 {
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
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;
self.countDown.enabled=NO;
[sender setEnabled:NO];
NSString* message=[NSString stringWithFormat:@"开始倒计时?还剩下%ld秒",_leftSeconds ];
self.showInfo.text=message;
_timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(tickeDown) userInfo:nil repeats:YES];
}
-(void)tickeDown{
_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
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];
_books=@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂swift讲义",@"疯狂XML讲义",@"疯狂java讲义"];
self.picker.dataSource = self;
self.picker.delegate = self;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
return _books.count;
}
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent :(NSInteger)component{
return _books[row];
}
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.showInfo.text=_books[row];
}
@end
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];
_authors=@[@"泰戈尔",@"纪伯伦",@"托尔斯泰"];
_books=@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂swift讲义",@"疯狂XML讲义",@"疯狂java讲义"];
self.picker.dataSource = self;
self.picker.delegate = self;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
return 2;
}
-(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);
}
}
-(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);
}
}
-(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
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];
_books = @{
@"泰戈尔":@[@"飞鸟集",@"园丁集"],
@"冯梦龙":@[@"醒世恒言",@"喻世明言",@"警世通言"],
@"李刚":@[@"疯狂android讲义",@"疯狂IOS讲义",@"疯狂JAVA讲义",@"疯狂XML讲义"]
};
_authors=[[_books allKeys] sortedArrayUsingSelector:@selector(compare:)];
_selectAuthor=_authors[0];
self.picker.dataSource = self;
self.picker.delegate = self;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
return 2;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==0) {
return _authors.count;
}
return [_books[_selectAuthor] count];
}
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent :(NSInteger)component{
if (component==0) {
return _authors[row];
}
return [_books[_selectAuthor] objectAtIndex:row];
}
-(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
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;
}
- (void)viewDidLoad {
[super viewDidLoad];
_lostImage=[UIImage imageNamed:@"lost.png"];
_winImage=[UIImage imageNamed:@"win.png"];
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=@[img11,img12,img13,img14,img15,img16];
self.picker.userInteractionEnabled=NO;
self.picker.dataSource=self;
self.picker.delegate=self;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
return 5;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
return _images.count;
}
#define kImageTag 1
-(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=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;
NSMutableDictionary* result=[[NSMutableDictionary alloc]initWithCapacity:6];
for (int i=0; i<5; i++) {
int selectedVal=arc4random()%_images.count;
[self.picker selectRow:selectedVal inComponent:i animated:YES];
if ([result objectForKey:[NSNumber numberWithInt:selectedVal]]) {
NSInteger newCount=[result [[NSNumber numberWithInt:selectedVal]] intValue];
[result setObject:[NSNumber numberWithLong:(newCount+1)] forKey:[NSNumber numberWithInt:selectedVal]];
}else{
[result setObject:[NSNumber numberWithInt:1] forKey:[NSNumber numberWithInt:selectedVal]];
}
NSUInteger maxOccurs=1;
for (NSNumber* num in [result allKeys]){
if ([[result objectForKey:num] integerValue]>maxOccurs) {
maxOccurs=[[result objectForKey:num]integerValue];
}
}
if (maxOccurs>=3) {
[self performSelector:@selector(showWin) withObject:nil afterDelay:0.5];
}else{
[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