手勢(shì)

手勢(shì)

@property (nonatomic, strong) UIView *testView;
///記錄旋轉(zhuǎn)角度
@property (nonatomic, assign) CGFloat rotation;
///記錄縮放比例
@property (nonatomic, assign) CGFloat scale;
// !!!: 點(diǎn)擊
- (void)addTapGestureRecognizer {
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    [self.view addGestureRecognizer:tapGes];
}
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {
    NSLog(@"點(diǎn)擊");
}
// !!!: 長(zhǎng)按
- (void)addLongPressGestureRecognizer {
    UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer  alloc]initWithTarget:self action:@selector(longPressGestureRecognizer:)];
    //用幾個(gè)手指觸屏愧驱,默認(rèn)1,設(shè)置多少就必須多少觸碰點(diǎn)
    longPressGes.numberOfTouchesRequired = 1;
    //設(shè)置最短長(zhǎng)按時(shí)間审孽,單位為秒(默認(rèn)0.5)
    longPressGes.minimumPressDuration = 1;
    //設(shè)置手勢(shì)識(shí)別期間所允許的手勢(shì)可移動(dòng)范圍,默認(rèn)10
    longPressGes.allowableMovement = 10;
    [self.view addGestureRecognizer:longPressGes];
}
- (void)longPressGestureRecognizer:(UILongPressGestureRecognizer *)sender {
//    CGPoint point  = [sender locationInView:self.view];
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"UIGestureRecognizerStateBegan");
    }else if (sender.state == UIGestureRecognizerStateChanged) {
        NSLog(@"UIGestureRecognizerStateChanged");
    }else if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"UIGestureRecognizerStateEnded");
    }
    //其他沒啥用
}
// !!!: 快速滑動(dòng)
- (void)addSwipeGestureRecognizer {
    UISwipeGestureRecognizer *leftSwipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    leftSwipeGes.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGes];
    UISwipeGestureRecognizer *rightSwipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
    rightSwipeGes.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGes];
}
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)sender {
    NSLog(@"%@",@(sender.direction));
}
// !!!: 慢速滑動(dòng)
- (void)addPanGestureRecognizer {
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
    [self.view addGestureRecognizer:panGesture];
}
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)sender {
    CGPoint point = [sender translationInView:self.view];
    if (ABS(point.x) > ABS(point.y)*2) {//30度
        if (point.x > 10) {//速度
            
        }else if (point.x < -10) {
            
        }
    }
    //每次調(diào)用之后拭宁,需要重置手勢(shì)的偏移量聚假,否則偏移量會(huì)自動(dòng)累加
    [sender setTranslation:CGPointZero inView:self.view];
}
// !!!: 邊緣滑動(dòng)
- (void)addScreenEdgePanGestureRecognizer {
    UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureRecognizer:)]; //手勢(shì)由self來管理
    edgePan.edges = UIRectEdgeRight;
    [self.view addGestureRecognizer:edgePan];
}
- (void)screenEdgePanGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)sender {
    CGFloat progress = fabs([sender translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);
    NSLog(@"%@-%@",@(sender.state).description,@(progress).description);
}
// !!!: 縮放
- (void)addPinchGestureRecognizer {
    UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureRecognizer:)];
    //    scale
    [self.view addGestureRecognizer:gesture];
    self.testView.backgroundColor = [UIColor greenColor];
}
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)sender {
    CGFloat scale = sender.scale;
    scale = self.scale * scale;
    if (sender.state == UIGestureRecognizerStateEnded) {
        self.scale = scale;
    }
    self.testView.transform = CGAffineTransformMake(scale, 0, 0, scale, 0, 0);
}
// !!!: 旋轉(zhuǎn)
- (void)addRotationGestureRecognizer {
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureRecognizer:)];
    //    rotation: //獲取旋轉(zhuǎn)角度
    //    velocity: //獲取旋轉(zhuǎn)速度
    [self.view addGestureRecognizer:rotationGesture];
    self.testView.backgroundColor = [UIColor greenColor];
}
- (void)rotationGestureRecognizer:(UIRotationGestureRecognizer *)sender {
    // 獲取手勢(shì)旋轉(zhuǎn)的弧度
    CGFloat rotation = sender.rotation;
    rotation += self.rotation;
    if (sender.state == UIGestureRecognizerStateEnded) {
        self.rotation = rotation;
        while (self.rotation < -M_PI*2) {
            self.rotation += M_PI*2;
        }
        while (self.rotation > M_PI*2) {
            self.rotation -= M_PI*2;
        }
    }
    self.testView.transform = CGAffineTransformMakeRotation(rotation);
}
- (UIView *)testView
{
    if (!_testView) {
        _testView = [UIView newAutoLayoutView];
        [self.view addSubview:_testView];
        [_testView autoCenterInSuperview];
        [_testView autoSetDimensionsToSize:CGSizeMake(50, 50)];
    }
    return _testView;
}
- (CGFloat)scale
{
    if (_scale == 0) {
        _scale = 1;
    }
    return _scale;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末碌廓,一起剝皮案震驚了整個(gè)濱河市敬锐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌呆瞻,老刑警劉巖台夺,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異痴脾,居然都是意外死亡颤介,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門赞赖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來滚朵,“玉大人,你說我怎么就攤上這事前域≡” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵匿垄,是天一觀的道長(zhǎng)移宅。 經(jīng)常有香客問我,道長(zhǎng)椿疗,這世上最難降的妖魔是什么漏峰? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮届榄,結(jié)果婚禮上浅乔,老公的妹妹穿的比我還像新娘。我一直安慰自己痒蓬,他們只是感情好童擎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著攻晒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪班挖。 梳的紋絲不亂的頭發(fā)上鲁捏,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音萧芙,去河邊找鬼给梅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛双揪,可吹牛的內(nèi)容都是我干的动羽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼渔期,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼运吓!你這毒婦竟也來了渴邦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤拘哨,失蹤者是張志新(化名)和其女友劉穎谋梭,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體倦青,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瓮床,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了产镐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片隘庄。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖癣亚,靈堂內(nèi)的尸體忽然破棺而出峭沦,到底是詐尸還是另有隱情,我是刑警寧澤逃糟,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布吼鱼,位于F島的核電站,受9級(jí)特大地震影響绰咽,放射性物質(zhì)發(fā)生泄漏菇肃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一取募、第九天 我趴在偏房一處隱蔽的房頂上張望琐谤。 院中可真熱鬧,春花似錦玩敏、人聲如沸斗忌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽织阳。三九已至,卻和暖如春砰粹,著一層夾襖步出監(jiān)牢的瞬間唧躲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來泰國打工碱璃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留弄痹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓嵌器,卻偏偏與公主長(zhǎng)得像肛真,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子爽航,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

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