首页 > 代码库 > UIEvent

UIEvent

// UIEvent:是由设备捕捉到用户对硬件的操作,每个时间都是一个UIevent对象
//iOS中的时间有三种:触摸事件,摇晃时间,以及远程控制事件
//触摸事件:是由用户对屏幕通过接触产生的事件
//对于UIView或者UIView的子类,都能接收到触摸事件,只是没有对于接触事件做出响应
//iOS支持多点触摸  如果一个视图想要对触摸事件做出响应,只需在该类中实现touchBegan:touchEnded:touchMoved:  等触摸方法.
//修改自身视图颜色

- (void)changeSelfColor
{
    self.backgroundColor = [UIColor randomColor];
}
//改变自身位置
- (void)changeSelfCenter:(CGPoint )point
{
    self.center = CGPointMake((arc4random()%(270-50+1)+50),(arc4random()%(518-50+1)+50));
    self.center = CGPointMake(point.x, point.y);
}
//修改子类视图颜色
- (void)changeSubViewColor
{
    self.superview.backgroundColor = [UIColor randomColor];
}

//当手指触摸屏幕时触发(刚开始接触)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__FUNCTION__);
    //touches 存储触摸屏幕的手指对象
    //每一个手指对象都是一个UITouch类型的对象
    //  NSLog(@"%lu",(unsigned long)[touches count]);
    //单击改变自身颜色 双击修改子视图的颜色
    //1.获取手指对象
    UITouch * touch = [touches anyObject];
    //2.获取点击次数
    NSLog(@"%g",touch.timestamp);
    if ([touch tapCount] == 1) {
        //        [self changeSelfColor];
        
        //延迟执行 performSelector
        [self performSelector:@selector(changeSelfColor) withObject:nil afterDelay:0.25];
    } else if ([touch tapCount] == 2)
    {
        //取消之前的操作.取消之前的任务
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelfColor) object:nil];
        
        [self changeSubViewColor];
    }
    
    
    
}
//当手指在屏幕滑动时触发
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__FUNCTION__);
    //  UITouch * touch  = [touches anyObject];
    //  CGPoint point = [touch locationInView:self.view];
    //  [self changeSelfCenter:point];
    
    
}

//当手指离开屏幕时触发(触摸结束时)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__FUNCTION__);
}
//当触摸被取消时,前提是手指触摸屏幕(如:来电话时)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__FUNCTION__);
}

UIEvent