.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController{ UIImageView *p_w_picpathView;}@end .m文件#import "RootViewController.h"
@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad];//给View添加背景颜色
self.view.backgroundColor =[UIColorwhiteColor]; //创建UIImageView p_w_picpathView =[[UIImageViewalloc]initWithFrame:CGRectMake(80, 100, 100, 100)]; p_w_picpathView.backgroundColor =[UIColorredColor]; [self.view addSubview:p_w_picpathView];//开启交互 p_w_picpathView.userInteractionEnabled = YES; // 实现点击方法 UITapGestureRecognizer *tap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapHand:)]; // 点击次数 tap.numberOfTapsRequired = 1; [p_w_picpathView addGestureRecognizer:tap]; [tap release]; // 实现长按方法 UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)]; // 长按需要多长时间才有效 longPress.minimumPressDuration = 2; // 允许偏移距离 longPress.allowableMovement = 100; [p_w_picpathViewaddGestureRecognizer:longPress]; [longPress release]; // 轻扫手势 UISwipeGestureRecognizer *swip =[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipPress:)]; // 轻扫的方向,(向左) swip.direction=UISwipeGestureRecognizerDirectionLeft; [p_w_picpathView addGestureRecognizer:swip]; [swip release]; // 旋转手势 UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationP:)]; [p_w_picpathViewaddGestureRecognizer:rotation]; [rotation release]; // 拖拽方法 UIPanGestureRecognizer *pan =[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePan:)]; [p_w_picpathView addGestureRecognizer:pan]; [pan release];}//点击方法-(void)tapHand:(UITapGestureRecognizer*)tap{ }//长按方法-(void)longPress:(UILongPressGestureRecognizer *)longP{ if (longP.state == UIGestureRecognizerStateBegan) { NSLog(@"开始"); }else if (longP.state == UIGestureRecognizerStateChanged) { NSLog(@"移动"); }else if (longP.state == UIGestureRecognizerStateEnded) { NSLog(@"结束"); }}//轻扫方法-(void)swipPress:(UISwipeGestureRecognizer*)swip{ if (swip.direction==UISwipeGestureRecognizerDirectionLeft) { NSLog(@"图片移动"); }}//旋转手势方法-(void)rotationP:(UIRotationGestureRecognizer *)rotationP{ rotationP.view.transform=CGAffineTransformRotate(rotationP.view.transform, rotationP.rotation); rotationP.rotation = 0;}//拖拽手势方法-(void)handlePan:(UIPanGestureRecognizer*)recognizer{ CGPoint translation = [recognizer translationInView:self.view]; recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,recognizer.view.center.y+translation.y); [recognizer setTranslation:CGPointZeroinView:self.view]; if(recognizer.state==UIGestureRecognizerStateEnded){ CGPoint velocity = [recognizer velocityInView:self.view]; CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y)); CGFloat slideMult = magnitude / 200; NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); float slideFactor = 0.1*slideMult;// Increase for more of a slide CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),recognizer.view.center.y +(velocity.y * slideFactor)); finalPoint.x = MIN(MAX(finalPoint.x, 0),self.view.bounds.size.width); finalPoint.y = MIN(MAX(finalPoint.y,0),self.view.bounds.size.height); [UIViewanimateWithDuration:slideFactor*2 delay:0options:UIViewAnimationOptionCurveEaseOutanimations:^{recognizer.view.center=finalPoint;}completion:nil]; }}