iOS疯狂讲解之手势识别器
生活随笔
收集整理的這篇文章主要介紹了
iOS疯狂讲解之手势识别器
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#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];// 創(chuàng)建一個(gè)imageViewUIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];imageView.image = [UIImage imageNamed:@"u=2473889012,1045156706&fm=21&gp=0.jpg"];// UIImageView 默認(rèn)交互是關(guān)閉的 ,所以需要打開imageView.userInteractionEnabled = YES;[self.view addSubview:imageView];[imageView release];// 手機(jī)識(shí)別器這個(gè)類 , 是一個(gè)抽象類// 自己本身不實(shí)現(xiàn)什么具體功能//具體功能都是由其子類來實(shí)現(xiàn)// 第一個(gè):輕拍手勢UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];// 用 幾個(gè)手指 輕拍tap.numberOfTouchesRequired = 2; // 默認(rèn)是1// 輕拍的次數(shù)tap.numberOfTapsRequired = 1; // 默認(rèn)是1// 把手勢添加到要點(diǎn)擊輕拍的視圖上[imageView addGestureRecognizer:tap];[tap release];/*//第二個(gè): 長按手勢UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionLongPress:)];// 最短長按時(shí)間longPress.minimumPressDuration = 1;// 添加到視圖上[imageView addGestureRecognizer:longPress];[longPress release];*//*// 第三個(gè): 旋轉(zhuǎn)手勢UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(actionRotation:)];// 添加到視圖上[imageView addGestureRecognizer:rotation];[rotation release];*//*// 第四個(gè): 捏合手勢UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(actionPinch:)];// 直接添加到視圖上[imageView addGestureRecognizer:pinch];[pinch release];*//*// 第五個(gè) 平移手勢UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)];// 添加到視圖上[imageView addGestureRecognizer:pan];[pan release];*/// // 第六個(gè) 清掃手勢
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)];
// // 設(shè)置左右掃
// swipe.direction = UISwipeGestureRecognizerDirectionLeft; // 默認(rèn)是右掃
//
// // 添加到視圖上
// [imageView addGestureRecognizer:swipe];
// [swipe release];// 第七個(gè): 邊緣掃 是默認(rèn)的 不設(shè)置也有邊緣掃UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(actionScreenEdgePan:)];// ?設(shè)置從屏幕邊緣 那邊去掃screenEdgePan.edges = UIRectEdgeLeft;[imageView addGestureRecognizer:screenEdgePan];[screenEdgePan release];}//1, 輕拍觸發(fā)方法
- (void)actionTap:(UITapGestureRecognizer *)tap
{NSLog(@"輕拍了");
}
// 2,長按觸發(fā)方法
- (void)actionLongPress:(UILongPressGestureRecognizer *)longPress
{// 長按 換圖片// 獲取到長按的viewUIImageView *imageView = (UIImageView *)longPress.view;imageView.image = [UIImage imageNamed:@"u=3484715933,459413563&fm=23&gp=0.jpg"];NSLog(@"長按手勢實(shí)現(xiàn)");
}
// 3,旋轉(zhuǎn)觸發(fā)方法
- (void)actionRotation:(UIRotationGestureRecognizer *)rotation
{// transform 形變屬性// 描述一下這個(gè)方法// 第一個(gè)參數(shù) 傳入要?jiǎng)?chuàng)建那個(gè)視圖的形變屬性// 第二個(gè)參數(shù) 傳入 手勢的弧度rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);// 把弧度重置rotation.rotation = 0;NSLog(@"旋轉(zhuǎn)了");
}//4, 捏合觸發(fā)方法
- (void)actionPinch:(UIPinchGestureRecognizer *)pinch
{// 形變屬性控制 捏合// 第二個(gè)參數(shù) 按捏合的刻度比例 形變pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);// 重置刻度比例pinch.scale = 1;NSLog(@"捏合手勢");
}// 5. 平移觸發(fā)方法
- (void)actionPan:(UIPanGestureRecognizer *)pan
{// 這個(gè)點(diǎn) 以手指觸摸到屏幕那一刻 即為0,0點(diǎn)CGPoint p = [pan translationInView:pan.view];NSLog(@"%@", NSStringFromCGPoint(p));pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);// 重置 點(diǎn) 讓他以為每次都是剛開始觸發(fā)[pan setTranslation:CGPointMake(0, 0) inView:pan.view];//[pan translationInView:self.view];
}// 6. 左右掃觸發(fā)方法
- (void)actionSwipe:(UISwipeGestureRecognizer *)swipe
{NSLog(@"左掃了");}// 7. 邊緣掃觸發(fā)方法
- (void)actionScreenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{// 剛一掃就觸發(fā)if (screenEdgePan.state == UIGestureRecognizerStateBegan) {NSLog(@"邊緣掃 左掃");}}
總結(jié)
以上是生活随笔為你收集整理的iOS疯狂讲解之手势识别器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩转华为ENSP模拟器系列 | 配置OS
- 下一篇: 在sweetalert弹出窗插件中加入h