UI基礎 手勢(上)
阿新 • • 發佈:2020-07-29
root m
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageV=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 500)]; imageV.image =[UIImage imageNamed:@"a.jpg"]; [self.view addSubview:imageV];// 開啟使用者互動事件 imageV.userInteractionEnabled=YES; //六大手勢 //1.點選手勢 //建立手勢 UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMe)]; //控制點選次數 tap.numberOfTapsRequired=3; //把手勢新增到圖片上 [imageV addGestureRecognizer:tap];//2.長按手勢 UILongPressGestureRecognizer *longP=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPresssMe:)]; //控制長按事件 (按多少秒觸發) longP.minimumPressDuration=3; //控制長按過程中手指允許移動多少 longP.allowableMovement=50; [imageV addGestureRecognizer:longP]; //3.輕掃手勢UISwipeGestureRecognizer* swip=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMe:)]; //指定方向 swip.direction=UISwipeGestureRecognizerDirectionLeft; UISwipeGestureRecognizer* swip1=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMe1:)]; //指定方向 swip.direction=UISwipeGestureRecognizerDirectionLeft; [imageV addGestureRecognizer:swip]; } //輕掃手勢對應的方法 -(void)swipMe:(UISwipeGestureRecognizer *)swipe { if(swipe.direction=UISwipeGestureRecognizerDirectionLeft) { NSLog(@"向左掃"); }else if (swipe.direction==UISwipeGestureRecognizerDirectionDown) { NSLog(@"向下掃"); } } //長按手勢對應方法 //想要實現什麼功能 就可以寫在哪個模組裡 -(void)longPresssMe:(UILongPressGestureRecognizer *)longPre { if(longPre.state==UIGestureRecognizerStateBegan){ NSLog(@"開始"); }else if (longPre.state==UIGestureRecognizerStateChanged){ NSLog(@"手抖"); }else if (longPre.state= UIGestureRecognizerStateEnded) { NSLog(@"結束"); } } //點選手勢對應的方法 -(void)tapMe { NSLog(@"已列印"); } @end