ps:被系统封装了的手势方法-(void)loadView { [super loadView]; self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease]; self.view.backgroundColor = [UIColor lightGrayColor]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 184, 200, 200)]; imageView.image = [UIImage imageNamed:@"1.png"]; //打开图片的交互(不打开不能手势操作) ps:很重要的细节 imageView.userInteractionEnabled = YES; imageView.multipleTouchEnabled = YES; imageView.tag = 100; [self.view addSubview:imageView]; [imageView release]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)]; // 点击,双击 singleTap.numberOfTapsRequired = 1; [imageView addGestureRecognizer:singleTap]; UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)]; doubleTap.numberOfTapsRequired = 2; [imageView addGestureRecognizer:doubleTap]; [singleTap requireGestureRecognizerToFail:doubleTap]; UITapGestureRecognizer *threeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)]; threeTap.numberOfTapsRequired = 3; [imageView addGestureRecognizer:threeTap]; [doubleTap requireGestureRecognizerToFail:threeTap]; [singleTap release]; [doubleTap release]; [threeTap release]; // 捏合 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; [imageView addGestureRecognizer:pinch]; [pinch release]; // 旋转 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationAction:)]; [imageView addGestureRecognizer:rotation]; [rotation release]; // 横扫 UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAction:)]; swipeR.numberOfTouchesRequired = 1; swipeR.direction = UISwipeGestureRecognizerDirectionRight; [imageView addGestureRecognizer:swipeR]; [swipeR release]; UISwipeGestureRecognizer *swipeL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAction:)]; swipeL.numberOfTouchesRequired = 1; swipeL.direction = UISwipeGestureRecognizerDirectionLeft; [imageView addGestureRecognizer:swipeL]; [swipeL release]; // 长按 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAction:)]; [imageView addGestureRecognizer:longPress]; [longPress release]; // 拖动 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)]; [imageView addGestureRecognizer:pan]; [pan release]; }
-(void)handleTapAction:(UITapGestureRecognizer *)sender { if (sender.numberOfTapsRequired == 1) { NSLog(@"这是单击"); // insert code here... } if (sender.numberOfTapsRequired == 2) { NSLog(@"这是双击"); // insert code here... } if (sender.numberOfTapsRequired == 3) { NSLog(@"这是三击"); // insert code here... } if (sender.numberOfTapsRequired == 4) { NSLog(@"这是四击"); // insert code here... } } -(void)handlePinch:(UIPinchGestureRecognizer *)sender { UIImageView *imageV = (UIImageView *)[self.view viewWithTag:100]; CGRect rect = imageV.frame; rect.size.width = sender.scale*imageV.image.size.width; rect.size.height = sender.scale*imageV.image.size.height; imageV.frame = rect; imageV.center = self.view.center; // sender.scale } -(void)handleRotationAction:(UIRotationGestureRecognizer *)sender { UIImageView *imageV = (UIImageView *)[self.view viewWithTag:100]; imageV.transform = CGAffineTransformRotate(imageV.transform, sender.rotation); sender.rotation = 0; // if (sender.state == UIGestureRecognizerStateEnded) // { // sender.rotation = 0; // return; // } // imageV.transform 参考的那个点(当前点) sender.rotation 旋转一定的角度 // 第一个参数是当前倾斜度,第二个参数是需要转动得倾斜度 //CGAffineTransform transform = CGAffineTransformRotate(imageV.transform, sender.rotation); //imageV.transform = transform; } -(void)handleSwipeAction:(UISwipeGestureRecognizer *)sender { NSLog(@"改变颜色了"); if (sender.direction == UISwipeGestureRecognizerDirectionRight ) { self.view.backgroundColor = [UIColor blackColor]; } if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { self.view.backgroundColor = [UIColor grayColor]; } } -(void)handleLongPressAction:(UILongPressGestureRecognizer *)sender { for (id type in [self.view subviews]) { if ([type isKindOfClass:[UIActionSheet class]]) { return; } } UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"下载到本地", nil]; [actionsheet showInView:self.view]; [actionsheet release]; } -(void)handlePanAction:(UIPanGestureRecognizer *)sender { UIImageView *imageV = (UIImageView *)[self.view viewWithTag:100]; /* CGPoint startPoint; if (sender.state == UIGestureRecognizerStateBegan) { startPoint = [sender locationInView:imageV]; } CGPoint currentPoint; if (sender.state == UIGestureRecognizerStateChanged) { currentPoint = [sender locationInView:imageV]; } float diaX = currentPoint.x - startPoint.x; float diaY = currentPoint.y - startPoint.y; CGPoint p =imageV.center; p.x + = diaX; p.y + = diaY; imageV.center = p; */ // 老师的方法 /* UIImageView * myView = (UIImageView *)[self.view viewWithTag:1000]; // 获得相对移动量 CGPoint p = [sender translationInView:myView]; // 改变中心点 myView.center = CGPointMake(myView.center.x + p.x, myView.center.y + p.y); // 重置坐标零点 [sender setTranslation:CGPointZero inView:myView]; */ //CGPoint stratPoint = imageV.frame.origin; CGPoint point = [sender translationInView:imageV]; CGPoint p = imageV.center; p.x += point.x; p.y += point.y ; imageV.center = p; [sender setTranslation:CGPointZero inView:imageV]; //imageV.frame = CGRectMake(p.x, p.y, imageV.frame.size.width, imageV.frame.size.height); }
视图的单击,双击,捏合,扫动,旋转,长按等。
最新推荐文章于 2020-10-27 13:40:32 发布