手势识别器

来源:互联网 发布:淘宝客服是干嘛的? 编辑:程序博客网 时间:2024/06/12 19:54

import “MainViewController.h”

@interface MainViewController ()
@property(nonatomic,retain)UIImageView *imageView;
@property(nonatomic,assign)BOOL isSelect;
@property(nonatomic,retain)UIAlertView *alterView;

@end

@implementation MainViewController

  • (void)dealloc
    {
    [_alterView release];
    [_imageView release];
    [super dealloc];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // UIImageView
    UIImage *image=[UIImage imageNamed:@”516e5ceccf732.jpg”];
    self.imageView=[[UIImageView alloc] initWithImage:image];
    self.imageView.frame=CGRectMake(10,100,355,300);
    [self.view addSubview:self.imageView];
    [self.imageView release];

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

    // 手势的使用
    // 1.点击
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 设置点击几次会触发方法
    tap.numberOfTapsRequired=2;
    // 设置几根手指进行点击
    tap.numberOfTouchesRequired=2;
    // 将手势添加到对应的图片上
    [self.imageView addGestureRecognizer:tap];
    [tap release];

    self.isSelect=YES;

    // 2.长按
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 设置长按触发的最小时间
    longPress.minimumPressDuration=2;

    // 允许用户手指在长按过程中允许移动的距离
    longPress.allowableMovement=200;
    // 把手势添加到图片中
    [self.imageView addGestureRecognizer:longPress];
    [longPress release];

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

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

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

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

    // 7.屏幕边际手势,iOS7.0以后出现的手势
    UIScreenEdgePanGestureRecognizer *screenEdgePan=[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
    }

pragma mark 点击的方法

-(void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@”测试一下点击手势”);
if (self.isSelect) {
self.imageView.image=[UIImage imageNamed:@”lol168.jpg”];
}else{
self.imageView.image=[UIImage imageNamed:@”516e5ceccf732.jpg”];
}
self.isSelect=!self.isSelect;

}

pragma mark 长按的方法

-(void)longPressAction:(UILongPressGestureRecognizer *)longPass{
// 长按的状态
// longPass.state
// 长按之后弹出来UIAlertView
if (!self.alterView) {
self.alterView=[[UIAlertView alloc] initWithTitle:@”图片咋样” message:@”您说” delegate:self cancelButtonTitle:@”哎呦,不错呀” otherButtonTitles:@”卧槽,很好呀”, nil];
[self.alterView show];
[self.alterView release];
}
[self.alterView 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改变图片的尺寸
// pinch.scale是相对于x,y轴的相对比例
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(@”向右”);
}
}

-(void)click:(UIStepper *)stepper{
NSLog(@”%g”,stepper.value);
}

0 0
原创粉丝点击