UI高级控件

一.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的认识

  1. UIView的子类
  2. 是很多控件的父类
  3. 属性
    @property(nonatomic,getter=isEnabled) BOOL enabled;
    如果enabled被设为false,不是控件不可用,而是控件不可触摸
    @property(nonatomic,getter=isSelected) BOOL selected;
    @property(nonatomic,getter=isHighlighted) BOOL highlighted;
  4. 方法
  • (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容