做测试之用
- (void)viewDidLoad { [super viewDidLoad]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 480) ]; view.backgroundColor = [UIColor grayColor]; [self.view addSubview:view]; [view release]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a.jpg"]]; imageView.frame = CGRectMake(0, 0, 320, 480); imageView.userInteractionEnabled = YES; [self.view addSubview:imageView]; [imageView release]; // 手势 // UITapGestureRecognizer 轻拍手势识别器 能识别轻拍操作 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; [imageView addGestureRecognizer:tap]; // 给视图添加手势 [tap release]; // 两根手指点两下 tap.numberOfTapsRequired = 2; //必须点击的次数 默认为1 tap.numberOfTouchesRequired = 2; //手指的数量 // UILongPressGestureRecognizer 长按手势 能识别长按操作 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [imageView addGestureRecognizer:longPress]; [longPress release]; [longPress setMinimumPressDuration:2]; //设置长按时间2S 默认0.5S // UIRotationGestureRecognizer 旋转手势 能识别旋转 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; [imageView addGestureRecognizer:rotation]; [rotation release]; // UIPinchGestureRecognizer 捏合手势 识别捏合 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [imageView addGestureRecognizer:pinch]; [pinch release]; // UIPanGestureRecognizer 拖放手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [imageView addGestureRecognizer:pan]; [pan release]; // UISwipeGestureRecognizer 轻扫手势 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]; [imageView addGestureRecognizer:swipe]; swipe.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫手势需要指定方向 默认right 指定多个方向会降低识别率 [swipe release]; // UIScreenEdgePanGestureRecognizer 屏幕边缘轻扫识别器 iOS7中新增的手势 (这个尚在摸索中) UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screen:)]; // 边缘滑动,必须设置方向edges属性 [screenEdgePanGesture setEdges:UIRectEdgeLeft]; [imageView addGestureRecognizer:screenEdgePanGesture]; // Do any additional setup after loading the view. }
实现手势的方法
// 点按 - (void)tap:(UITapGestureRecognizer *)tap { // 在开发过程中 如果没有必要,不要对一个控件即使用触摸 有使用手势 NSLog(@"点我啦%@", tap); } - (void)longPress:(UILongPressGestureRecognizer *)longPress { NSLog(@"long:%@", longPress); } // 旋转 - (void)rotation:(UIRotationGestureRecognizer *)rotation { rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation); // NSLog(@"%f", rotation.rotation); NSLog(@"%f", rotation.velocity); // 旋转的速度 NSLog(@"rotation:%@", rotation); rotation.rotation = 0; // 旋转角度 } // 捏合 - (void)pinch:(UIPinchGestureRecognizer *)pinch { NSLog(@"scale:%f", pinch.scale); NSLog(@"velocity:%f" ,pinch.velocity); // 速度 NSLog(@"pinch:%@", pinch); pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1; // 比例复位 } // 拖放手势 - (void)pan:(UIPanGestureRecognizer *)pan { // 在拖放手势中是需要考虑手指的状态的UIGestureRecognizerState // 在拖放手势中使用的状态是UIGestureRecognizerStateChanged // 使用拖放手势的时候 当手指离开的时候,应该做一个很小的动作,提醒用户拖放完成 // if (pan.state == UIGestureRecognizerStateChanged) { // [pan.view setCenter:[pan locationInView:self.view]]; // } NSLog(@"pan:%@", pan); CGPoint point = [pan translationInView:pan.view]; pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y); [pan setTranslation:CGPointZero inView:pan.view]; } // 轻扫手势 - (void)swipe:(UISwipeGestureRecognizer *)swipe { NSLog(@"swipe:%@", swipe); } // 屏幕边缘轻扫手势 (一定要从屏幕的边缘开始) - (void)screen:(UIScreenEdgePanGestureRecognizer *)screen { NSLog(@"screen"); }