码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 初学-手势

时间:2015-09-19 19:34:32      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

一.学习手势,首先要先建立一个对象进行操作,我们以一个照片为例.首先创建一个imageView

1.声明一个属性:

@property (nonatomic, retain)UIImageView *imageView;

2.开始真正创建对象

self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"beautiful.jpg"]];

    self.imageView.frame = CGRectMake(100, 200, 200, 300);

    [self.view addSubview:self.imageView];

    [_imageView release];

 3.如果要创建视图,就要打开图片用户交互功能,默认情况下是NO

    self.imageView.userInteractionEnabled = YES;

 二,手势的几个常用方法

1.点击事件

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];//初始化方法,添加点击方法名为tapAction:

    [self.imageView addGestureRecognizer:tap];//把手势加到指定的视图上

    [tap release];

tap.numberOfTouchesRequired = 2;//设置几个手指点击才可响应

tap.numberOfTapsRequired = 2;//设置连续点击几下才可响应

//点击方法

- (void)tapAction:(UITapGestureRecognizer *)tap

{

    NSLog(@"我被点击了");

}

 2.长按事件

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpressAction:)];//同样的添加事件,事件名为longpressAction:

       [self.imageView addGestureRecognizer:longPress];//把手势添加到imageview上

    [longPress release];

    longPress.minimumPressDuration = 1;//设置长按触发时间

 

- (void)longpressAction:(UITapGestureRecognizer *)longpressAction

{

    if (longpressAction.state == UIGestureRecognizerStateBegan) {

        NSLog(@"长按开始");

    }

}

 3.旋转

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];

    [self.imageView addGestureRecognizer:rotation];

    [rotation release];

以上创建方法同前两个方法.

- (void)rotationAction:(UIRotationGestureRecognizer *)rotationAction{

    //可以通过手势,知道手势添加到那个视图

    UIImageView *imageView = (UIImageView *) rotationAction.view;

    //找到视图,并且让视图进行旋转

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotationAction.rotation);

    rotationAction.rotation = 0;

}

4.缩放功能

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];

    [self.imageView addGestureRecognizer:pinch];

    [pinch release];

- (void)pinch:(UIPinchGestureRecognizer *)pinch

{

    //通过手势,找到视图

    UIImageView *imageViews = (UIImageView *) pinch.view;

    //找到视图,让视图缩放或者放大

    imageViews.transform = CGAffineTransformScale(imageViews.transform, pinch.scale, pinch.scale);

    pinch.scale = 1;

}

5.拖拽手势

UIPanGestureRecognizer *tuozhuai = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tuozhuai:)];

    [self.imageView addGestureRecognizer:tuozhuai];

    [tuozhuai release];

- (void)tuozhuai:(UIPanGestureRecognizer *)tuozhuai

{

    //通过手势,找到视图

    UIImageView *imageView = (UIImageView *)tuozhuai.view;

    //通过手势,来获取移动的点

    CGPoint p = [tuozhuai translationInView:imageView];

//    CGPoint p = [tuozhuai  trans:imageView];

    //通过位置transform实现拖拽

    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);

    [tuozhuai setTranslation:CGPointZero inView:imageView];  

}

//6.清扫手势,此手势是看你的滑动方向

    UISwipeGestureRecognizer *qingsao = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(qingsao:)];

    [self.view addGestureRecognizer:qingsao];

    [qingsao release];

    qingsao.direction = UISwipeGestureRecognizerDirectionLeft;//清扫的方向

- (void)qingsao:(UISwipeGestureRecognizer *)qingsao

{

    if (qingsao.direction == UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"向左进行清扫");

    }

}

iOS 初学-手势

标签:

原文地址:http://www.cnblogs.com/guominghao/p/4821996.html

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