手势Gesture

主要的几个手势操作,便于以后查找,所以简单总结下

@interface MainViewController ()

@property(nonatomic,retain)UIImageView *imageView;
@property(nonatomic,retain)UIAlertView *alert;

@end

@implementation MainViewController

- (void)dealloc
{
    [_imageView release];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    // UIImageView
    UIImage *image = [UIImage imageNamed:@"backFind.png"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = CGRectMake(0, 100, 300, 300);
    [self.view addSubview:self.imageView];

    // 把图片的交互打开.它默认是关闭的,此外还有一个控件是label
    self.imageView.userInteractionEnabled = YES;


    // 手势的使用
#pragma mark 点击
    // 1.点击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 设置点击几次才会触发方法
    tap.numberOfTapsRequired = 2;
    // 设置几根手指进行点击
    tap.numberOfTouchesRequired = 2;

    // 将手势添加到对应的图片上
    [self.imageView addGestureRecognizer:tap];
    [tap release];


#pragma mark 长按

    // 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 设置长按触发最小时间
    longPress.minimumPressDuration = 1;
    // 允许手指移动
    longPress.allowableMovement = 200;
    // 把手势添加到图片上
    [self.imageView addGestureRecognizer:longPress];
    [longPress release];

#pragma mark 旋转

    // 旋转
    // 创建一个旋转的手势
    UIRotationGestureRecognizer *rotationGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    // 手势放到图片上
    [self.imageView addGestureRecognizer:rotationGes];
    // 释放
    [rotationGes release];

#pragma mark 捏合

    // 捏合
    // 创建
    UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    // 添加到图片
    [self.imageView addGestureRecognizer:pinchGes];
    // 释放
    [pinchGes release];

#pragma mark 拖拽

//    // 拖拽
//    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//    // 添加
//    [self.imageView addGestureRecognizer:panGes];
//    // 释放
//    [panGes release];

#pragma mark 轻扫

    // 轻扫
    UISwipeGestureRecognizer *swipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // 添加
    [self.imageView addGestureRecognizer:swipeGes];
    // 释放
    [swipeGes release];

    // 轻扫的方向
    swipeGes.direction = UISwipeGestureRecognizerDirectionRight;

#pragma mark 屏幕边际手势,iOS7.0之后出现的手势

//    UIScreenEdgePanGestureRecognizer *

//    UIStepper





}
#pragma mark tap对应方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
        self.imageView.image = [UIImage imageNamed:@"backFind"];
}
#pragma mark 长按对应方法
- (void)longPressAction:(UILongPressGestureRecognizer *)press
{
    if (!self.alert) {
        self.alert = [[UIAlertView alloc] initWithTitle:@"1" message:@"2" delegate:self cancelButtonTitle:@"3" otherButtonTitles: nil];
        [self.alert show];
    }else{
        [self.alert show];
    }
}
#pragma mark 旋转对应方法
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
    // 可以通过手势获取手势添加的视图是哪一个
    UIImageView *imageView = (UIImageView *)rotation.view;
    // 进行旋转的操作
    // 通过视图的transform属性,让视图进行旋转
    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
    rotation.rotation = 0;
}
#pragma mark 捏合对应方法
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    // 通过手势找到视图
    UIImageView *imageView =(UIImageView *)pinch.view;
    // 通过transform改变尺寸
    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
    // 为了防止手势的变化让图片直接消失
    pinch.scale = 1;
}
#pragma mark 拖拽对应方法
- (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];
}
#pragma mark 轻扫对应方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"向右");
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值