本文共 4397 字,大约阅读时间需要 14 分钟。
/** 基类UIGestureRecognizer UITapGestureRecognizer Tap 点击 UIPanGestureRecognizer Pan (慢速滑动,拖移) UILongPressGestureRecognizer LongPress (长按) UIPinchGestureRecognizer Pinch (捏合,两手指往内或外拨动) UIRotationGestureRecognizer Rotation (旋转) UISwipeGestureRecognizer Swipe (快速滑动,轻扫) */ #import "ViewController.h" #define kRandomValue (arc4random() %256 /256.0) @interface ViewController () <UIGestureRecognizerDelegate> @property (nonatomic,strong) UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //强烈不建议往一个控件上加很多手势 [self addImageView]; [self addTapGesture];//点击手势 //[self addPanGesture];//拖移手势 [self addLongPressGesture];//长按手势 [self addPinchGesture];//捏合 [self addResetButton];//重置 [self addRotationGesture];//旋转 [self addSwipeGesture];//轻扫 } -(void)addImageView{ UIImage *image=[UIImage imageNamed:@"cloth"]; self.imageView=[[UIImageView alloc] initWithImage:image];//这样写默认imageView的大小和image的大小一样 self.imageView.userInteractionEnabled=YES;//打开用户交互 self.imageView.center=self.view.center; [self.view addSubview:self.imageView]; } -(void)addTapGesture{ UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] init]; //tap.numberOfTapsRequired=2;//默认为1,点击次数 //tap.numberOfTouchesRequired=2;//默认为1,多少根手指触摸 [tap addTarget:self action:@selector(tap:)]; [self.imageView addGestureRecognizer:tap]; } -(void)tap:(UITapGestureRecognizer *)tap{ UIView *view=tap.view; view.superview.backgroundColor=[UIColor colorWithRed:kRandomValue green:kRandomValue blue:kRandomValue alpha:1.0]; } -(void)addPanGesture{ UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] init]; pan.delegate=self; [pan addTarget:self action:@selector(pan:)]; [self.imageView addGestureRecognizer:pan]; } -(void)pan:(UIPanGestureRecognizer *)pan{ CGPoint offset=[pan translationInView:self.view]; CGPoint center=self.imageView.center; center.x+=offset.x; center.y+=offset.y; self.imageView.center=center; //不让其累加 [pan setTranslation:CGPointZero inView:self.view]; } -(void)addLongPressGesture{ UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc ] init]; [longPress addTarget:self action:@selector(longPress:)]; [self.imageView addGestureRecognizer:longPress]; } -(void)longPress:(UILongPressGestureRecognizer *)longPress{ switch (longPress.state) { case UIGestureRecognizerStateBegan: NSLog(@"你长按了"); break; case UIGestureRecognizerStateEnded: NSLog(@"长按结束"); break; default: break; } } -(void)addPinchGesture{ UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [self.imageView addGestureRecognizer:pinch]; } -(void)pinch:(UIPinchGestureRecognizer *)pinch{ NSLog(@"%lf",pinch.scale); //捏合 scale缩放比例 //transform 变换,仿射变换/矩阵变换, self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); //重置捏合手势比例,每次都应该相对于当前的 pinch.scale=1;//下次相对比例应该从1开始 } -(void)addResetButton{ UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(0, CGRectGetMaxY(self.imageView.frame)+60, 200, 40); [button setTitle:@"Reset" forState:UIControlStateNormal]; [button addTarget:self action:@selector(reset:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)reset:(id)sender{ //恢复imageView的transform self.imageView.transform=CGAffineTransformIdentity; } -(void)addRotationGesture{ UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; [self.imageView addGestureRecognizer:rotation]; } -(void)rotation:(UIRotationGestureRecognizer *)rotation{ NSLog(@"旋转弧度:%lf",rotation.rotation); self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation); rotation.rotation=0;//重置旋转弧度,相当于当前的从0开始 } -(void)addSwipeGesture{ UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; //swipe默认的direction为right swipe.direction=UISwipeGestureRecognizerDirectionUp; [self.imageView addGestureRecognizer:swipe]; } -(void)swipe:(UISwipeGestureRecognizer *)swipe{ CGPoint center=self.imageView.center; center.y -=50; self.imageView.center=center; } #pragma mark -UIGestureRecognizerDelegate //是否支持多个手势 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } @end转载地址:http://pjsni.baihongyu.com/