2018-09-06 UIPanGestureRecognizer

//
// ViewController.m
// 22
//
// Created by xjk on 2018/9/6.
// Copyright ? 2018年 xjk. All rights reserved.
//

import "ViewController.h"

CGFloat const gestureMinimumTranslation =20.0;
typedef enum:NSInteger{

kCameraMoveDirectionNone,

kCameraMoveDirectionUp,

kCameraMoveDirectionDown,

kCameraMoveDirectionRight,

kCameraMoveDirectionLeft

}CameraMoveDirection;
@interface ViewController ()
{
CameraMoveDirection direction;
}
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIPanGestureRecognizer*recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    [self.view addGestureRecognizer:recognizer];
    }

  • (void)handleSwipe:(UIPanGestureRecognizer*)gesture

{

CGPoint translation = [gesture translationInView:self.view];

if(gesture.state ==UIGestureRecognizerStateBegan)
    
{
    
    direction = kCameraMoveDirectionNone;
    
}

else if(gesture.state ==UIGestureRecognizerStateChanged&& direction == kCameraMoveDirectionNone)

{
    
    direction = [self determineCameraDirectionIfNeeded:translation];
    
    // ok, now initiate movement in the direction indicated by the user's gesture
    
    switch(direction) {
            
        case kCameraMoveDirectionDown:
            
            NSLog(@"Start moving down");
            
            break;
            
        case kCameraMoveDirectionUp:
            
            NSLog(@"Start moving up");
            
            break;
            
        case kCameraMoveDirectionRight:
            
            NSLog(@"Start moving right");
            
            break;
            
        case kCameraMoveDirectionLeft:
            
            NSLog(@"Start moving left");
            
            break;
            
        default:
            
            break;
            
    }
    
}

else if(gesture.state ==UIGestureRecognizerStateEnded)

{
    
    // now tell the camera to stop
    
    NSLog(@"Stop");
    
}

}

// This method will determine whether the direction of the user's swipe

  • (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation

{

if(direction != kCameraMoveDirectionNone)
    
    return direction;

// determine if horizontal swipe only if you meet some minimum velocity

if(fabs(translation.x) > gestureMinimumTranslation)
    
{
    
    BOOL gestureHorizontal = NO;
    
    if(translation.y ==0.0)
        
        gestureHorizontal = YES;
    
    else
        
        gestureHorizontal = (fabs(translation.x / translation.y) >5.0);
    
    if(gestureHorizontal)
        
    {
        
        if(translation.x >0.0)
            
            return kCameraMoveDirectionRight;
        
        else
            
            return kCameraMoveDirectionLeft;
        
    }
    
}

// determine if vertical swipe only if you meet some minimum velocity

else if(fabs(translation.y) > gestureMinimumTranslation)

{
    
    BOOL gestureVertical = NO;
    
    if(translation.x ==0.0)
        
        gestureVertical = YES;
    
    else
        
        gestureVertical = (fabs(translation.y / translation.x) >5.0);
    
    if(gestureVertical)
        
    {
        
        if(translation.y >0.0)
            
            return kCameraMoveDirectionDown;
        
        else
            
            return kCameraMoveDirectionUp;
        
    }
    
}

return direction;

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末十绑,一起剝皮案震驚了整個濱河市颓遏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌懈万,老刑警劉巖虑椎,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡阁危,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門汰瘫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狂打,“玉大人,你說我怎么就攤上這事混弥∨肯纾” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵蝗拿,是天一觀的道長晾捏。 經(jīng)常有香客問我,道長哀托,這世上最難降的妖魔是什么惦辛? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮仓手,結(jié)果婚禮上胖齐,老公的妹妹穿的比我還像新娘。我一直安慰自己俗或,他們只是感情好市怎,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辛慰,像睡著了一般区匠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天驰弄,我揣著相機(jī)與錄音麻汰,去河邊找鬼。 笑死戚篙,一個胖子當(dāng)著我的面吹牛五鲫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播岔擂,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼位喂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了乱灵?” 一聲冷哼從身側(cè)響起塑崖,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎痛倚,沒想到半個月后规婆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蝉稳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年抒蚜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片耘戚。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡嗡髓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出毕莱,到底是詐尸還是另有隱情器贩,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布朋截,位于F島的核電站,受9級特大地震影響吧黄,放射性物質(zhì)發(fā)生泄漏部服。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一拗慨、第九天 我趴在偏房一處隱蔽的房頂上張望廓八。 院中可真熱鬧,春花似錦赵抢、人聲如沸剧蹂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宠叼。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冒冬,已是汗流浹背伸蚯。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留简烤,地道東北人剂邮。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像横侦,于是被迫代替她去往敵國和親挥萌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容