关于iOS的手势

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];

    // 1.创建一个UIImage
    // 通过文件名直接进行创建
//    UIImage *image = [UIImage imageNamed:@"Curry.png"];
//    
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//    imageView.frame = CGRectMake(100, 100, 100, 200);
//    [self.view addSubview:imageView];
//    
//    [imageView release];

    UIImage *image = [UIImage imageNamed:@"zuozuomuxi.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 160, 360, 360)];
    imageView.image = image;
    [self.view addSubview:imageView];
    [imageView release];
    // UIImageView相当于相框,用来显示,UIImage显示的内容

    // 把图片的交互打开
    imageView.userInteractionEnabled = YES;

    // 手势 Gesture
    // 1.轻拍Tap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

    // 设置需要点击几次才会触发方法
    tap.numberOfTapsRequired = 2;
    tap.numberOfTouchesRequired = 2;

    // 把手势加入到图片上
    [imageView addGestureRecognizer:tap];
    // 内存管理
    [tap release];

    // 2.长按 LongPress
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    // 设置一下长按需要的最短时间
    longPress.minimumPressDuration = 3;
    // 判断在长按过程中允许手指移动的距离
    longPress.allowableMovement = 100;
    [imageView addGestureRecognizer:longPress];
    [longPress release];

    // 3.旋转 rotation
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    [rotation release];

    // 4.捏合 pinch
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    [pinch release];

    // 5.拖拽 pan
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    [pan release];

    // 6.轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [imageView addGestureRecognizer:swipe];
    [swipe release];
    // 轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;

}

- (void)tapAction:(id)sender {
    NSLog(@"taped an image");
}

- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    // 长按的方法在手势的各个状态中都会进行触发,所以需要进行判断
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按了");
    }

    NSLog(@"long pressed");
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture {
    // 获得添加手势的视图
    UIImageView *imageView = (UIImageView *)[rotationGesture view];
    // 调整视图的transform属性(顺时针正数,逆时针负数)
    imageView.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinchGesture {
    UIImageView *imageView = (UIImageView *)[pinchGesture view];

    imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);

//    imageView.transform = CGAffineTransformScale(imageView.transform, pinchGesture.scale, pinchGesture.scale);
//    pinchGesture.scale = 1;
}

- (void)panAction:(UIPanGestureRecognizer *)panGesture {
    // 获取拖拽手势添加的视图
    UIImageView *imageView = (UIImageView *)[panGesture view];
    // 获取手势经过的点
    CGPoint p = [panGesture translationInView:imageView];
    // 然后对视图的transform属性进行改变(横移x变y不变 竖移x不变y变)
    imageView.transform = CGAffineTransformMakeTranslation(p.x, p.y);
//    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
//    [panGesture setTranslation:CGPointZero inView:imageView];
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左");
    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"向右");
    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"向上");
    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下");
    }
}

以上是关于手势的一些使用,先记一下以防以后忘记。
另外需要注意: 一个手势只能添加到一个view上 一个view可以添加多个手势 估计和一个view只能有一个superview一样 一个guesture也只能对应一个view吧. 猜得.

你可能感兴趣的:(关于iOS的手势)