iOS学习之UIControl
- 只要跟控制有关的控件都是继承于该类。
- UIControl这个类通常我们并不直接使用,而是使用其子类。
- 事件响应的三种形式:基于触摸、基于值、基于编辑:
// 添加一个事件 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; // 移除一个事件 - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
/** * 1.当触摸从控件内部拖动到外部时触发:UIControlEventTouchDragExit 2.当控件之内触摸抬起时触发:UIControlEventTouchUpInside 3.当控件之外触摸抬起时触发:UIControlEventTouchUpOutside 4.触摸取消事件,设备被上锁或者电话呼叫打断:UIControlEventTouchCancel 5.用户按下时触发:UIControlEventTouchDown 6.点击计数大于1时触发:UIControlEventTouchDownRepeat 7.当触摸在控件之内拖动时触发:UIControlEventTouchDragInside 8.当触摸在控件之外拖动时触发:UIControlEventTouchDragOutside 9.当触摸从控件之外拖动到内部时触发:UIControlEventTouchDragEnter 10.当控件的值发生变化时(用于滑块、分段控件等控件):UIControlEventValueChanged 11.文本控件中开始编辑时:UIControlEventEditingDidBegin 12.文本控件中的文本被改变:UIControlEventEditingChanged 13.文本控件中编辑结束时:UIControlEventEditingDidEnd 14.文本控件内通过按下回车键结束编辑时:UIControlEventEditingDidOnExit 15.所有触摸事件:UIControlEventAllTouchEvents 16.文本编辑的所有事件:UIControlEventAllEditingEvents 17.所有事件:UIControlEventAllEvents */
#pragma mark UISwitch - (void)addSwitch { // 创建对象 // 设置frame只有origin起作用,size使用系统默认大小 UISwitch *firstSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(, , , )]; // 开关风格的颜色 firstSwitch.tintColor = [UIColor lightGrayColor]; // 开的时候的颜色 firstSwitch.onTintColor = [UIColor greenColor]; // 按钮颜色 firstSwitch.thumbTintColor = [UIColor grayColor]; [firstSwitch setOn:YES animated:NO]; // 添加事件 [firstSwitch addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:firstSwitch]; } // 实现事件 - (void)firstAction:(UISwitch *)sender { if (sender.isOn) { sender.thumbTintColor = [UIColor whiteColor]; NSLog(@"开了"); }else { sender.thumbTintColor = [UIColor grayColor]; NSLog(@"关了"); } }
- UISlider是iOS中的滑块控件。
- 通常用于控制视频播放进度,控制音量等。
- 它继承于UIControl,滑块提供了一系列连续的值,滑块停在不同的位置,获取到滑块上的值也不同
@interface RootView : UIView @property (nonatomic, strong) UISlider *mySlider; @property (nonatomic, strong) UIImageView *myImageView; @end
在RootView.m中实现
#import "RootView.h" @implementation RootView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 添加子视图 [self addAllViews]; } return self; } - (void)addAllViews { // 布局slider self.mySlider = [[UISlider alloc] initWithFrame:CGRectMake(, , , )]; // 滑块最小值 self.mySlider.minimumValue = 0.0; // 滑块最大值 self.mySlider.maximumValue = ; [self addSubview:self.mySlider]; // 设置动图 self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )]; NSMutableArray *imageArray = [NSMutableArray array]; ; i <= ; i++) { NSString *str = [NSString stringWithFormat:@"%d.tiff", i]; UIImage *image = [UIImage imageNamed:str]; [imageArray addObject:image]; } // 播放的动画数组 self.myImageView.animationImages = imageArray; // 播放时间 self.myImageView.animationDuration = ; // 开始动画 [self.myImageView startAnimating]; // 添加到父视图 [self addSubview:self.myImageView]; }
在RootViewController.m中添加滑块滑动事件
#import "RootViewController.h" #import "RootView.h" @interface RootViewController () @property (nonatomic, strong) RootView *rootView; @end @implementation RootViewController - (void)loadView { self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view = self.rootView; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 添加滑杆滑动事件 [self.rootView.mySlider addTarget:self action:@selector(mySliderAction:) forControlEvents:UIControlEventValueChanged]; } - (void)mySliderAction:(UISlider *)sender { // 设置动图 self.rootView.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )]; NSMutableArray *imageArray = [NSMutableArray array]; ; i <= ; i++) { NSString *str = [NSString stringWithFormat:@"%d.tiff", i]; UIImage *image = [UIImage imageNamed:str]; [imageArray addObject:image]; } // 播放的动画数组 self.rootView.myImageView.animationImages = imageArray; // 播放时间 self.rootView.myImageView.animationDuration = 2.1 - sender.value; // 播放次数 self.rootView.myImageView.animationRepeatCount = ; // 开始动画 [self.rootView.myImageView startAnimating]; // 添加到父视图 [self.rootView addSubview:self.rootView.myImageView]; NSLog(@"%.2f", self.rootView.mySlider.value); }@end
- UISegmentedControl是iOS中常用的分段控件。
- 每个segment都能被点击,它相当于继承了若干个button。分段控件提供一栏按钮(有时称为按钮栏),但一个时刻只能激活其中一个按钮。
- 分段控件会导致用户在屏幕上看到的内容发生变化。它们常被用在不同类别的信息之间选择,或者在切换不同的视图。
#import <UIKit/UIKit.h> @interface Root : UIView // 分段选择器 @property (nonatomic, strong) UISegmentedControl *segmentControl; // 图片视图 @property (nonatomic, strong) UIImageView *myImageView; @property (nonatomic, strong) UIImageView *myImageView1; @end
#import "Root.h" @implementation Root - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 添加视图 [self addAllViews]; } return self; } // 添加视图 - (void)addAllViews { // 创建对象 self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"女神", @"男神", @"程序员"]]; // 设置属性 self.segmentControl.backgroundColor = [UIColor grayColor]; self.segmentControl.frame = CGRectMake(, , , ); // 指定被选中的分段 self.segmentControl.selectedSegmentIndex = ; self.segmentControl.tintColor = [UIColor colorWithRed: / alpha:]; [self.segmentControl setTitle:]; // 添加到父视图 [self addSubview:self.segmentControl]; // 布局图片视图 self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.segmentControl.frame) + , , )]; // 设置默认图片 self.myImageView.image = [UIImage imageNamed:@"女神.jpg"]; [self addSubview:self.myImageView]; } @end
#import "RootViewController.h" #import "Root.h" @interface RootViewController () @property (nonatomic, strong) Root *root; @end @implementation RootViewController - (void)loadView { self.root = [[Root alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view = self.root; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.root.segmentControl addTarget:self action:@selector(segmentControlAction:) forControlEvents:UIControlEventValueChanged]; } - (void)segmentControlAction:(UISegmentedControl *)sender { NSInteger index = sender.selectedSegmentIndex; switch (index) { : self.root.myImageView.image = [UIImage imageNamed:@"女神.jpg"]; [self.root addSubview:self.root.myImageView]; break; : self.root.myImageView.image = [UIImage imageNamed:@"男神.jpg"]; [self.root addSubview:self.root.myImageView]; break; : self.root.myImageView.image = [UIImage imageNamed:@"屌丝.jpg"]; [self.root addSubview:self.root.myImageView]; break; default: break; } } @end
#import "RootViewController.h" @interface RootViewController () @property (nonatomic, strong) UIPageControl *myPage; @property (nonatomic, strong) UIView *pageView; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addPageControl]; // 添加事件 [self.myPage addTarget:self action:@selector(myPageAction:) forControlEvents:UIControlEventValueChanged]; } #pragma mark - UIPageControl - (void)addPageControl { self.myPage = [[UIPageControl alloc] initWithFrame:CGRectMake(, self.view.frame.size.height - , self.view.frame.size.width - , )]; // 设置页数 self.myPage.numberOfPages = ; // 设置当前页 self.myPage.currentPage = ; self.myPage.backgroundColor = [UIColor grayColor]; //如果是单页就隐藏 self.myPage.hidesForSinglePage = YES; // 选中的圆点颜色 self.myPage.currentPageIndicatorTintColor = [UIColor whiteColor]; // 未选中的圆点颜色 self.myPage.pageIndicatorTintColor = [UIColor blackColor]; [self.view addSubview:self.myPage]; self.pageView = [[UIView alloc] initWithFrame:CGRectMake(, self.view.frame.size.height - , , )]; self.pageView.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.pageView]; } // 实现事件 - (void)myPageAction:(UIPageControl *)page { NSInteger index = page.currentPage; switch (index) { : self.pageView.backgroundColor = [UIColor grayColor]; break; : self.pageView.backgroundColor = [UIColor cyanColor]; break; : self.pageView.backgroundColor = [UIColor blackColor]; break; : self.pageView.backgroundColor = [UIColor brownColor]; break; default: break; } } @end
iOS学习之UIControl的更多相关文章
- iOS学习笔记-地图MapKit入门
代码地址如下:http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错漏 ...
- iOS学习系列 - 扩展机制category与associative
iOS学习系列 - 扩展机制category与associative category与associative作为objective-c的扩展机制的两个特性,category即类型,可以通过它来扩展方 ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
- 【原】iOS学习之事件处理的原理
在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- 【原】iOS学习47之第三方-FMDB
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...
- iOS学习路线图
一.iOS学习路线图 二.iOS学习路线图--视频篇 阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天 学习后目标: ...
- 黑苹果-IOS学习的开始
深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...
- iOS 学习资源
这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...
随机推荐
- 洛谷P1983 车站分级
P1983 车站分级 297通过 1.1K提交 题目提供者该用户不存在 标签图论贪心NOIp普及组2013 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 求帮忙指出问题! 我这么和(diao ...
- new 、operator new 和 placement new
一.原生operator new 我们先从原生operator new开始.考虑如下代码,它用来分配5个int型的空间并返回指向他们的指针[1]: int* v = static_cast<in ...
- 【spring 6】Spring和Hibernate的整合:编程式事务
一.编程式事务简介 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用beginTransaction() ...
- C++中rapidxml用法及例子
rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml,关于这个库 ...
- EasyUI学习笔记
1,tabs获得被选中的标题 var tabTitle = $('#tabs').tabs('getSelected').panel('options').title;//获得被选中的标题 2.当设置 ...
- JavaScript 性能优化1
一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够透彻异或是自己太笨,更多的是自己不擅于 ...
- linux 文本处理
tr,awk,sed 一:tr 1.大小写转换 cat file | tr [a-z] [A-Z] > new_file(大写 --> 小写) cat file | tr [A-Z] [a ...
- hadoop 根据SecondaryNameNode恢复Namenode
1.修改conf/core-site.xml 增加 <property> <name>fs.checkpoint.period</name> <value&g ...
- hdu2072
注意输入全是0的情况. #include <stdio.h> #include <string.h> #include <algorithm> using name ...
- ubuntu 字体 android stuido 汉字 显示 方块
Ubuntu 12.04 LTS 中安装 windows 字体 ubuntu 中的中文字体看着总觉的有点不爽,于是百度了下,这里记录下怎么在 ubuntu 12.04 中安装 windows 字体 ...