1.触摸事件
1 #import "ViewController.h" 2 @interface ViewController () 3 @property (strong,nonatomic) UILabel *simple; 4 @end 5 6 @implementation ViewController 7 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 // Do any additional setup after loading the view, typically from a nib. 11 self.view.backgroundColor = [UIColor whiteColor]; 12 self.simple = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 13 self.simple.center = self.view.center; 14 self.simple.backgroundColor = [UIColor magentaColor]; 15 //设置圆角 16 self.simple.layer.cornerRadius = 20; 17 self.simple.layer.masksToBounds = YES; 18 // self.simple.layer.shadowOffset = CGSizeMake(4, 5); 19 // self.simple.layer.shadowColor = [UIColor blackColor].CGColor; 20 // self.simple.layer.borderColor = [UIColor magentaColor].CGColor; 21 // self.simple.layer.borderWidth = 5; 22 [self.view addSubview:self.simple]; 23 } 24 //开始触摸 25 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 26 { 27 #define KWidth self.view.bounds.size.width 28 #define KHeight self.view.bounds.size.height 29 #define KMax 300 30 #define KMin 30 31 self.simple.backgroundColor = [UIColor yellowColor]; 32 //随机大小 33 CGFloat side = arc4random()%(KMax - KMin + 1) +KMin; 34 self.simple.bounds = CGRectMake(0, 0, side, side); 35 self.simple.layer.cornerRadius = side / 2; 36 self.simple.layer.masksToBounds = YES; 37 38 //随机位置 39 CGFloat x = arc4random()%(NSInteger)(KWidth - 50 + 1) + 50; 40 CGFloat y = arc4random()%(NSInteger)(KHeight - 50 + 1) + 50; 41 42 self.simple.center = CGPointMake(x, y); 43 44 45 NSLog(@"我要开始摸了"); 46 } 47 //移动触摸 48 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 49 { 50 #define KColor arc4random()% 256/255.0 51 self.simple.backgroundColor = [UIColor colorWithRed:KColor green:KColor blue:KColor alpha:1]; 52 53 //获取触摸设备的对象 54 UITouch *touch = [touches anyObject]; 55 //获取前一个点 56 CGPoint p1 = [touch previousLocationInView:self.view]; 57 //获取当前的点 58 CGPoint p2 = [touch locationInView:self.view]; 59 //根据两个点的纵向距离来计算出移动结果,并复制给center,更新位置 60 //self.view.center = CGPointMake(self.view.center.x + p2.x - p1.x, self.view.center.y + p2.y - p1.y); 61 62 self.simple.center = CGPointMake(p2.x, p2.y); 63 64 NSLog(@"左右摸一摸"); 65 } 66 //结束触摸 67 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 68 { 69 //随机颜色 70 #define KColor arc4random()% 256/255.0 71 // self.simple.backgroundColor = [UIColor colorWithRed:KColor green:KColor blue:KColor alpha:1]; 72 //self.simple.backgroundColor = [UIColor greenColor]; 73 NSLog(@"哎呀,不摸了"); 74 } 75 //取消触摸 76 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 77 { 78 //这个事件点击屏幕不松手,然后把程序转入后台才会触发,在实际中触发改事件一般有来个电话什么的 79 NSLog(@"取消触摸"); 80 } 81 - (void)didReceiveMemoryWarning { 82 [super didReceiveMemoryWarning]; 83 // Dispose of any resources that can be recreated. 84 } 85 86 @end
2.响应者链和手势
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @property (strong,nonatomic) UIView *simpleView; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view, typically from a nib. 12 self.view.backgroundColor = [UIColor redColor]; 13 self.simpleView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; 14 self.simpleView.backgroundColor = [UIColor magentaColor]; 15 [self.view addSubview:self.simpleView]; 16 17 //开启页面交互 18 //userInteractionEnabled 是可以中断响应者链的 19 //lable imageView是默认关闭的 20 self.view.userInteractionEnabled = YES; 21 22 UIImageView *image = [[UIImageView alloc] init]; 23 image.userInteractionEnabled = NO; 24 //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapAction:)]; 25 // //设置点击次数 26 // tap.numberOfTapsRequired = 1; 27 // //设置手指个数 28 // tap.numberOfTouchesRequired = 2; 29 // //添加手势控制器到视图上 30 // [self.simpleView addGestureRecognizer:tap]; 31 //长按手势 32 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongAction)]; 33 //设置长按时间 34 longPress.minimumPressDuration = 1; 35 [self.simpleView addGestureRecognizer:longPress]; 36 37 //平移手势 38 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; 39 [self.simpleView addGestureRecognizer:pan]; 40 41 42 //捏合手势 43 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(PinchAction:)]; 44 [self.simpleView addGestureRecognizer:pinch]; 45 46 //旋转手势 47 UIRotationGestureRecognizer *rotion = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotionAction:)]; 48 [self.simpleView addGestureRecognizer:rotion]; 49 50 } 51 //旋转手势事件 52 -(void)rotionAction:(UIRotationGestureRecognizer *)rotion 53 { 54 rotion.view.transform = CGAffineTransformMakeRotation(rotion.rotation); 55 NSLog(@"头有点转晕了"); 56 } 57 58 //捏合手势事件 59 -(void)PinchAction:(UIPinchGestureRecognizer *)pin 60 { 61 pin.view.transform = CGAffineTransformMakeScale(pin.scale, pin.scale); 62 NSLog(@"你是不是二哈"); 63 } 64 //平移事件 65 -(void)panAction:(UIPanGestureRecognizer *)pan 66 { 67 //pan.view 中的view是上边的self.simpleView ,由于pan添加到了simpleView上,所以能控制它 68 CGPoint p = [pan translationInView:pan.view]; 69 pan.view.transform = CGAffineTransformMakeTranslation(p.x, p.y); 70 NSLog(@"你是猪吗?"); 71 } 72 73 //长按手势事件 74 -(void)LongAction 75 { 76 NSLog(@"你是英雄...."); 77 } 78 79 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 80 { 81 // NSLog(@"%@",self.simpleView.nextResponder); 82 // NSLog( @"%@",self.simpleView.nextResponder.nextResponder); 83 // NSLog(@"%@",self.simpleView.nextResponder.nextResponder.nextResponder); 84 // NSLog(@"%@",self.simpleView.nextResponder.nextResponder.nextResponder.nextResponder); 85 // NSLog(@"%@",self.simpleView.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder); 86 // NSLog(@"%@",self.simpleView.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder); 87 88 } 89 - (void)didReceiveMemoryWarning 90 { 91 [super didReceiveMemoryWarning]; 92 // Dispose of any resources that can be recreated. 93 } 94 95 @end