<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

    iOS開發(fā)之手勢(shì)識(shí)別

     感覺(jué)有必要把iOS開發(fā)中的手勢(shì)識(shí)別做一個(gè)小小的總結(jié)。在上一篇iOS開發(fā)之自定義表情鍵盤(組件封裝與自動(dòng)布局)博客中用到了一個(gè)輕擊手勢(shì),就是在輕擊TextView時(shí)從表情鍵盤回到系統(tǒng)鍵盤,在TextView中的手是用storyboard添加的。下面會(huì)先給出如何用storyboard給相應(yīng)的控件添加手勢(shì),然后在用純代碼的方式給我們的控件添加手勢(shì),手勢(shì)的用法比較簡(jiǎn)單。和button的用法類似,也是目標(biāo)動(dòng)作回調(diào),話不多說(shuō),切入今天的正題。總共有六種手勢(shì)識(shí)別:輕擊手勢(shì)(TapGestureRecognizer),輕掃手勢(shì)(SwipeGestureRecognizer), 長(zhǎng)按手勢(shì)(LongPressGestureRecognizer),  拖動(dòng)手勢(shì)(PanGestureRecognizer), 捏合手勢(shì)(PinchGestureRecognizer),旋轉(zhuǎn)手勢(shì)(RotationGestureRecognizer);
      其實(shí)這些手勢(shì)用touche事件完全可以實(shí)現(xiàn),蘋果就是把常用的觸摸事件封裝成手勢(shì),來(lái)提供給用戶。讀者完全可以用TouchesMoved來(lái)寫拖動(dòng)手勢(shì)等
      一,用storyboard給控件添加手勢(shì)識(shí)別,當(dāng)然啦用storyboard得截張圖啦
      1.用storyboard添加手勢(shì)識(shí)別,和添加一個(gè)Button的步驟一樣,首先我們得找到相應(yīng)的手勢(shì),把手勢(shì)識(shí)別的控件拖到我們要添加手勢(shì)的控件中,截圖如下:
      2.給我們拖出的手勢(shì)添加回調(diào)事件,和給Button回調(diào)事件沒(méi)啥區(qū)別的,在回調(diào)方法中添加要實(shí)現(xiàn)的業(yè)務(wù)邏輯即可,截圖如下:
      二,純代碼添加手勢(shì)識(shí)別
      用storyboard可以大大簡(jiǎn)化我們的操作,不過(guò)純代碼的方式還是要會(huì)的,就像要Dreamwear編輯網(wǎng)頁(yè)一樣(當(dāng)然啦,storyboard的拖拽功能要比Dreamwear的拖拽強(qiáng)大的多),用純代碼敲出來(lái)的更為靈活,更加便于維護(hù)。不過(guò)用storyboard可以減少我們的工作量,這兩個(gè)要配合著使用才能大大的提高我們的開發(fā)效率。個(gè)人感覺(jué)用storyboard把框架搭起來(lái)(Controller間的關(guān)系),一下小的東西還是用純代碼敲出來(lái)更好一些。下面就給出如何給我們的控件用純代碼的方式來(lái)添加手勢(shì)識(shí)別。
      1.輕擊手勢(shì)(TapGestureRecognizer)的添加
      初始化代碼TapGestureRecongnizer的代碼如下:
      1     //新建tap手勢(shì)
      2     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
      3     //設(shè)置點(diǎn)擊次數(shù)和點(diǎn)擊手指數(shù)
      4     tapGesture.numberOfTapsRequired = 1; //點(diǎn)擊次數(shù)
      5     tapGesture.numberOfTouchesRequired = 1; //點(diǎn)擊手指數(shù)
      6     [self.view addGestureRecognizer:tapGesture];
      在回調(diào)方法中添加相應(yīng)的業(yè)務(wù)邏輯:
      1 //輕擊手勢(shì)觸發(fā)方法
      2 -(void)tapGesture:(id)sender
      3 {
      4     //輕擊后要做的事情
      5 }
      2.長(zhǎng)按手勢(shì)(LongPressGestureRecognizer)
      初始化代碼:
      1     //添加長(zhǎng)摁手勢(shì)
      2     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
      3     //設(shè)置長(zhǎng)按時(shí)間
      4     longPressGesture.minimumPressDuration = 0.5; //(2秒)
      5     [self.view addGestureRecognizer:longPressGesture];
      在對(duì)應(yīng)的回調(diào)方法中添加相應(yīng)的方法(當(dāng)手勢(shì)開始時(shí)執(zhí)行):
      1 //常摁手勢(shì)觸發(fā)方法
      2 -(void)longPressGesture:(id)sender
      3 {
      4     UILongPressGestureRecognizer *longPress = sender;
      5     if (longPress.state == UIGestureRecognizerStateBegan)
      6     {
      7         UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"長(zhǎng)按觸發(fā)" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
      8         [alter show];
      9     }
      10 }
     代碼說(shuō)明:手勢(shì)的常用狀態(tài)如下
      開始:UIGestureRecognizerStateBegan
      改變:UIGestureRecognizerStateChanged
      結(jié)束:UIGestureRecognizerStateEnded
      取消:UIGestureRecognizerStateCancelled
      失敗:UIGestureRecognizerStateFailed
      3.輕掃手勢(shì)(SwipeGestureRecognizer)
      在初始化輕掃手勢(shì)的時(shí)候得指定輕掃的方向,上下左右。 如果要要添加多個(gè)輕掃方向,就得添加多個(gè)輕掃手勢(shì),不過(guò)回調(diào)的是同一個(gè)方法。
      添加輕掃手勢(shì),一個(gè)向左一個(gè)向右,代碼如下:
      1     //添加輕掃手勢(shì)
      2     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
      3     //設(shè)置輕掃的方向
      4     swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默認(rèn)向右
      5     [self.view addGestureRecognizer:swipeGesture];
      6
      7     //添加輕掃手勢(shì)
      8     UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
      9     //設(shè)置輕掃的方向
      10     swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默認(rèn)向右
      11     [self.view addGestureRecognizer:swipeGestureLeft];
      回調(diào)方法如下:
    1 //輕掃手勢(shì)觸發(fā)方法
    2 -(void)swipeGesture:(id)sender
    3 {
    4     UISwipeGestureRecognizer *swipe = sender;
    5     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    6     {
    7         //向左輕掃做的事情
    8     }
    9     if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    10     {
    11         //向右輕掃做的事情
    12     }
    13 }
    14
      4.捏合手勢(shì)(PinchGestureRecognizer)
      捏合手勢(shì)初始化
      1     //添加捏合手勢(shì)
      2     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
      3     [self.view addGestureRecognizer:pinchGesture];
      捏合手勢(shì)要觸發(fā)的方法(放大或者縮小圖片):
    1 ////捏合手勢(shì)觸發(fā)方法
    2 -(void) pinchGesture:(id)sender
    3 {
    4      UIPinchGestureRecognizer *gesture = sender;
    5
    6     //手勢(shì)改變時(shí)
    7     if (gesture.state == UIGestureRecognizerStateChanged)
    8     {
    9         //捏合手勢(shì)中scale屬性記錄的縮放比例
    10         _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    11     }
    12
    13     //結(jié)束后恢復(fù)
    14     if(gesture.state==UIGestureRecognizerStateEnded)
    15     {
    16         [UIView animateWithDuration:0.5 animations:^{
    17             _imageView.transform = CGAffineTransformIdentity;//取消一切形變
    18         }];
    19     }
    20 }
      5.拖動(dòng)手勢(shì)(PanGestureRecognizer)
      拖動(dòng)手勢(shì)的初始化
      1     //添加拖動(dòng)手勢(shì)
      2     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
      3     [self.view addGestureRecognizer:panGesture];
      拖動(dòng)手勢(shì)要做的方法(通過(guò)translationInView獲取移動(dòng)的點(diǎn),和TouchesMoved方法類似)
      1 //拖動(dòng)手勢(shì)
      2 -(void) panGesture:(id)sender
      3 {
      4     UIPanGestureRecognizer *panGesture = sender;
      5
      6     CGPoint movePoint = [panGesture translationInView:self.view];
      7
      8     //做你想做的事兒
      9 }
     6.旋轉(zhuǎn)手勢(shì)(RotationGestureRecognizer)
      旋轉(zhuǎn)手勢(shì)的初始化
      1     //添加旋轉(zhuǎn)手勢(shì)
      2     UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
      3     [self.view addGestureRecognizer:rotationGesture];
      旋轉(zhuǎn)手勢(shì)調(diào)用的方法:
    1 //旋轉(zhuǎn)手勢(shì)
    2 -(void)rotationGesture:(id)sender
    3 {
    4
    5     UIRotationGestureRecognizer *gesture = sender;
    6
    7     if (gesture.state==UIGestureRecognizerStateChanged)
    8     {
    9         _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
    10     }
    11
    12     if(gesture.state==UIGestureRecognizerStateEnded)
    13     {
    14
    15         [UIView animateWithDuration:1 animations:^{
    16             _imageView.transform=CGAffineTransformIdentity;//取消形變
    17         }];
    18     }
    19
    20 }
      上面的東西沒(méi)有多高深的技術(shù),就是對(duì)iOS開發(fā)中的手勢(shì)做了一下小小的總結(jié),溫故一下基礎(chǔ)知識(shí)。

    posted on 2014-11-19 10:03 順其自然EVO 閱讀(242) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄

    <2014年11月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 成年大片免费视频| 亚洲免费观看网站| 亚洲一区二区三区深夜天堂| 成人免费网站视频www| 成年丰满熟妇午夜免费视频| 色吊丝性永久免费看码| 色播亚洲视频在线观看| 中国一级特黄高清免费的大片中国一级黄色片| 日本免费福利视频| 亚洲精品国产精品国自产网站| 97久久免费视频| 亚洲AV中文无码字幕色三| 一级成人a免费视频| 亚洲精品国产电影| 黄床大片30分钟免费看| 免费人成网站7777视频| 一道本不卡免费视频| 亚洲伊人久久大香线焦| 亚洲精品无码久久久久去q| 黄色视屏在线免费播放| 国产亚洲一区二区三区在线观看| 人妻视频一区二区三区免费| 狠狠色伊人亚洲综合网站色| 在线播放免费人成视频在线观看| 久久精品亚洲日本波多野结衣| 国产精品免费观看久久| 免费国产99久久久香蕉| 久久久无码精品亚洲日韩蜜桃| 69视频免费观看l| 国产日韩在线视频免费播放| 亚洲Av永久无码精品黑人| 免费国产a国产片高清| 1024免费福利永久观看网站| 亚洲精品国产国语| 久久夜色精品国产噜噜噜亚洲AV| 亚洲国产精品尤物YW在线观看| 国产精品偷伦视频观看免费 | 韩国免费三片在线视频| 美女被免费视频网站a| 国产午夜亚洲精品| 亚洲视频中文字幕在线|