一.UIScrollView
1. 为什么使用UIScrollView?
手机屏幕有限,又想展示更多的内容
2. UIScrollView是如何实现分屏展示的?
Frame属性:可见内容
Contentsize属性:我们需要展示的内容
3. UIScrollView基本原理与使用
//
// ViewController.m
// UIScrollView
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self scrollView1];
[self scrollView2];
}
//属性
-(void)scrollView1 {
// 1 Frame:可见内容 ContentSize:UIScrollView的整个内容
UIScrollView *scrollView1 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 414, 200)];
// 2 ContentSize:UIScrollView的整个内容
scrollView1.contentSize = CGSizeMake(414*5, 200);
//偏移量,让scrollView打开的时候就处于这个位置,这个点是相对于左上角的
scrollView1.contentOffset = CGPointMake(414, 0);
scrollView1.backgroundColor = [UIColor grayColor];
// 指示光标,就是滑动时候下面的指示条
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// 滑动是否有反弹效果
scrollView1.bounces = true;
//分页效果,本例contextSize为5个屏幕大小,它表示每次移动一个屏幕大小
scrollView1.pagingEnabled = true;
//使能效果,为true,表明当前可以滑动
scrollView1.scrollEnabled = true;
scrollView1.delegate = self;
//3 add
[self.view addSubview:scrollView1];
//在第二页创建一个label
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(414, 0, 414, 50)]; //y写0是因为一会直接添加到scrollView上,而不是self.view
lab.text = @"Hello";
lab.textColor = [UIColor redColor];
lab.backgroundColor = [UIColor greenColor];
lab.font = [UIFont systemFontOfSize:12.0];
[scrollView1 addSubview:lab];
}
//UIScrollView滚动的时候回不停的回调这个方法
//1 精度高 2 一般用于打印滚动坐标 3 并不是线性增长的,x从1到3和从3到5时间不一定相同
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
float x = scrollView.contentOffset.x;
float y = scrollView.contentOffset.y;
NSLog(@"%f %f",x,y);
}
//大图展示 1 UIScrollView 2 UIImageView 3 将UIImageView添加到UIScrollView
//4 将UIScrollView添加到self.view
-(void)scrollView2 {
/*
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
[self.view addSubview:imageView];
//NSStringFromCGSize:打印CGSize
NSLog(@"image:%@",NSStringFromCGSize(imageView.frame.size));
//image:{1440, 900}
NSLog(@"s creen:%@",NSStringFromCGSize([UIScreen mainScreen].bounds.size));
//screen:{414, 736}
//只能展示图片的一个角,这种方法不好
*/
//如果frame和contextSize设置大小一样,不能滚动
UIScrollView *scrollView1 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 414, 200)];
scrollView1.contentSize = CGSizeMake(1440, 900);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1440, 900)];
imageView.image = [UIImage imageNamed:@"1.jpg"];
scrollView1.backgroundColor = [UIColor grayColor];
[scrollView1 addSubview:imageView];
[self.view addSubview:scrollView1];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4. UIScrollView循环滚动案例
//无限循环滚动
//1 时刻监听回调
//2 在合适的位置实现从最后一页回到首页(跳转逻辑)2 -> 0
//3 为了使循环滚动效果更加连续,只需要3个页面,但设置5个页面,由012 ->20120
-(void)scrollView3 {
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 414, 200)];
scrollView.contentSize = CGSizeMake(414*5, 200);
scrollView.delegate = self;
scrollView.pagingEnabled = true;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
label.backgroundColor = [UIColor redColor];
label.text = @"2";
[scrollView addSubview:label];
//为了使初始页面在label.text为0的页面(是整个页面的第2页)
scrollView.contentOffset = CGPointMake(414, 0);
for (int i = 0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(414*(i+1), 0, 100, 100)];
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%d",i%3];//0 1 2 0
[scrollView addSubview:label];
}
[self.view addSubview:scrollView];
}
//UIScrollView滚动的时候回不停的回调这个方法
//1 精度高 2 一般用于打印滚动坐标 3 并不是线性增长的,x从1到3和从3到5时间不一定相同
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
float x = scrollView.contentOffset.x;
// float y = scrollView.contentOffset.y;
if (x == 414*4) {
CGPoint point = CGPointMake(414, 0);
scrollView.contentOffset = point;
} else if (x == 0) {
CGPoint point = CGPointMake(414*3, 0);
scrollView.contentOffset = point;
}
}
新闻轮播图
// ipnone 5s
// ViewController.m
// NewsCarousel
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate> {
UIScrollView *scrollView;
NSTimer *timer;
int imageIndex;//当前播放到了哪一章
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self scrollView5];
}
//1 每次拖拽 展示一张图片
//2 自动播放一张图片
//3 UIScrollView UIImageView NSTimer 循环滚动
-(void)scrollView5 {
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
scrollView.contentSize = CGSizeMake(320*4, 200);
scrollView.backgroundColor = [UIColor clearColor];
scrollView.pagingEnabled = true;
scrollView.delegate = self;
for (int index = 0; index<4; index++) {
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(index*320, 0, 320, 200)];
NSString *imagePath = [NSString stringWithFormat:@"%d.jpg",index+1];
imageview.image = [UIImage imageNamed:imagePath];
[scrollView addSubview:imageview];
}
[self.view addSubview:scrollView];
timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(imagePlay) userInfo:nil repeats:true];
imageIndex = 0;
}
-(void)imagePlay {
scrollView.contentOffset = CGPointMake(imageIndex*320, 0);
imageIndex++;
if (imageIndex >= 4) {
imageIndex = 0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
属性补充
scrollView.layer.borderColor = [UIColor redColor].CGColor;
scrollView.layer.borderWidth = 1.0;
scrollView.layer.cornerRadius = 10.0;
二.UISearchBar快速集成
//
// ViewController.m
// UISearchBar
#import "ViewController.h"
@interface ViewController ()<UISearchBarDelegate> {
NSArray *array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self searchBar1];
[self searchBar2];
}
- (void)searchBar1 {
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 100, 414, 50)];
searchBar.showsCancelButton = true; //cancel按钮是否显示
searchBar.delegate = self;
searchBar.keyboardAppearance = true;
//补充属性
searchBar.showsScopeBar = true;//在searchBar下方添加两个按钮,分别为"a""b"
array = @[@"a",@"b"];
searchBar.scopeButtonTitles = array;
[self.view addSubview:searchBar
];
}
//点击cancel触发
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"cancel");
[searchBar resignFirstResponder];//键盘收起
}
//点击search触发,搜索需要查询的内容
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"%@将在数据库中进行搜索",searchBar.text);
[searchBar resignFirstResponder];
}
//补充属性
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
NSLog(@"selectedScope = %ld",(long)selectedScope);
NSLog(@"array = %@",[array objectAtIndex:selectedScope]);
}
//自定义searchBar
- (void)searchBar2 {
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 50, 414, 50)];
searchBar.showsCancelButton = true;
searchBar.showsBookmarkButton = true; //书签是否显示
searchBar.delegate = self;
searchBar.backgroundImage = [UIImage imageNamed:@"searchbarbg.png"];
//forSearchBarIcon:UISearchBarIconBookmark作用在书签按钮上
[searchBar setImage:[UIImage imageNamed:@"bookmarkicon.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
[searchBar setImage:[UIImage imageNamed:@"bookmarkiconhighlighted.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateHighlighted];
[self.view addSubview:searchBar];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
三.UI高级控件拓展
1. UISegmentControl
UISegmentedControl分段控件代替了桌面OS上的单选按钮。不过它的选项个数非常有限,因为你的IOS设备屏幕有限。当我们需要使用选项非常少的单选按钮时它很合适。
- (void)segmentControl1 {
//items:内容
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[[UIImage imageNamed:@"check.png"],[UIImage imageNamed:@"search.png"],[UIImage imageNamed:@"tools.png"]]];
segment.frame = CGRectMake(0, 80, 320, 30);
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
}
- (void)segmentAction:(UISegmentedControl *)msegment{
NSLog(@"%ld",(long)[msegment selectedSegmentIndex]);
}
UISegmentControl
2. UIPageControl
- (void)pageControl1 {
UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 160, 414, 20)];
page.numberOfPages = 10;//共有多少页
page.currentPage = 3;//当前位于第几页
page.backgroundColor = [UIColor grayColor];
[self.view addSubview:page];
}
UIPageControl.jpeg
3. UITextView
- (void)textView1 {
textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 250, 200, 100)];
textView.textColor = [UIColor blackColor];
textView.font = [UIFont systemFontOfSize:18.0];
textView.delegate = self;
textView.text = @"textViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextViewtextView";
textView.scrollEnabled = true;//滚动属性
[self.view addSubview:textView];
}
//点空白处调用
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[textView resignFirstResponder];
}
//编辑结束的时候调用
- (void)textViewDidEndEditing:(UITextView *)textView {
NSLog(@"%@",textView.text);
}
四.UIControl的认识
- UIView的子类
- 是很多控件的父类
- 属性
@property(nonatomic,getter=isEnabled) BOOL enabled;
如果enabled被设为false,不是控件不可用,而是控件不可触摸
@property(nonatomic,getter=isSelected) BOOL selected;
@property(nonatomic,getter=isHighlighted) BOOL highlighted; - 方法
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;