1响应者对象
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”
UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
2.UIResponder
UIResponder
内部提供了以下方法来处理事件
触摸事件
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event;
- ( void )touchesMoved:( NSSet *)touches withEvent:( UIEvent *)event;
- ( void )touchesEnded:( NSSet *)touches withEvent:( UIEvent *)event;
- ( void )touchesCancelled:( NSSet *)touches withEvent:( UIEvent *)event;
加速计事件
- ( void )motionBegan:( UIEventSubtype )motion withEvent:( UIEvent *)event;
- ( void )motionEnded:( UIEventSubtype )motion withEvent:( UIEvent *)event;
- ( void )motionCancelled:( UIEventSubtype )motion withEvent:( UIEvent *)event;
远程控制事件
触摸事件
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event;
- ( void )touchesMoved:( NSSet *)touches withEvent:( UIEvent *)event;
- ( void )touchesEnded:( NSSet *)touches withEvent:( UIEvent *)event;
- ( void )touchesCancelled:( NSSet *)touches withEvent:( UIEvent *)event;
加速计事件
- ( void )motionBegan:( UIEventSubtype )motion withEvent:( UIEvent *)event;
- ( void )motionEnded:( UIEventSubtype )motion withEvent:( UIEvent *)event;
- ( void )motionCancelled:( UIEventSubtype )motion withEvent:( UIEvent *)event;
远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
手指按下
- ( void )touchesBegan:
手指移动
- ( void )touchesMoved:
手指抬起
- ( void )touchesEnded:
意外中断事件(电话打扰)
- ( void )touchesBegan:
手指移动
- ( void )touchesMoved:
手指抬起
- ( void )touchesEnded:
意外中断事件(电话打扰)
- (
void
)touchesCancelled:
2. NSSet和NSArray的区别
* NSSet:
1> 无序的、不重复的。存放到 NSSet 中的内容并不会排序与添加顺序也没有关系
2> 通过 anyObject 来访问单个元素
3> 遍历 NSSet 中的每个元素
4> 好处 : 效率高。
5> 应用场景 :
* 比如重用 Cell 的时候 , 从缓存池中随便获取一个就可以了 , 无需按照指定顺序来获取
1> 无序的、不重复的。存放到 NSSet 中的内容并不会排序与添加顺序也没有关系
2> 通过 anyObject 来访问单个元素
3> 遍历 NSSet 中的每个元素
4> 好处 : 效率高。
5> 应用场景 :
* 比如重用 Cell 的时候 , 从缓存池中随便获取一个就可以了 , 无需按照指定顺序来获取
* 当需要把数据存放到一个集合中, 然后判断集合中是否有某个对象的时候
* NSArray
1> 有序的、可以有重复对象。对象的顺序是按照添加的顺序来保存的
2> 好处 : 有序访问
3> 应用场景 : 在绝大多数需要依赖顺序的情况下(比如 tableView 的数据源集合,在实际操作中要根据下标来获取对象)
1> 有序的、可以有重复对象。对象的顺序是按照添加的顺序来保存的
2> 好处 : 有序访问
3> 应用场景 : 在绝大多数需要依赖顺序的情况下(比如 tableView 的数据源集合,在实际操作中要根据下标来获取对象)
3> 通过下标来访问
UITouch
当用户用一根手指触摸屏幕时,会创建一个与手指相关联的
UITouch
对象
一根手指对应一个 UITouch 对象
UITouch 的作用
保存着跟本次手指触摸相关的信息,比如触摸的位置、时间、阶段
当手指移动时,系统会更新同一个 UITouch 对象,使之能够一直保存该手指在的触摸位置
一根手指对应一个 UITouch 对象
UITouch 的作用
保存着跟本次手指触摸相关的信息,比如触摸的位置、时间、阶段
当手指移动时,系统会更新同一个 UITouch 对象,使之能够一直保存该手指在的触摸位置
当手指离开屏幕时,系统会销毁相应的
UITouch
对象
UIEvent
每产生一个事件,就会产生一个
UIEvent
对象
UIEvent :称为事件对象,记录事件产生的 时刻 和 类型
常见属性
事件类型
@property ( nonatomic , readonly ) UIEventType type;
@property ( nonatomic , readonly ) UIEventSubtype subtype;
事件产生的时间
@property ( nonatomic , readonly ) NSTimeInterval timestamp;
UIEvent :称为事件对象,记录事件产生的 时刻 和 类型
常见属性
事件类型
@property ( nonatomic , readonly ) UIEventType type;
@property ( nonatomic , readonly ) UIEventSubtype subtype;
事件产生的时间
@property ( nonatomic , readonly ) NSTimeInterval timestamp;
UIEvent
还提供了相应的方法可以获得在某个
view
上面的触摸对象(
UITouch
)
1.实现单点触摸的例子
#import "CZView.h"
@implementation CZView
// 触摸 view 的时候调用
- ( void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// 获取触摸对象
UITouch* t = touches.anyObject;
// 获取当前手指的位置
CGPoint loc = [t locationInView:self.superview];
// 让view的center 为 手指的位置
self.center = loc;
}
// 在view上移动的时候 调用
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// 获取触摸对象
UITouch* t = touches.anyObject;
// 获取每一次移动的手指的位置
CGPoint loc = [t locationInView:self];
// 获取上一次的点
CGPoint lastLoc = [t previousLocationInView:self];
// 计算偏移量
CGFloat offsetX = loc.x - lastLoc.x;
CGFloat offsetY = loc.y - lastLoc.y;
self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
}
@end
实现效果:
点击yellowView进行移动
二.单点触碰的第二个例子
添加一个白色的View的类
// (3)手指在白色view移动的时候 只有橙色x轴跟着移动
// 获取触摸的对象
UITouch* t = touches.anyObject;
// 获取当前的位置
CGPoint loc = [t locationInView:self];
// 获取上一次的位置
CGPoint lastLoc = [t previousLocationInView:self];
// 计算偏移量
CGFloat offsetX = loc.x - lastLoc.x;
CGFloat offsetY = loc.y - lastLoc.y;
// 让橙色view的center在自己的基础上 加上偏移量
// 获取触摸的对象
UITouch* t = touches.anyObject;
// 获取当前的位置
CGPoint loc = [t locationInView:self];
// 获取上一次的位置
CGPoint lastLoc = [t previousLocationInView:self];
// 计算偏移量
CGFloat offsetX = loc.x - lastLoc.x;
CGFloat offsetY = loc.y - lastLoc.y;
// 让橙色view的center在自己的基础上 加上偏移量
self.orangeView.center = CGPointMake(self.orangeView.center.x + offsetX, self.orangeView.center.y + 0);
实现效果:
三. 事件的产生和传递
使用的是touchesBegin
实现效果
点击白色 输出白色
点击绿色 输出白色->黄色->绿色
点击蓝色 输出白色->黄色->蓝色
点击红色 输出白的->黄色->蓝色->红色
点击橙色 输出白的->黄色->蓝色->橙色
四 hittext
五.使用color的属性创建image
//
设置背景图片
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_refresh_bg"]];
六:设置样式
//
设置样式
[path setLineWidth : 10 ]; // 线宽
[path setLineCapStyle : kCGLineCapRound ]; // 头尾
[path setLineJoinStyle : kCGLineJoinRound ]; // 连接处
[path setLineWidth : 10 ]; // 线宽
[path setLineCapStyle : kCGLineCapRound ]; // 头尾
[path setLineJoinStyle : kCGLineJoinRound ]; // 连接处
[[UIColor whiteColor] set]; // 颜色
七布局九宫格
// 布局九宫格
- ( void)layoutSubviews
{
[super layoutSubviews];
CGFloat w = 74;
CGFloat h = w;
int colCount = 3;
CGFloat margin = (self.frame.size.width - 3 * w) / 4;
for (int i = 0; i < self.btns.count; i++) {
CGFloat x = (i % colCount) * (margin + w) + margin;
CGFloat y = (i / colCount) * (margin + w) + margin;
[self.btns[i] setFrame:CGRectMake(x, y, w, h)];
}
}
八:手势识别
一般创建手势识别一般都市这三步
1.创建一个手势的对象
2.把手指的对象添加到需要的手手势的view中
3.实现手势方法
1.
UITapGestureRecognizer(
敲击
)-------------
UITapGestureRecognizer* tap = [[UITapGestureRecognizer
loc] initWithTarget:self action:@selector(tap:)];
// 几根手指
tap.numberOfTouchesRequired = 2;
// 点几次
tap.numberOfTapsRequired = 2;
// 2.对imageView添加手势
loc] initWithTarget:self action:@selector(tap:)];
// 几根手指
tap.numberOfTouchesRequired = 2;
// 点几次
tap.numberOfTapsRequired = 2;
// 2.对imageView添加手势
[self.imageView addGestureRecognizer:tap];
2.
UISwipeGestureRecognizer(
轻扫
)-------------
UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 往左滑
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// 2.
[self.imageView addGestureRecognizer:swipe];
UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
// 往左滑
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// 2.
[self.imageView addGestureRecognizer:swipe];
[self.imageView addGestureRecognizer:swipe1];
3
//UILongPressGestureRecognizer(
长按
)-------------
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 长按多长时间执行方法
longPress.minimumPressDuration = 2;
// 误差
longPress.allowableMovement = 10;
// 2.
[self.imageView addGestureRecognizer:longPress];
// 长按多长时间执行方法
longPress.minimumPressDuration = 2;
// 误差
longPress.allowableMovement = 10;
// 2.
[self.imageView addGestureRecognizer:longPress];
4.
UIRotationGestureRecognizer(
旋转
)-------------
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
// 2.
// 2.
[self.imageView addGestureRecognizer:rotation]
5.
UIPinchGestureRecognizer(
捏合,用于缩放
)-------------
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 2.
// 2.
[self.imageView addGestureRecognizer:pinch];
6.
UIPanGestureRecognizer(
拖拽
)-------------
UIPanGestureRecognizer
* pan = [[
UIPanGestureRecognizer
alloc
]
initWithTarget
:
self
action
:
@selector
(pan:)];
// 2.
// 2.
[self.imageView addGestureRecognizer:pan];
7.解决手势冲突的情况下
让旋转称为代理
实现下面这个方法
// 解决手势冲突的问题
- ( BOOL )gestureRecognizer:( UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:( UIGestureRecognizer *)otherGestureRecognizer
{
NSLog ( @"%p --gestureRecognizer" , gestureRecognizer);
return YES;