码迷,mamicode.com
首页 > 其他好文 > 详细

UI 06 7种手势

时间:2015-08-12 23:40:00      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

// UIImageView
    UIImage *image = [UIImage imageNamed:@"u=3179572108,1349777253&fm=21&gp=0.jpg"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = CGRectMake(45, 100, 300, 300);
    [self.view addSubview:self.imageView];
    [_imageView release];

1. 点击

    // 用户交互默认是关闭的就只有两个控件,一个是不能够点击的UILabel, 一个是UIImageView.我们想要点击UIImageView需要把用户交互打开.
    self.imageView.userInteractionEnabled = YES;


 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 设置点击几次才会触发的方法:
    tap.numberOfTapsRequired = 2;
    //设置几根手指:
    tap.numberOfTouchesRequired = 2;
    //将手势添加到对应的图片上
    [self.imageView addGestureRecognizer:tap];
    [tap release];
#pragma mark 点击的手势方法.
- (void)tapAction:(UITapGestureRecognizer *)tap{
    // 点击改变图片
    self.imageView.image = [UIImage imageNamed:@"1.jpg"];
}

2.长按

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 设置长按触发的最小时间
    longPress.minimumPressDuration = 1;
    // 允许用户手指在长安过程中允许移动的距离,超过范围就停止.
    longPress.allowableMovement = 200;
    // 把手势添加到图片
    [self.imageView addGestureRecognizer:longPress];
    [longPress release];
#pragma  mark 长按的手势方法.
- (void)longPressAction:(UILongPressGestureRecognizer *)longpress{
    // 长按状态
   // longpress.state;
    //长按弹出来一个UIAlertView
    if (self.alertView == nil) {
    self.alertView = [[UIAlertView alloc] initWithTitle:@"要删除么?" message:@"不要删除我啊!" delegate:self cancelButtonTitle:@"卖萌可耻,无情地删除!" otherButtonTitles:@"你这么可爱,不删了~", nil];
    [self.alertView show];
    [_alertView release];
    }else{
       [self.alertView show];
    }
}

3.旋转

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.imageView addGestureRecognizer:rotation];
    [rotation release];
#pragma mark 通过旋转手势,让图片发生旋转.
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
    // 可以通过手势获取手势添加的视图是哪一个
    UIImageView *imageview =(UIImageView *) rotation.view;
    NSLog(@"%@",imageview);
    // 进行旋转操作
    //通过视图的一个transform属性,让视图进行旋转.
//    imageview.transform = CGAffineTransformMakeRotation(rotation.rotation);
    imageview.transform = CGAffineTransformRotate(imageview.transform, rotation.rotation);
    rotation.rotation = 0;
}

4.捏合

 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.imageView addGestureRecognizer:pinch];
    [pinch release];
#pragma mark 通过捏合手势,等比例缩放图片.
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    // 通过手势找视图
    UIImageView *imageview = (UIImageView *)pinch.view;
    // 通过Transform 改变图片尺寸
    imageview.transform = CGAffineTransformScale(imageview.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;// 给它终止的一个尺寸,不让照片直接消失.=
}

5. 拖拽

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.imageView addGestureRecognizer:pan];
// 手势需要指定方向.
    //向右划
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
#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];
}

6.轻扫

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.imageView addGestureRecognizer:swipe];
    [swipe release];

#pragma  mark 轻扫手势
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight ) {
        NSLog(@"xiangyou");//需要把拖拽移除.
    }
}

7.屏幕边际手势,iOS7.0之后出现的手势.

UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeAction:)];
    [self.imageView addGestureRecognizer:screenEdge
     ];
    [screenEdge release];

对应的方法还没有添加呢!

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI 06 7种手势

标签:

原文地址:http://blog.csdn.net/gao_zi/article/details/47453009

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!