#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
// 創建一個imageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageNamed:@"u=2473889012,1045156706&fm=21&gp=0.jpg"];
// UIImageView 默認交互是關閉的 ,所以需要打開
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
[imageView release];
// 手機識別器這個類 , 是一個抽象類
// 自己本身不實現什麼具體功能
//具體功能都是由其子類來實現
// 第一個:輕拍手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];
// 用 幾個手指 輕拍
tap.numberOfTouchesRequired = 2; // 默認是1
// 輕拍的次數
tap.numberOfTapsRequired = 1; // 默認是1
// 把手勢添加到要點擊輕拍的視圖上
[imageView addGestureRecognizer:tap];
[tap release];
/*
//第二個: 長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionLongPress:)];
// 最短長按時間
longPress.minimumPressDuration = 1;
// 添加到視圖上
[imageView addGestureRecognizer:longPress];
[longPress release];
*/
/*
// 第三個: 旋轉手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(actionRotation:)];
// 添加到視圖上
[imageView addGestureRecognizer:rotation];
[rotation release];
*/
/*
// 第四個: 捏合手勢
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(actionPinch:)];
// 直接添加到視圖上
[imageView addGestureRecognizer:pinch];
[pinch release];
*/
/*
// 第五個 平移手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)];
// 添加到視圖上
[imageView addGestureRecognizer:pan];
[pan release];
*/
// // 第六個 清掃手勢
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)];
// // 設置左右掃
// swipe.direction = UISwipeGestureRecognizerDirectionLeft; // 默認是右掃
//
// // 添加到視圖上
// [imageView addGestureRecognizer:swipe];
// [swipe release];
// 第七個: 邊緣掃 是默認的 不設置也有邊緣掃
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(actionScreenEdgePan:)];
// 設置從屏幕邊緣 那邊去掃
screenEdgePan.edges = UIRectEdgeLeft;
[imageView addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
}
//1, 輕拍觸發方法
- (void)actionTap:(UITapGestureRecognizer *)tap
{
NSLog(@"輕拍了");
}
// 2,長按觸發方法
- (void)actionLongPress:(UILongPressGestureRecognizer *)longPress
{
// 長按 換圖片
// 獲取到長按的view
UIImageView *imageView = (UIImageView *)longPress.view;
imageView.image = [UIImage imageNamed:@"u=3484715933,459413563&fm=23&gp=0.jpg"];
NSLog(@"長按手勢實現");
}
// 3,旋轉觸發方法
- (void)actionRotation:(UIRotationGestureRecognizer *)rotation
{
// transform 形變屬性
// 描述一下這個方法
// 第一個參數 傳入要創建那個視圖的形變屬性
// 第二個參數 傳入 手勢的弧度
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
// 把弧度重置
rotation.rotation = 0;
NSLog(@"旋轉了");
}
//4, 捏合觸發方法
- (void)actionPinch:(UIPinchGestureRecognizer *)pinch
{
// 形變屬性控制 捏合
// 第二個參數 按捏合的刻度比例 形變
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 重置刻度比例
pinch.scale = 1;
NSLog(@"捏合手勢");
}
// 5. 平移觸發方法
- (void)actionPan:(UIPanGestureRecognizer *)pan
{
// 這個點 以手指觸摸到屏幕那一刻 即爲0,0點
CGPoint p = [pan translationInView:pan
.view];
NSLog(@"%@", NSStringFromCGPoint(p));
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// 重置 點 讓他以爲每次都是剛開始觸發
[pan setTranslation:CGPointMake(0, 0) inView:pan.view];
//[pan translationInView:self.view];
}
// 6. 左右掃觸發方法
- (void)actionSwipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"左掃了");
}
// 7. 邊緣掃觸發方法
- (void)actionScreenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
// 剛一掃就觸發
if (screenEdgePan.state == UIGestureRecognizerStateBegan) {
NSLog(@"邊緣掃 左掃");
}
}
iOS瘋狂講解之手勢識別器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.