UIEvent 基本学习
2015-06-02 23:37
330 查看
UIEvent 事件, 是由硬件捕获的一个表示用户操作设备的对象, 事件主要有三类: 触摸, 晃动, 远程控制。触摸事件最重要,一个触摸事件可以包含多个 UITouch.
由UIResponder 获得相应的对象,并可以进行处理, 所有处理都是UIResponder的子类实现。
当模拟器运行起来后触摸事件一直都在发生,只是会丢弃没有必要的响应操作。
UITouch 表示手指触摸事件, UIView 支持触摸事件,需要实现的基本方法:
touchesBegan:withEvent 触摸事件发生
touchesMove:withEvent 触摸事件移动
touchesEnded:withEvent 触摸事件结束
touchesCancelled:withEvent 触摸事件取消
支持移动的方法
motionBegan:withEvent
motionEnded:withEvent
motionCancelled:withEvent
支持远程控制的方法
remoteControlReceivedWithEvent
这些方法实现后才能实现触摸事件的响应。
新建一个 touchView的类
实现上述函数
#import "TouchView.h"
@implementation TouchView
- (id) initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
}
return
self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Begin\n");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Move\n");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Ended\n");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Cancelled\n");
}
@end
把这个TouchView 添加到 UIViewControler中
TouchView *aView = [[TouchView
alloc] initWithFrame:CGRectMake(40,
20, 300,
400)];
[aView setBackgroundColor:[UIColor
redColor]];
[self.view
addSubview:aView];
此时运行后,在模拟器红色区域点击,移动就会看到有log输出, 但是此时不会调用cancelled的方法, 这个问题应该是 当我们正在使用过程中突然来电话短信等,打断
你当前的操作,就会调用该方法。
在- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event 添加
[self
setBackgroundColor:[UIColor
colorWithRed:(arc4random() %
256)/255.0
green:(arc4random() %
256)/255.0
blue:(arc4random() %
256)/255.0
alpha:(arc4random() %
256)/255.0]];
arch4random是产生随机数,这样每次都可以得到不同的颜色
每点击一次就会出现一概随机的颜色。
移动实现方法 移动一概View时,可以移动Frame位置,或者是移动中心(center)。
实现移动时要先得到手指的位置, 在touchesBegan中,有几个手指都存在NSSet中,这是一概数组。
首先定义 UITouch 变量获得 当前的一个点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self
setBackgroundColor:[UIColor
colorWithRed:(arc4random() %
256)/255.0
green:(arc4random() %
256)/255.0
blue:(arc4random() %
256)/255.0
alpha:(arc4random() %
256)/255.0]];
UITouch *touch = [touches
anyObject];
StartPoint = [touch
locationInView:self];
NSLog(@"ponint = %@",
NSStringFromCGPoint(StartPoint));
NSLog(@"touch Begin\n");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Move\n");
UITouch *moveTouch = [touches
anyObject];
CGPoint curPoint = [moveTouch
locationInView:self];
CGFloat offSetX = curPoint.x -
StartPoint.x;
CGFloat offSetY = curPoint.y -
StartPoint.y;
CGPoint newCenter =
self.center;
newCenter.x = newCenter.x + offSetX;
newCenter.y = newCenter.y + offSetY;
[self
setCenter:newCenter];
NSLog(@"point offsex = %f, offsety= %f ", offSetX, offSetY);
NSLog(@"ponint = %@",
NSStringFromCGPoint(curPoint));
}
这段代码可以移动当前的View, StartPoint 为全局变量, 用于记录最开始点击的点,主要原理是 移动View的中心点,这样就可以在 began 和 move方法中使用了
由UIResponder 获得相应的对象,并可以进行处理, 所有处理都是UIResponder的子类实现。
当模拟器运行起来后触摸事件一直都在发生,只是会丢弃没有必要的响应操作。
UITouch 表示手指触摸事件, UIView 支持触摸事件,需要实现的基本方法:
touchesBegan:withEvent 触摸事件发生
touchesMove:withEvent 触摸事件移动
touchesEnded:withEvent 触摸事件结束
touchesCancelled:withEvent 触摸事件取消
支持移动的方法
motionBegan:withEvent
motionEnded:withEvent
motionCancelled:withEvent
支持远程控制的方法
remoteControlReceivedWithEvent
这些方法实现后才能实现触摸事件的响应。
新建一个 touchView的类
实现上述函数
#import "TouchView.h"
@implementation TouchView
- (id) initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
}
return
self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Begin\n");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Move\n");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Ended\n");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Cancelled\n");
}
@end
把这个TouchView 添加到 UIViewControler中
TouchView *aView = [[TouchView
alloc] initWithFrame:CGRectMake(40,
20, 300,
400)];
[aView setBackgroundColor:[UIColor
redColor]];
[self.view
addSubview:aView];
此时运行后,在模拟器红色区域点击,移动就会看到有log输出, 但是此时不会调用cancelled的方法, 这个问题应该是 当我们正在使用过程中突然来电话短信等,打断
你当前的操作,就会调用该方法。
在- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event 添加
[self
setBackgroundColor:[UIColor
colorWithRed:(arc4random() %
256)/255.0
green:(arc4random() %
256)/255.0
blue:(arc4random() %
256)/255.0
alpha:(arc4random() %
256)/255.0]];
arch4random是产生随机数,这样每次都可以得到不同的颜色
每点击一次就会出现一概随机的颜色。
移动实现方法 移动一概View时,可以移动Frame位置,或者是移动中心(center)。
实现移动时要先得到手指的位置, 在touchesBegan中,有几个手指都存在NSSet中,这是一概数组。
首先定义 UITouch 变量获得 当前的一个点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self
setBackgroundColor:[UIColor
colorWithRed:(arc4random() %
256)/255.0
green:(arc4random() %
256)/255.0
blue:(arc4random() %
256)/255.0
alpha:(arc4random() %
256)/255.0]];
UITouch *touch = [touches
anyObject];
StartPoint = [touch
locationInView:self];
NSLog(@"ponint = %@",
NSStringFromCGPoint(StartPoint));
NSLog(@"touch Begin\n");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch Move\n");
UITouch *moveTouch = [touches
anyObject];
CGPoint curPoint = [moveTouch
locationInView:self];
CGFloat offSetX = curPoint.x -
StartPoint.x;
CGFloat offSetY = curPoint.y -
StartPoint.y;
CGPoint newCenter =
self.center;
newCenter.x = newCenter.x + offSetX;
newCenter.y = newCenter.y + offSetY;
[self
setCenter:newCenter];
NSLog(@"point offsex = %f, offsety= %f ", offSetX, offSetY);
NSLog(@"ponint = %@",
NSStringFromCGPoint(curPoint));
}
这段代码可以移动当前的View, StartPoint 为全局变量, 用于记录最开始点击的点,主要原理是 移动View的中心点,这样就可以在 began 和 move方法中使用了
相关文章推荐
- 指针式压力表自动读数:Auto Read the Value of Manometer
- UVa 1252 Twenty Questions
- 用MFC实现WebGUI--(CDHtmlDialog)
- 在js中获取query string 以及重写URL的函数
- [NetTopologySuite](1)线面相交
- CSS3新增属性选择器: [attribute*=value] 、[attribute^=value] 和[attribute$=value]
- DuiLib 最小框架
- Qt Gui程序显示文本用QPlainTextEdit
- iOS积累 002:UIImage的缩放与压缩
- UIView UIButton UIImageView 添加事件
- vector ,deque 和 list
- iOS开发系列--UITableView全面解析
- UILabel自适应高度和自动换行
- iOS_UITextField 基本操作
- Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'yintingting_baisi/Info.plist'.
- Android Volley完全解析(三),定制自己的Request
- iOS_UIButton 简单操作
- iOS按钮UIButton常用属性总结
- UIScrollView代理方法
- easyui换主题,并记录在cookie.以及cookie作用域介绍