UIGestureRecognizer手势识别

来源:互联网 发布:雪纺衫淘宝好评 编辑:程序博客网 时间:2024/05/17 21:07

UIGestureRecognizer手势识别

UIGestureRecognizer 继承于 NSObject 共有七种手势

即: 
1.UIPinchGestureRecognizer (捏合手势)

2.UITapGestureRecognizer (轻拍手势)

3.UIRotationGestureRecognizer (旋转手势)

4.UISwipeGestureRecognizer (轻扫手势)

5.UIPanGestureRecognizer (拖拽手势)

6.UIScreenEdgePanGestureRecognizer (屏幕边缘轻扫手势)

7.UILongPressGestureRecognizer (长按手势)

实现手势步骤

1创建手势对象

UIGestureRecognizer 的 七个子类并没有自身的初始化方法, 所以需要使用父类的 初始化方法,如下

- (instancetype)initWithTarget:(id)target action:(SEL)action

2将手势加在视图上, 注意视图的用户交互属性是否开启

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer/* 创建对象 */@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled/* 用户交互属性*/

3内存管理

- (void)release

4设置相关属性

5手势方法实现

实现创建对象 - (instancetype)initWithTarget:(id)target action:(SEL)action中的 action:(SEL)action  

例如

1.UIPinchGestureRecognizer (捏合手势)


- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建一个UIImageView对象 */    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];    imageView.frame = CGRectMake(0, 0, 300, 300);    imageView.center = self.view.center;    imageView.layer.cornerRadius = 50;    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;    imageView.layer.masksToBounds = YES;    [self.view addSubview:imageView];    [imageView release];/* 创建UIPinchGestureRecognizer对象*/        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [imageView addGestureRecognizer:pinch];    imageView.userInteractionEnabled = YES;/* 默认NO */    [pinch release];}/* 实现action:@selector(pinchAction:)*/ - (void)pinchAction:(UIPinchGestureRecognizer *)pinch{    NSLog(@"捏合");    /* 设置imageView的transform属性     *使用CGAffineTransformScale函数改变比例实现缩放效果     */    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);    pinch.scale = 1;}

2.UITapGestureRecognizer (轻拍手势)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建一个UIImageView对象 */    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];    imageView.frame = CGRectMake(0, 0, 300, 300);    imageView.center = self.view.center;    imageView.layer.cornerRadius = 50;    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;    imageView.layer.masksToBounds = YES;    [self.view addSubview:imageView];    [imageView release];/* 创建UITapGestureRecognizer手势对象 */    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];/* 设置相关属性 */    tap.numberOfTapsRequired = 1;    tap.numberOfTouchesRequired = 1;/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [imageView addGestureRecognizer:tap];    imageView.userInteractionEnabled = YES;/* 默认NO */    [tap release];}/* 实现action:@selector(pinchAction:)*/ - (void)tapAction:(UITapGestureRecognizer *)tap{    NSLog(@"轻拍");}

3.UIRotationGestureRecognizer (旋转手势)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建一个UIImageView对象 */    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];    imageView.frame = CGRectMake(0, 0, 300, 300);    imageView.center = self.view.center;    imageView.layer.cornerRadius = 50;    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;    imageView.layer.masksToBounds = YES;    [self.view addSubview:imageView];    [imageView release];/* 创建UIRotationGestureRecognizer手势对象 */    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [imageView addGestureRecognizer:rotation];    imageView.userInteractionEnabled = YES;/* 默认NO */    [rotation release];}/* 实现action:@selector(pinchAction:)*/- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{    NSLog(@"旋转");    NSLog(@"%@", rotation);    /* 设置imageView的transform属性     * 使用 CGAffineTransformRotate 函数     */    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);    rotation.rotation = 0;}

4.UISwipeGestureRecognizer (轻扫手势)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建UISwipeGestureRecognizer手势对象 */    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];/* 设置相关属性 */    swipe.direction = UISwipeGestureRecognizerDirectionUp;/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [self.view addGestureRecognizer:swipe];    [swipe release];}/* 实现action:@selector(pinchAction:)*/- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{    NSLog(@"轻扫");}

5.UIPanGestureRecognizer (拖拽手势)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建一个UIImageView对象 */    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];    imageView.frame = CGRectMake(0, 0, 300, 300);    imageView.center = self.view.center;    imageView.layer.cornerRadius = 50;    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;    imageView.layer.masksToBounds = YES;    [self.view addSubview:imageView];    [imageView release];/* 创建UIPanGestureRecognizer手势对象 */    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [imageView addGestureRecognizer:pan];    imageView.userInteractionEnabled = YES;/* 默认NO */    [pan release];}/* 实现action:@selector(pinchAction:)*/- (void)panAction:(UIPanGestureRecognizer *)pan{    NSLog(@"拖拽");    /* 设置imageView的transform属性     * 使用 CGAffineTransformTranslate 函数      */    CGPoint point = [pan translationInView:pan.view];    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);    [pan setTranslation:CGPointZero inView:pan.view];}

6.UIScreenEdgePanGestureRecognizer (屏幕边缘轻扫手势)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建UIScreenEdgePanGestureRecognizer手势对象 */    UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];/* 设置相关属性 */    screenEdge.edges = 2;/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [self.view addGestureRecognizer:screenEdge];    [screenEdge release];}/* 实现action:@selector(pinchAction:)*/-(void)screenAction:(UIScreenEdgePanGestureRecognizer *)screen{    NSLog(@"屏幕边缘轻扫");}

7.UILongPressGestureRecognizer (长按手势)

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view./* 创建一个UIImageView对象 */    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];    imageView.frame = CGRectMake(0, 0, 300, 300);    imageView.center = self.view.center;    imageView.layer.cornerRadius = 50;    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;    imageView.layer.masksToBounds = YES;    [self.view addSubview:imageView];    [imageView release];/* 创建UILongPressGestureRecognizer手势对象 */    UILongPressGestureRecognizer *long1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */    [imageView addGestureRecognizer:long1];    imageView.userInteractionEnabled = YES;/* 默认NO */    [long1 release];}/* 实现action:@selector(pinchAction:)*/- (void)longAction:(UILongPressGestureRecognizer *)long1{    NSLog(@"长按");}

/* 注: 摘自http://blog.csdn.net/yuling962464/article/details/47320243 */


0 0
原创粉丝点击