IOS中的七种手势小结
程序员文章站
2023-12-20 19:41:34
今天为大家介绍一下ios 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码:
// 初始化一个uiimageview
uii...
今天为大家介绍一下ios 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码:
// 初始化一个uiimageview uiimageview *imageview = [[uiimageview alloc]initwithframe:cgrectmake(100, 100, 300, 300)]; imageview.image = [uiimage imagenamed:@"12.jpg"]; // uiimageview的用户交互是默认关闭的,要想使他可以处理触摸事件,我们得手动打开它 [imageview setuserinteractionenabled:yes]; [self.window addsubview:imageview]; //初始化一个视图(响应者)来承载手势 /*uiview *gestureview = [[uiview alloc] initwithframe:cgrectmake(100, 100, 100, 100)]; //当前视图放置到屏幕* gestureview.center = self.window.center; gestureview.backgroundcolor = [uicolor yellowcolor]; [self.window addsubview:gestureview];
1、轻拍手势
//创建轻拍手势 uitapgesturerecognizer *tapgr = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapaction:)];
1.设置触控对象,和轻拍的次数
//设置触控对象的个数(几个手指) [tapgr setnumberoftouchesrequired:1]; //设置轻拍次数 [tapgr setnumberoftapsrequired:2]; //给创建好的视图添加手势 [gestureview addgesturerecognizer:tapgr]; //轻拍手势的回调方法 - (void)tapaction:(uitapgesturerecognizer*)sender{ //可以根据手势得到它当前所作用的视图 uiimageview *imageview = (uiimageview*)sender.view; //得到textfield viewwithtag此方法的返回值为uiview类型,但是uitextfield为uiview的子类,父类对象不能直接指向子类对象,所以需要强制转换 uitextfield *textfield = (uitextfield*)[self.window viewwithtag:1000]; //回收键盘,取消第一响应者 [textfield resignfirstresponder]; nslog(@"我轻拍了gestureview"); }
2、捏合手势
//创建捏合手势 uipinchgesturerecognizer* pinchgr = [[uipinchgesturerecognizer alloc] initwithtarget:self action:@selector(pinchaction:)]; pinchgr.delegate = self; // 可以在同一个视图上实现多个手势 //捏合手势的回调方法 - (void)pinchaction:(uipinchgesturerecognizer*)sender{ //通过捏合手势的到缩放比率 float scale = sender.scale; //得到该手势所作用的视图 uiview *view = sender.view; //2d仿射变换函数中的缩放函数来实现视图的放大缩小 //是在原有基础上来改变当前的视图 //函数的第一个参数:现有的视图的transform值 //第二个参数:x轴上的缩放比率 //第三个参数:y轴上的缩放比率 //是在视图最初的transform状态上改变,不管执行多少次,都是以该视图最初的transform状态为基础来改变 view.transform = cgaffinetransformmakescale(2, 2); view.transform = cgaffinetransformscale(view.transform, scale, scale); //每次捏合动作完毕之后,让此捏合值复原,使得它每次都是从100%开始缩放 sender.scale = 1; }
3、旋转手势
//旋转手势 uirotationgesturerecognizer* rotagr = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@selector(rotaaction:)]; rotagr.delegate = self; //旋转手势回调方法 - (void)rotaaction:(uirotationgesturerecognizer*)sender{ //通过手势的到旋转角度 float rota = sender.rotation; //得到该手势作用的视图 uiview *view = sender.view; //通过2d仿射变换函数中的旋转函数来使得当前视图旋转。 view.transform = cgaffinetransformrotate(view.transform, rota); //复原 sender.rotation = 0; }
4、平移手势
//平移手势 uipangesturerecognizer *pangp = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(panaction:)]; //平移手势的回调方法 - (void)panaction:(uipangesturerecognizer*)sender{ //得到当前手势所在视图 uiview *view = sender.view; //得到我们在视图上移动的偏移量 cgpoint currentpoint = [sender translationinview:view.superview]; //通过2d仿射变换函数中与位移有关的函数实现视图位置变化 view.transform = cgaffinetransformtranslate(view.transform, currentpoint.x, currentpoint.y); //复原 // 每次都是从00点开始 [sender settranslation:cgpointzero inview:view.superview]; }
5、边缘轻扫手势
//边缘轻扫手势 uiscreenedgepangesturerecognizer *edgepangr = [[uiscreenedgepangesturerecognizer alloc] initwithtarget:self action:@selector(edgepanaction:)]; edgepangr.edges = uirectedgeall; //边缘轻扫手势回调方法 - (void)edgepanaction:(uiscreenedgepangesturerecognizer*)sender{ nslog(@"我成功的触发了屏幕边缘手势"); }
6、长按手势
// ⑥长按手势 uilongpressgesturerecognizer *longpresspr = [[uilongpressgesturerecognizer alloc]initwithtarget:self action:@selector(longpressaction:)]; longpresspr.minimumpressduration = 1; // ⑥长按手势的回调方法 - (void)longpressaction:(uilongpressgesturerecognizer *)sender{ if (sender.state == uigesturerecognizerstateended) { uialertview *alertview = [[uialertview alloc] initwithtitle:@"看你麻痹" message:@"不服你咬死我" delegate:self cancelbuttontitle:@"取消" otherbuttontitles:@"确定", nil]; [alertview show]; } }
7、轻扫手势
// ⑦轻扫手势 uiswipegesturerecognizer *swipegr = [[uiswipegesturerecognizer alloc]initwithtarget:self action:@selector(swipeaction:)]; // ⑦轻扫手势的回调方法 - (void)swipeaction:(uiswipegesturerecognizer *)sender{ if (sender.state == uigesturerecognizerstateended) { uiactionsheet *actionsheet = [[uiactionsheet alloc]initwithtitle:@"槽尼玛" delegate:self cancelbuttontitle:@"纸张" destructivebuttontitle:@"哈哈哈" otherbuttontitles:@"切毛毛", nil]; [actionsheet showinview:self.window]; } }
给imageview视图添加手势
// 3.给图片添加手势 一个视图可以添加多种手势,但是一个手势,只能添加到一个视图上 [imageview addgesturerecognizer:tapgr]; [imageview addgesturerecognizer:pinchgr]; [imageview addgesturerecognizer:rotagr]; [imageview addgesturerecognizer:pangr]; [imageview addgesturerecognizer:edgepangr]; [imageview addgesturerecognizer:longpresspr]; [imageview addgesturerecognizer:swipegr];
当一个视图上想要添加多种手势的时候就要用到手势的代理(重点)
pragma mark ----手势的代理方法 // 使得多个手势可以同时响应 - (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer{ // 返回值为yes的时候,当执行一个手势的操作的时候,也可以执行其他手势的操作 return yes; }
以上所述是小编给大家介绍的ios中的七种手势小结,希望对大家有所帮助