您的位置:首页 > 其它

五大手势

2015-10-15 23:20 288 查看
//

// ViewController.m

// UI-NO-8

//

// Created by Bruce on 15/7/22.

// Copyright (c) 2015年 Bruce. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

{

UIImageView *imageView;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

imageView.center = self.view.center;

imageView.image = [UIImage imageNamed:@"1.tiff"];

[self.view addSubview:imageView];

// 如果不设置背景颜色的话 有可能 不响应手势

// 手势如果不响应

// 1、看是否添加到了imageView上 如果是 把userInteractionEnabled 设置成YES

// 2、查看 是否设置了背景颜色 如果没有设置 设置背景颜色

// 3、手势冲突 设置手势优先级 (截获触发事件、手势)设置优先级

// 五大手势:点击(长按) 拖拽 旋转 捏合 轻扫 都属于手势 继承自UIGestureRecognizer 父类

// 之一 点击

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

// 设置点击手指的个数

tap.numberOfTouchesRequired = 1;

// 设置手指点击的次数

tap.numberOfTapsRequired = 1;

[self.view addGestureRecognizer:tap];

// 之二 长按

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

// 最小 按下的持续时间

longPress.minimumPressDuration = 1;

longPress.numberOfTouchesRequired = 2;

// 把手势 添加到 某个视图上addGestureRecognizer

[self.view addGestureRecognizer:longPress];

// 之三 拖拽

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

// 设置最少的手指个数

// pan.minimumNumberOfTouches = 2;

// 设置最多的手指个数

// pan.maximumNumberOfTouches = 2;

[self.view addGestureRecognizer:pan];

// 之四 轻扫

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

swipe.direction = UISwipeGestureRecognizerDirectionLeft;

// 处理 拖拽和轻扫 两个手势的冲突

// 需要轻扫手势 执行完毕之后 再去执行拖拽手势

[pan requireGestureRecognizerToFail:swipe];

[self.view addGestureRecognizer:swipe];

// 之五 捏合

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

[self.view addGestureRecognizer:pinch];

// 之六 旋转

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

[self.view addGestureRecognizer:rotation];

}

- (void)rotation:(UIRotationGestureRecognizer *)sender

{

// 通过旋转手势 让图片进行相同的变形

imageView.transform = CGAffineTransformMakeRotation(sender.rotation);

}

- (void)pinch:(UIPinchGestureRecognizer *)sender

{

// 通过手势 得到的 变化比例 让imageView的形态发生改变

imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);

imageView.center = self.view.center;

}

- (void)swipe:(UISwipeGestureRecognizer *)sender

{

CGFloat w = CGRectGetWidth([UIScreen mainScreen].bounds);

CGFloat h = CGRectGetHeight([UIScreen mainScreen].bounds);

self.view.frame = CGRectMake(w, 0, w, h);

[UIView animateWithDuration:2 animations:^{

self.view.frame = CGRectMake(0, 0, w, h);

}];

}

- (void)pan:(UIPanGestureRecognizer *)sender

{

// 获得 手势的触摸点

CGPoint tapLoc = [sender locationInView:self.view];

NSLog(@"%f %f",tapLoc.x,tapLoc.y);

// 点记的位置 设置成图片视图的 中心点

imageView.center = tapLoc;

}

- (void)longPress:(UILongPressGestureRecognizer *)sender

{

// 获得 手势的触摸点

CGPoint tapLoc = [sender locationInView:self.view];

NSLog(@"%f %f",tapLoc.x,tapLoc.y);

[UIView animateWithDuration:2 animations:^{

// 点记的位置 设置成图片视图的 中心点

imageView.center = tapLoc;

}];

}

//单击手势触发的方法

- (void)tapAction:(UITapGestureRecognizer *)sender

{

// self.view.backgroundColor = [UIColor redColor];

// 恢复视图的初始形态

imageView.transform = CGAffineTransformIdentity;

CGPoint tapLoc = [sender locationInView:self.view];

NSLog(@"%f %f",tapLoc.x,tapLoc.y);

[UIView animateWithDuration:0.5 animations:^{

// 点记的位置 设置成图片视图的 中心点

imageView.center = tapLoc;

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: