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 学习路线, 更快的入门, 更准确的定位的 ...
随机推荐
- MacOSX和Windows 8的完美融合
MacOSX和Windows8的完美融合 一般情况下我们要在MACOS系统下运行Windows软件怎么办呢?一种方法我们可以装CrossOver这款软件,然后在configuration->in ...
- PAT1030. Travel Plan
//晴神模板,dij+dfs,貌似最近几年PAT的的图论大体都这么干的,现在还在套用摸板阶段....估计把这及格图论题题搞完,dij,dfs,并查集就掌握差不多了(模板还差不多)为何bfs能自己干出来 ...
- HTTP协议请求方式: 中GET、POST和HEAD的介绍以及错误提示码
HTTP协议中GET.POST和HEAD的介绍 2008-05-10 14:15 GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档 ...
- 【转载】FPGA功耗的那些事儿
在项目设计初期,基于硬件电源模块的设计考虑,对FPGA设计中的功耗估计是必不可少的. 笔者经历过一个项目,整个系统的功耗达到了100w,而单片FPGA的功耗估计得到为20w左右, 有点过高了,功耗过高 ...
- 介绍一点.NET反编译的知识
反编译是我们理解.NET内部实现的一种良好的手段. 程序编译时 Test.exe是IL代码.我们可以通过一些工具,来查看这些IL代码. 一模一样? 理论上来说,一模一样的反编译是不存在的.原因有以下3 ...
- VS2013添加NuGet的方法
1.工具->扩展和更新->联机 2.右上角搜索框搜索NuGet,选择NuGet Package Manager for Visual Studio 2013,安装后重启VS 下面通过添加N ...
- Composer -- PHP依赖管理的用法
1:下载 1.1:方法一: 通过PHP来安装 cd /home/composer curl -sS https://getcomposer.org/installer | php #这个命令会下载c ...
- js 实现获取对象所有键名(key)的方法
1.for in 循环 并且使用hasOwnProperty 方法 var jsonObject1 = { "name": "xiaoming", " ...
- [leetcode]_Symmetric Tree
第二道树的题目,依旧不会做,谷歌经验. 题目解释: give you a tree , judge if it is a symmetric tree. 思路:我以为要写个中序遍历(进阶学习非递归算法 ...
- mysql 语句其它及优化
将检索到的数据保存到文件 Select * into outfile ‘文件地址’ from tabname; 生成的文件以制表符区分字段,以换行符区分记录 为满足特殊需求会采用不同的分割方式. 支 ...