在IOS开发中我们通常会用到手势 但UIEvent 也必不可少 相比手势 更加直接
/*
uievent 是由设备部或对硬件的操作 每个事件都是一个uievent 对象
ios的事件有三种 触摸事件 摇晃事件 远程控制事件
触摸事件是由用户 都屏幕触摸产生的事件 对于 UIView或者其子类可以接收到触摸事件 只是没有作出响应
如果想要作出相响应 就要实现以下方法
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//当手指触摸屏幕时(刚开始接触)
// [self modifiySuperviewColor];
// [self modifySelfviewColor];
//touches存储触摸屏幕的手指对象,每一个手指对象都是一个UITouch的对象
NSLog(@"%lu",[touches count]);
//随机色
//单机改变自身颜色
// 1,获取手指对象
UITouch *touch1 = [touches anyObject];
// 2,获取手指点击次数
if ([touch1 tapCount] == 2) {
//取消之前的任务
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(modifySelfviewColor) object:nil];
[self performSelector:@selector(modifiySuperviewColor) withObject:nil afterDelay:0.3];
}else if([touch1 tapCount] == 1){
[self performSelector:@selector(modifySelfviewColor) withObject:nil afterDelay:0.3];
}else if([touch1 tapCount] == 3){
[self modifySelfviewLocation];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(modifiySuperviewColor) object:nil];
}
NSLog(@"%s",__FUNCTION__);
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
//当触摸被取消时 (前提是手指之前在触摸屏幕)
// 例如当电话进入时
NSLog(@"%s",__FUNCTION__);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//当手指离开屏幕时 (触摸结束)
// [self modifySelfviewLocation];
NSLog(@"%s",__FUNCTION__);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//当是一直触摸屏幕并且在视图内移动时
NSLog(@"%s",__FUNCTION__);
}
//修改自身颜色
-(void)modifySelfviewColor{
self.backgroundColor = [UIColor randomColor];
}
//修改父视图颜色
-(void)modifiySuperviewColor{
self.superview.backgroundColor = [UIColor randomColor];
}
//改变自身位置
-(void)modifySelfviewLocation{
}
#import "UIColor+RandomColor.h"
@implementation UIColor (RandomColor)
+(UIColor *)randomColor{
return [self colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];
}
@end