手势

来源:互联网 发布:windows iscsi target 编辑:程序博客网 时间:2024/04/29 16:41

// 1.点击

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];    // 设置触发方法所需点击次数    tap.numberOfTapsRequired = 2;    // 设置触发方法所需手指个数    tap.numberOfTouchesRequired = 2;    // 将手势添加到对应的图片上    [self.imageView addGestureRecognizer:tap];    [tap release];

// 点击方法

- (void)click:(UITapGestureRecognizer *)tap {    self.imageView.image = [UIImage imageNamed:@"12345.jpg"];}

// 2.长按

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];    // 设置长按触发所需最小时间    longPress.minimumPressDuration = 2;    // 允许用户手指在长按过程中允许移动的距离    longPress.allowableMovement = 200;    // 把手势添加到imageView中    [self.imageView addGestureRecognizer:longPress];    [longPress release];

// 长按方法

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {    // 长按弹出AlertView        self.alertview = [[UIAlertView alloc] initWithTitle:@"测试" message:@"测试" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];    [self.alertview show];    [self.alertview release];}

// 3.旋转

    // 创建旋转手势    UIRotationGestureRecognizer *revolve = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(revolveAction:)];    // 把手势放入对应的imageView中    [self.imageView addGestureRecognizer:revolve];    // 释放    [revolve release];

// 旋转方法

- (void)revolveAction:(UIRotationGestureRecognizer *)revolve {    // 可通过手势获取手势添加的视图是哪个    UIImageView *imageView = (UIImageView *)revolve.view;    // 进行旋转操作    // 通过视图transform属性,对视图进行旋转        imageView.transform = CGAffineTransformRotate(imageView.transform, revolve.rotation);    revolve.rotation = 0;}

// 4.捏合

    // 创建    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];    // 将手势添加到图片上    [self.view addGestureRecognizer:pinch];    // 释放    [pinch release];

// 捏合方法

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {    UIImageView *imageView = (UIImageView *)pinch.view;    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);    // 防止图片消失    pinch.scale = 1;}

// 5.拖拽

    // 创建    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];    // 添加    [self.view addGestureRecognizer:pan];    // 释放    [pan release];

// 拖拽方法

- (void)panAction:(UIPanGestureRecognizer *)pan {    UIImageView *imageView = (UIImageView *)pan.view;    // 通过手势获得经过的点    CGPoint p = [pan translationInView:imageView];    // 设置移动的位置    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);    // 为了防止手势在操作时视图消失    [pan setTranslation:CGPointZero inView:imageView];}

// 6. 轻扫

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

// 轻扫手势

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)        NSLog(@"测试");}
0 0
原创粉丝点击