RootViewController
#import "RootViewController.h" #import "TouchView.h" #import "PanView.h" #import "PinchView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // TouchView *greenView = [[TouchView alloc] initWithFrame:CGRectMake(60, 184, 200, 200)]; // greenView.layer.cornerRadius = greenView.frame.size.width/2; // greenView.backgroundColor = [UIColor greenColor]; // // [self.view addSubview:greenView]; // [greenView release]; // PanView *greenView = [[PanView alloc] initWithFrame:CGRectMake(60, 184, 200, 200)]; // greenView.layer.cornerRadius = greenView.frame.size.width/2; // greenView.backgroundColor = [UIColor greenColor]; // // [self.view addSubview:greenView]; // [greenView release]; PinchView *redView = [[PinchView alloc] initWithFrame:CGRectMake(20, 40, 280, 500)]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; [redView release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIView *view = self.view.subviews[0]; view.backgroundColor = [UIColor redColor]; } @end
TouchView.m
#import "TouchView.h" #import "UIColor+Creat.h" @implementation TouchView /* 为什么定义UIView的子类?是因为当我们想让Uiview响应触摸事件的话,需要让它的.m文件中实现几个方法,UIView不能在其.m文件中实现这几个方法,因此需要定义UIView的子类 如果想让一个视图多个触摸事件作出响应,需要实现依稀几个能够响应触摸事件的方法 */ /**刚开始触摸的时候触发(手指刚碰到视图)*/ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //touches 存储手指点击对象,对应的类是UITouch //1.获取手指点击的点 UITouch *touch = [touches anyObject]; // 从集合中快速去除点击的对象 //2.获取点击的次数 if (1 == touch.tapCount) { //当识别是单击操作是,延迟执行,看是否是双击操作 [self performSelector:@selector(changeSelfColor) withObject:nil afterDelay:0.3]; //[self changeSelfColor]; }else if(2 == touch.tapCount){ //当识别是双击操作时,取消单击操作 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeSelfColor) object:nil]; [self changeSupColor]; } } //当手指在视图内移动的时候触发(前提是手指还未离开视图) - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%s,%d", __FUNCTION__,__LINE__); } //当手机离开视图时,即触摸结束. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ // NSLog(@"%s,%d", __FUNCTION__,__LINE__); // [self changeSupColor]; // [self changeSelfFrame]; } //当触摸取消时触发(触发被意外中断,比如有电话接入) - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%s,%d", __FUNCTION__,__LINE__); } //修改自身视图颜色 - (void)changeSelfColor{ self.backgroundColor = [UIColor randomeColor]; } //修改父视图颜色 - (void)changeSupColor{ self.superview.backgroundColor = [UIColor randomeColor]; } //修改自身位置 - (void)changeSelfFrame { self.center = CGPointMake(arc4random() % 101 + 100, arc4random() % 301 + 100); } //实现单击,双击 @end
PanView.m
#import "PanView.h" @implementation PanView - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //1.获取手指对象 UITouch *touch = [touches anyObject]; //2.获取手指当前位置 CGPoint currentPoint = [touch locationInView:self]; //3.获取手指之前的位置 CGPoint previousPoint = [touch previousLocationInView:self]; //4.计算移动的增量 CGFloat dx = currentPoint.x - previousPoint.x; CGFloat dy = currentPoint.y - previousPoint.y; //修改视图位置 self.center = CGPointMake(self.center.x + dx, self.center.y + dy); } @end
PinchView.m
#import "PinchView.h" @implementation PinchView - (instancetype)initWithFrame:(CGRect)frame { if(self = [super initWithFrame:frame]) { //iOS 支持多点触摸,只不过默认的是单点触摸 self.multipleTouchEnabled = YES; } return self; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ if (1 == [touches count]) { return; // 如果触摸视图只有一点,则不捏合 } //1.获取手指对象 NSArray *allTouch = [touches allObjects]; //不用下标的好处,不会造成程序的崩溃(数组下标越界) UITouch *firstTouch = [allTouch firstObject]; UITouch *secondTouch = [allTouch lastObject]; //2获取两个手指对象当前位置 CGPoint currentFirstPoint = [firstTouch locationInView:self]; CGPoint currentSecondPoint = [secondTouch locationInView:self]; //3.获取之前的手指的位置 CGPoint previousFirstPoint = [firstTouch previousLocationInView:self]; CGPoint previousSecondPoint = [secondTouch previousLocationInView:self]; //4.计算当前两个手指的距离 CGFloat currentDistance = [self distanceFromPoint:currentFirstPoint toPoint:currentSecondPoint]; //5.计算捏合前两个手指的距离 CGFloat previousDistance = [self distanceFromPoint:previousFirstPoint toPoint:previousSecondPoint]; //6.求出牛和前后两个捏合的比例 CGFloat rate = currentDistance / previousDistance; //7.缩放如果视图大小变化,并且想保持中心点不变,修改bounds即可 self.bounds = CGRectMake(0, 0, self.bounds.size.width * rate, self.bounds.size.height * rate); } //计算两个点的距离 - (CGFloat)distanceFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{ CGFloat dx = fromPoint.x - toPoint.x; CGFloat dy = fromPoint.y - toPoint.y; return sqrt(dx * dx + dy * dy); } @end