您的位置:首页 > 理论基础 > 计算机网络

网络编程第三方 时间选择器

2015-10-04 10:20 561 查看
标题
网络编程第三方:
要用到第三方的框架:
在AppDelegate.h 同时导入 Reachability.h
当网络状态发生改变的时候 Reachability会发送kReachabilityChangedNotification这个通知
通知观察者
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeStatus:) name:kReachabilityChangedNotification object:nil];

初始化 Reachability
HostName 网址 <<注意>>不用填写http://
reachability 会访问 www.baidu.com来分析网络状态
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];

开始分析观察这个网址的网络状态
[reachability startNotifier];

return YES;
}
- (void)changeStatus:(NSNotification *)not
{

NSLog(@"%@",not.object);

网络状态发生改变的时候
会把Reachability的对象传过来

Reachability *reachability = not.object;
NSLog(@“%ld",reachability.currentReachabilityStatus);

停止检查网络状态
[reachability stopNotifier];

NSString *status;
switch (reachability.currentReachabilityStatus) {
case NotReachable:

status = @"没有网络";
break;
case ReachableViaWiFi:

status = @"WiFi";
break;

case ReachableViaWWAN:

status = @"蜂窝数据网络";
break;

default:
break;
}

}

在ViewController.h 中导入AFNetWorking.h

{
[super viewDidLoad];

/*
#pragma mark------ 检测网络 ------

使用 AFNetWorking
自带的类 判断网络状态

初始化AFNetworkReachabilityManager
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];

开始监测
[manager startMonitoring];

[manager setReachabilityStatusChangeBlock:^void(AFNetworkReachabilityStatus status) {

switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知的网络");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"没有网络");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"有WiFi");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"蜂窝数据");
break;

default:
break;
}
NSLog(@"当前网络状态:%ld",status);

}];
*/

get

初始化

GET URL 字符串
地址 parameters body体
的内容
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager GET:@"" parameters:nil success:^void(AFHTTPRequestOperation * operation,id responseObject) {

responseObject 请求下来的数据内容

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

post

parameters//参数
需要 post 的内容
[manager POST:@"" parameters:@{} constructingBodyWithBlock:^void(id responseObject){

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *errror) {

}];

}

第三方2.
#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@property(nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

UIScrollView *sView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, 300, 440)];

sView.contentSize = CGSizeMake(1200, 440);
sView.pagingEnabled = YES;
sView.showsHorizontalScrollIndicator = NO;
sView.tag = 200;

//循环创建添加4张图片
for (int i = 0; i < 4; i ++) {
UIImageView *imgView = [[UIImageView alloc]init];
imgView.frame = CGRectMake(i*300, 0, 300, 440);
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[sView addSubview:imgView];
}

[self.view addSubview:sView];

//创建书页控件
UIPageControl *pageControl = [[UIPageControl alloc]init];
pageControl.frame = CGRectMake( 100, 440, 120, 20);
pageControl.numberOfPages = 4;
pageControl.currentPage = 0;
pageControl.tag = 100;
[self.view addSubview:pageControl];

//设置scrollView的代理为当前类对象
sView.delegate = self;

//添加定时器,使用scheuled方法创建的定时器,不需要用fird方法打开(自动开启的)
[self addtimer];
}

//添加定时器方法
-(void) addtimer{
_timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];

//返回当前的消息循环对象
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

//删除定时器方法
-(void) deleteTimer{
[_timer invalidate];
_timer = nil;
}

-(void)nextPage{
int page = 0;
UIPageControl *pControl = (UIPageControl *)[self.view viewWithTag:100];
if (pControl.currentPage == 3) {
page = 0;
}else{
// page = pControl.currentPage + 1;
}

计算滚动的位置
UIScrollView *sView = (UIScrollView *)[self.view viewWithTag:200];
CGFloat offsetX = page * sView.frame.size.width;
CGPoint offset = CGPointMake(offsetX, 0);
[sView setContentOffset:offset animated:YES];
}

#pragma - mark UIScrollViewDelegate
//监听滚动的位置,改变pageCotrol的currentPage的值.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

UIPageControl *pControl = (UIPageControl *)[self.view viewWithTag:100];
CGFloat scrollW = scrollView.frame.size.width;

int page = (scrollView.contentOffset.x + scrollW * 0.5 )/ scrollW;
pControl.currentPage = page;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self deleteTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self addtimer];
}

@end

时间选择器(UIDatePicker)

#import "ViewController.h"

@interface ViewController ()
{

定义全局变量
UIView *bgView;
UIDatePicker *datePicker;

}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self creatDatePicker];

}

- (void)creatDatePicker
{

if (bgView) {

}

bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(self.view.frame), 400)];

[self.view addSubview:bgView];

//
时间选择器 UIDatePicker

datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,0, CGRectGetWidth(self.view.frame),400)];

datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.backgroundColor = [UIColor lightGrayColor];

设置最小的时间范围
datePicker.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];

设置最大的时间范围
datePicker.maximumDate = [NSDate dateWithTimeIntervalSince1970:24 * 60 * 60 * 365 * 10];

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

[bgView addSubview:datePicker];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(CGRectGetWidth(self.view.frame) - 50, 0, 50, 59);
[button setTitle:@"X" forState:UIControlStateNormal];

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:@selector(removePicker:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:button];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

button1.frame = CGRectMake(CGRectGetWidth(self.view.frame) - 50, 0, 50, 59);
[button1 setTitle:@"显示" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

button1.showsTouchWhenHighlighted = YES;
[button1 addTarget:self action:@selector(findPicker:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];

}
- (void)findPicker:(UIButton *)sender
{

bgView.hidden = NO;

}

- (void)removePicker:(UIButton *)sender
{

// [bgView removeFromSuperview];
// bgView = nil;
bgView.hidden = YES;

}

时间选择器实现的方法
- (void)didFinishSelectDate:(UIDatePicker *)sender
{

NSLog(@"%@",sender.date);

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: