第二周笔记
1. 访问网络 加载数据 并显示在标签上
NSString *path = @"http://c.3g.163.com/nc/article/list/ T1348649079062/0-20.html"; // 构建网址
NSURL *url=[NSURL URLWithString:path];
NSData *data=[[NSData alloc]initWithContentsOfURL:url]; // 访问 并得到结果
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; // 解析数据1.
NSArray *array = dic[@"T1348649079062"]; //2
NSDictionary *dict = array[0];//3
NSString *name = dict[@"tname”];//4
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 30)]; //创建标签
label.backgroundColor = [UIColor lightGrayColor];
label.textColor = [UIColor redColor];
label.text = name;
[self.view addSubview:label];
2.自定义tabbar
// 创建 UITabBarController 作为根视图控制器
在UITabBarController的
//1. 隐藏系统的tabBar显示自己的tabBar
self.tabBar.hidden = YES;
// 2.创建背景图
UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, Screen_Height-50, Screen_Width, 50)];
// 注意1:背景透明
back.backgroundColor = [UIColor clearColor];
back.image = [UIImage imageNamed:@"导航栏"];
//注意2: 打开交互 ,为了图片可以添加点击事件
back.userInteractionEnabled = YES;
[self.view addSubview:back];
//3.创建tabbar按钮
// 未选中状态
NSArray *images = [[NSArray alloc]initWithObjects:@"tab_0", @"tab_1",@"3",@"tab_3",@"tab_2",nil];
// 选中状态
NSArray *Selectimages = [[NSArray alloc]initWithObjects:@"tab_c0@2x", @"tab_c1@2x",@"3",@"tab_c3@2x",@"tab_c2@2x",nil];
// 摆放按钮
float width = Screen_Width/5.0f;
float height = 30;
for (int i=0; i<5 ; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(width*i, 12, width, height);
button.backgroundColor = [UIColor clearColor];
// 给一个tag值,来区分点击的是哪个
button.tag = baseTag + i;
// 添加事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// 给button添加图片
[button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed: Selectimages[i]] forState:UIControlStateSelected];
// 设置button的选中状态
// 默认第0个是选中状态
button.selected = i == 0;
self.lastSelectTag = baseTag;
[back addSubview:button];
//3.点击事件的处理
- (void)buttonAction:(UIButton *)button
{
// 1、判断是不是把这个按钮点了两次,如果是就return
if (button.tag == self.lastSelectTag) {
return;
}
// 2、把当前选中的按钮点亮
button.selected = YES;
// 3、把上次选中的按钮还原
UIButton *lastButton = (UIButton *)[self.view viewWithTag:self.lastSelectTag];
// lastButton.selected = NO;
// 记录当前选中按钮的tag值
self.lastSelectTag = button.tag;
self.selectedIndex = button.tag - baseTag;
}
// 根据字符串创建类
Class class = NSClassFromString(arr[i]);
// 根据类创建对象
RootViewController *vc = [[class alloc]init];
// 全部加导航
3.手势1
三个方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
// 1. 获取手指当前的位置
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
// 2. 获取手指上一个位置
CGPoint preLocation = [touch previousLocationInView:self.view];
3.手势2
// 让视图支持多根手指点击事件
[self.view setMultipleTouchEnabled:YES];
4其他控件
分段控制器
// 1. 实例化分段控制器
UISegmentedControl *segmentedC = [[UISegmentedControl alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
添加分段(文字)
[segmentedC insertSegmentWithTitle:@"第一个" atIndex:0 animated:NO];
[segmentedC insertSegmentWithTitle:@"第二个" atIndex:1 animated:NO];
[segmentedC insertSegmentWithTitle:@"第三个" atIndex:2 animated:NO];
添加分段(图片)
[segmentedC insertSegmentWithImage:[UIImage imageNamed:@"0"] atIndex:3 animated:NO];
添加点击事件
[segmentedC addTarget:self action:@selector(clickSegmentedC:) forControlEvents:UIControlEventValueChanged];
// 4.默认选择某个分段
segmentedC.selectedSegmentIndex = 1;
// 5. 设置颜色
segmentedC.tintColor = [UIColor redColor];
// 6.修改某个分段标题
[segmentedC setTitle:@"FIRST" forSegmentAtIndex:0];
// 7.修改某个分段标题图片
[segmentedC setImage:[UIImage imageNamed:@""] forSegmentAtIndex:3];
// 8. 读取某个分段的标题
NSString *titleStr = [segmentedC titleForSegmentAtIndex:0];
// 9. 读取某个分段的图片
UIImage *image = [segmentedC imageForSegmentAtIndex:3];
// 10.添加到界面
// [self.view addSubview:segmentedC];
self.navigationItem.titleView = segmentedC;
// 获取用户点击的位置
NSInteger index = sender.selectedSegmentIndex;
UISlider
// 设置大小位置(默认高度)设置高度没有用
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 300, 100)];
// 设置最小值
slider.minimumValue = 0;
// 设置最大值
slider.maximumValue = 100;
// 设置初始值
slider.value = 40;
// 添加滑动事件
[slider addTarget:self action:@selector(moveSlider:) forControlEvents:UIControlEventValueChanged];
UISwitch
// 设置默认
swith.on = NO;
// 添加点击事件
[swith addTarget:self action:@selector(moveSwith:) forControlEvents:UIControlEventValueChanged];
UIActivityIndicatorView
UIActivityIndicatorView *acticityIV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// 设置位置 和 大小
acticityIV.frame = CGRectMake(100, 100, 50, 50);
acticityIV.backgroundColor = [UIColor lightGrayColor];
// 启动旋转
[acticityIV startAnimating];
// 停止旋转
[acticityIV stopAnimating];
// 显示
acticityIV.hidesWhenStopped = NO;
[self.view addSubview:acticityIV];
UIProgressView
// 设置大小和位置(高度无法改变)
_pregressView = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 100, 300, 20)];
// 进度条的颜色
_pregressView.progressTintColor = [UIColor redColor];
// 进度条的背景颜色
_pregressView.trackTintColor = [UIColor blackColor];
[self.view addSubview:_pregressView];
UIStepper
// 大小固定
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 200, 50, 50)];
// 最大值
stepper.maximumValue = 15;
// 最小值
stepper.minimumValue = 1;
// 每次点击添加的数量
stepper.stepValue = 2;
// 添加点击事件
[stepper addTarget:self action:@selector(clickStepper:) forControlEvents:UIControlEventValueChanged];
// 是否循环
[stepper setWraps:NO]
UIAlertView
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示标题" message:@"请输入账号和密码,不要被别人看到(⊙o⊙)哦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",@"😄",@"(*^__^*) 嘻嘻……",@"(⊙o⊙)…", nil];
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UIWebView
_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
// 2. 获取Url
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
// 3. 请求加载网址
[_webView loadRequest:urlRequest];
// 4. 自适应屏幕大小
[_webView setScalesPageToFit:YES];
// 5. 设置代理
_webView.delegate = self;
[self.view addSubview:_webView];
webViewDelegate
将要加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
已经开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
}
加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}
加载有误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"%@",error);
}
MPMoviePlayerViewController
#import <MediaPlayer/MediaPlayer.h>
// 1. 获取视频路径(沙盒路径)
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
// 2. 获取视屏的URL
NSURL *url = [NSURL fileURLWithPath:path];
// 3. 判断是否存在MPMoviePlayerViewController
if (!_moviePlayerVC) {
_moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
}
// 4.模态推出
[self presentViewController:_moviePlayerVC animated:YES completion:nil];
// 5. 设置播放的样式
_moviePlayerVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
// 6. 开始播放
[_moviePlayerVC.moviePlayer play];
UITextView 和 UITextField
scrollView
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
_scrollView.backgroundColor = [UIColor redColor];
#pragma mark - _scollView 可以显示的区域 参数1:
_scrollView.contentSize = CGSizeMake(screenW *7, 0);
[self.view addSubview:_scrollView];
#pragma mark - 是否分页显示
_scrollView.pagingEnabled = YES;
#pragma mark - 滚动条的隐藏与显示
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
#pragma mark - 弹簧效果
_scrollView.bounces = NO;
#pragma mark - 不用手指滑动
// _scrollView.scrollEnabled = NO;
#pragma
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
#pragma mark - 加个Label
// UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(90, 20, 178, 20)];
// labe.backgroundColor = [UIColor blackColor];
// [_scrollView addSubview:labe];
#pragma mark - 加个picture
for (int i = 0; i< 8; i++) {
CGFloat imageViewW = screenW;
CGFloat imageViewX = screenW * (i - 1);
CGFloat imageViewH = self.view.frame.size.height;
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageViewX, 0, imageViewW, imageViewH)];
NSString *imagename = [NSString stringWithFormat: @"%d.jpg",i];
imageView.image = [UIImage imageNamed:imagename];
[_scrollView addSubview:imageView];
}
pagecontrol
_pagecontrol = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 100 , screenW, 40)];
#pragma mark - 添加个数
_pagecontrol.numberOfPages = 7;
#pragma mark - 颜色
_pagecontrol.pageIndicatorTintColor = [UIColor redColor];// 未选中
_pagecontrol.currentPageIndicatorTintColor = [UIColor greenColor];// 选中
#pragma mark - 交互
_pagecontrol.userInteractionEnabled = NO;
#pragma mark - 点击事件
[self.view addSubview:_pagecontrol];
5.单例
#pragma mark - 1.定义一个静态变量
static JianBingGuoZi *_guozi;
@implementation JianBingGuoZi
#pragma mark - 2.s、实现shared方法
+ (instancetype)sharedJianBingGuoZi{
if (_guozi == nil) {
_guozi = [[JianBingGuoZi alloc]init];
}
return _guozi;
}
#pragma mark - 3.重写allocWithZone方法 线程俺去
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t once;
dispatch_once(&once, ^{
_guozi = [super allocWithZone:zone];
});
return _guozi;
}