苹果为我们封装了7种手势识别
UIGestureRecognizer
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UIScreenEdgePanGestureRecognizer
- UILongPressGestureRecognizer
UITapGestureRecognizer 轻拍动作
设置方法:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
tap.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tap];
[tap release];
效果为点击imageView两次执行tapAction时间.
属性
- numberOfTapsRequired 是点击的次数,可以用这个属性来分别单击和双击事件,(另一种用touch事件中得延迟事件,但是延迟比较严重,效果不好)
- numberOfTouchesRequired 是点击的手指的个数,可以用来检测多个手指点击
UIPinchGestureRecognizer 捏合 ,用来放大和缩小
代码
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch]
[pinch release];
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
//改变图片的比例
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
pinch.scale = 1.0;
}
属性
1.scale 两个手指捏合的比例
UIRotationGestureRecognizer 旋转
代码
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
[rotation release];
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
属性
UISwipeGestureRecognizer 清扫 向4个方向运动
代码
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//设置手势触发的方向,支持水平或垂直方向的清扫
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipeLeft];
[swipeLeft release];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//设置手势触发的方向,支持水平或垂直方向的清扫
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeRight];
[swipeRight release];
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
if (swipe.direction == 1) {
imageIndex++;
if (imageIndex == 8) {
imageIndex = 1;
}
NSString *name = [NSString stringWithFormat:@"%d", imageIndex];
imageView.image = [UIImage imageNamed:name];
NSLog(@"right -- %@",name);
}
if (swipe.direction == 2) {
imageIndex--;
if (imageIndex == 0) {
imageIndex = 7;
}
NSString *name = [NSString stringWithFormat:@"%d",imageIndex];
imageView.image = [UIImage imageNamed:name];
NSLog(@"left -- %@", name);
}
NSLog(@"%lu",(unsigned long)swipe.direction);
}
属性
UIPanGestureRecognizer 拖拽
代码
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
[pan release];
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan translationInView:imageView];
//修改图片的坐标
imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:imageView];
}
属性
UIScreenEdgePanGestureRecognizer 边缘运动
代码
UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
screen.edges = UIRectEdgeLeft;
[imageView addGestureRecognizer:screen];
[screen release];
属性
UILongPressGestureRecognizer 长按 (进行截屏)
代码
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpressAction:)];
[imageView addGestureRecognizer:longpress];
[longpress release];
- (void)longpressAction:(UILongPressGestureRecognizer *)longpress{
if (longpress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
//屏幕截屏
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//viewImage就是获取的截图,如果要将图片存入相册.只需在后面调用
UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}