iOS中七种手势

来源:互联网 发布:python time sleep作用 编辑:程序博客网 时间:2024/06/05 04:08

iOS中提供了7种手势,其原理都是对touchesBegin, touchesEnded, touchesMoved, touchesCanceled 四种方法的封装,继承于UIGestureRecognizer类,
这七种方法分别是:

1. 轻拍手势 UITapGestureRecognizer
2. 长按手势 UILongPressGestureRecognizer
3. 轻扫手势 UISwipeGestureRecognizer
4. 拖拽手势 UIPanGestureRecognizer
5. 旋转手势 UIRotationGestureRecognizer</span>
6. 捏合手势 UIPinchGestureRecognizer</span>
7. 屏幕边缘拖拽 UIScreenEdgePanGestureRecognizer</span>
其中拖拽和屏幕边缘拖拽会有冲突, 轻扫手势和拖拽手势可能造成冲突
以下是各手势的属性应用及其方法,以及实现的一些图片处理功能:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.//    UIImageView 的使用    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 280, 280)];    imageView.backgroundColor = [UIColor redColor];    [self.view addSubview:imageView];    [imageView release];//    利用图片产生一个UIImage对象    UIImage *image = [UIImage imageWithContentsOfFile:@"/Users/dlios/Documents/girl.jpg"];//    将图片加载到相框    imageView.image = image;//    手势识别器//    1. 轻拍手势    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];//    轻拍的设置//    需要轻拍2次才能响应事件    tap.numberOfTapsRequired = 2;//    需要2个手指头才能响应事件    tap.numberOfTouchesRequired = 2;    [imageView addGestureRecognizer:tap];    [tap release];//    将UIImageView的用户交互打开,使它能响应轻拍    imageView.userInteractionEnabled = YES;//     2.长按手势    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAciton:)];    [imageView addGestureRecognizer:longPress];//    长按5秒触发方法    longPress.minimumPressDuration = 5;//    长按时,允许用户移动手指距离,默认10,单位像素    longPress.allowableMovement = 1000;    //    3.轻扫手势    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];    [imageView addGestureRecognizer:swip];//    只支持一条直线上的轻扫  如上下 或者左右     swip.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;//    4. 拖拽手势//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];//    [imageView addGestureRecognizer:pan];//    5. 旋转手势    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];    [imageView addGestureRecognizer:rotation];//    6.捏合手势    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];    [imageView addGestureRecognizer:pinch];//    7.屏幕边缘拖拽    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];    [imageView addGestureRecognizer:screenEdgePan];//    设置检测哪一边的屏幕边缘    screenEdgePan.edges = UIRectEdgeLeft;}//边缘拖拽触发方法- (void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan{    NSLog(@"边缘拖拽");}//捏合的触发方法- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{    NSLog(@"捏合");    UIImageView *imageView = (UIImageView *)pinch.view;//    在x,y轴方向 放大缩小    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);    pinch.scale = 1;}//旋转手势方法- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{    NSLog(@"旋转");    UIImageView *imageView = (UIImageView *)rotation.view;//    让view旋转    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);//    让旋转弧度始终为0    rotation.rotation = 0;}- (void)panAction:(UIPanGestureRecognizer *)pan{    NSLog(@"拖拽");//    通过手势的view属性,获取到当前手势添加到的view    UIImageView *imageViewe = (UIImageView  *)pan.view;//    获取到当前手指接触的点    CGPoint p = [pan translationInView:imageViewe];    //    让view 变形    imageViewe.transform = CGAffineTransformTranslate(imageViewe.transform, p.x, p.y);//    设置手势的属性    [pan setTranslation:CGPointZero inView:imageViewe];    }- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{    NSLog(@"轻扫");}- (void)longPressAciton:(UILongPressGestureRecognizer *)longPress{//    当长按手势开始触发时    if (longPress.state == UIGestureRecognizerStateBegan) {        NSLog(@"longpress");    }    }//轻拍的触发方法- (void)tapAction:(UITapGestureRecognizer *)tap{    NSLog(@"paipaipai");    NSLog(@"%d", tap.numberOfTouches);    }




0 0
原创粉丝点击