亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS開(kāi)發(fā)中的幾個(gè)手勢(shì)操作實(shí)例分享

 更新時(shí)間:2015年09月21日 09:01:32   作者:TommyYaphetS  
這篇文章主要介紹了iOS開(kāi)發(fā)中的幾個(gè)手勢(shì)操作實(shí)例分享,編寫(xiě)代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下

手勢(shì)操作---識(shí)別單擊還是雙擊
在視圖上同時(shí)識(shí)別單擊手勢(shì)和雙擊手勢(shì)的問(wèn)題在于,當(dāng)檢測(cè)到一個(gè)單擊操作時(shí),無(wú)法確定是確實(shí)是一個(gè)單擊操作或者只是雙擊操作中的第一次點(diǎn)擊。解決這個(gè)問(wèn)題的方法就是:在檢測(cè)到單擊時(shí),需要等一段時(shí)間等待第二次點(diǎn)擊,如果沒(méi)有第二次點(diǎn)擊,則為單擊操作;如果有第二次點(diǎn)擊,則為雙擊操作。
檢測(cè)手勢(shì)有兩種方法,一種是定制子視圖,重寫(xiě)視圖從UIResponder類(lèi)中繼承來(lái)的事件處理方法,即touchesBegan:withEvent:等一系列方法來(lái)檢測(cè)手勢(shì);另一個(gè)方法是使用手勢(shì)識(shí)別器,即UIGestureRecognizer的各種具體子類(lèi)。
一.重寫(xiě)事件處理方法

復(fù)制代碼 代碼如下:

- (id)init { 
    if ((self = [super init])) { 
        self.userInteractionEnabled = YES; 
        self.multipleTouchEnabled = YES; 
        // ... 
    } 
    return self; 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self]; 
 
    if (touch.tapCount == 1) { 
        [self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3]; 
    }else if(touch.tapCount == 2) 
    { 
        [self handleDoubleTap:[NSValue valueWithCGPoint:touchPoint]]; 
    } 

 
-(void)handleSingleTap:(NSValue*)pointValue 

    CGPoint touchPoint = [pointValue CGPointValue]; 
    //... 

 
-(void)handleDoubleTap:(NSValue*)pointValue 

    CGPoint touchPoint = [pointValue CGPointValue]; 
    //... 

首先確認(rèn)定制視圖的userInteractionEnabled和multipleTouchEnabled屬性都為YES.
在touchesEnded:withEvent:方法中,如果是第一次觸摸結(jié)束,則cancelPreviousPerformRequestsWithTarget:方法不會(huì)起作用,因?yàn)閟elf未調(diào)度任何方法,此時(shí)tapCount為1,使用performSelector:withObject:afterDelay:調(diào)用單擊事件處理方法,在0.3s鐘后執(zhí)行。

復(fù)制代碼 代碼如下:

[self performSelector:@selector(handleSingleTap:) withObject:[NSValue valueWithCGPoint:touchPoint] afterDelay:0.3];


如果這是一個(gè)單擊操作,則后面0.3鐘內(nèi)不會(huì)再有觸摸事件,則handleSingleTap:方法執(zhí)行,這樣識(shí)別出了單擊操作。
如果這是一個(gè)雙擊操作,則第二次點(diǎn)擊在0.3s內(nèi)觸發(fā),在第二次觸摸操作的touchesEnded:withEvent:方法中,cancelPreviousPerformRequestsWithTarget:首先會(huì)取消之前對(duì)handleSingleTap:方法的調(diào)度,使之不會(huì)執(zhí)行,然后在調(diào)用handleDoubleTap:方法處理雙擊操作。
二.使用Gesture Recognizer
使用Gesture Recognizer識(shí)別就會(huì)簡(jiǎn)單許多,只需添加兩個(gè)手勢(shì)識(shí)別器,分別檢測(cè)單擊和雙擊事件,設(shè)置必要的屬性即可。

復(fù)制代碼 代碼如下:

- (id)init { 
    if ((self = [super init])) { 
    self.userInteractionEnabled = YES; 
        UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)]; 
        singleTapGesture.numberOfTapsRequired = 1; 
        singleTapGesture.numberOfTouchesRequired  = 1; 
        [self addGestureRecognizer:singleTapGesture]; 
 
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)]; 
        doubleTapGesture.numberOfTapsRequired = 2; 
        doubleTapGesture.numberOfTouchesRequired = 1; 
        [self addGestureRecognizer:doubleTapGesture]; 
 
        [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture]; 
    } 
    return self; 

-(void)handleSingleTap:(UIGestureRecognizer *)sender{ 
    CGPoint touchPoint = [sender locationInView:self]; 
    //... 

-(void)handleDoubleTap:(UIGestureRecognizer *)sender{ 
    CGPoint touchPoint = [sender locationInView:self]; 
    //... 

唯一需要注意的是

復(fù)制代碼 代碼如下:

[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture]; 


這句話的意思時(shí),只有當(dāng)doubleTapGesture識(shí)別失敗的時(shí)候(即識(shí)別出這不是雙擊操作),singleTapGesture才能開(kāi)始識(shí)別,同我們一開(kāi)始講的是同一個(gè)問(wèn)題。


UIGestureRecognizer小應(yīng)用
1、輕拍手勢(shì):雙指、單擊,修改imageView的frame為(0,0,320,200)
2、長(zhǎng)按手指:?jiǎn)沃福薷膇mageView的alpha=0.5
3、實(shí)現(xiàn)平移、旋轉(zhuǎn)、捏合
4、輕掃:豎向輕掃實(shí)現(xiàn)圖:像隨機(jī)切換顯示;橫向輕掃實(shí)現(xiàn):圖像消失,隨機(jī)修改imageview的背景顏色
5、imageview每次只能添加一種手勢(shì)識(shí)別器。

復(fù)制代碼 代碼如下:

#define _originalRect CGRectMake(10, 50, 300, 450) 
#define _originalImageName  @"h4.jpeg" 
 
#import "HMTRootViewController.h" 
 
@interface HMTRootViewController (){ 
 
    UITapGestureRecognizer       * _tapGesture; 
    UILongPressGestureRecognizer * _longGesture; 
    UIPanGestureRecognizer       * _panGesture; 
    UIRotationGestureRecognizer  * _rotateGesture; 
    UIPinchGestureRecognizer     * _pinchGesture; 
    UISwipeGestureRecognizer     * _verticalSwipeGesture; 
    UISwipeGestureRecognizer     * _horizontanlSwipeGesture; 
    BOOL isTopDownOfRightLeft;    // 垂直滑動(dòng)是YES,水平滑動(dòng)是NO 
     

 
@property (nonatomic,retain) UIButton * button; 
@property (nonatomic,retain) UIImageView * imageView; 
 
@end 
 
@implementation HMTRootViewController 
 
- (void)dealloc{ 
     
    RELEASE_SAFELY(_imageView); 
    RELEASE_SAFELY(_button); 
    [super dealloc]; 
 

 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
        // Custom initialization 
        isTopDownOfRightLeft = YES; 
    } 
    return self; 

 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
     
    [self createButtonView]; 
    [self createImageView]; 
 

 
#pragma mark - 設(shè)置圖像 
- (void)createImageView{ 
     
    self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_originalImageName]]; 
    _imageView.frame = CGRectMake(10, 50, 300, 450); 
    _imageView.userInteractionEnabled = YES; 
    [self.view addSubview:_imageView]; 
    [_imageView release]; 

 
 
 
#pragma mark - 設(shè)置手勢(shì) 
 
#pragma mark  點(diǎn)擊手勢(shì) 
- (void)createTapGestureRecognizer{ 
    
 
    _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapGestureRecognizer:)]; 
    _tapGesture.numberOfTapsRequired = 1; 
    _tapGesture.numberOfTouchesRequired = 2; 
    [self.imageView addGestureRecognizer:_tapGesture]; 
    [_tapGesture release]; 
 

 
- (void)TapGestureRecognizer:(UITapGestureRecognizer *)tapGesture{ 
 
    self.imageView.frame = CGRectMake(0, 0, 320, 200); 
    NSLog(@"%@",NSStringFromCGRect(self.imageView.frame)); 
 
}  
 
#pragma mark  長(zhǎng)按手勢(shì) 
- (void)createLongGestureRecognizer{ 
     
    _longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGestureRecognizer:)]; 
    _longGesture.numberOfTouchesRequired = 1; 
    _longGesture.minimumPressDuration = 1.0; 
    [self.imageView addGestureRecognizer:_longGesture]; 
    [_longGesture release]; 
     

 
- (void)longGestureRecognizer:(UILongPressGestureRecognizer *)longGesture{ 
 
    self.imageView.alpha = 0.5; 
    NSLog(@"%s",__FUNCTION__); 
 

 
#pragma mark 平移拖拽手勢(shì) 
- (void)createPanGestureRecognizer{ 
     
    _panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)]; 
    [self.imageView addGestureRecognizer:_panGesture]; 
    [_panGesture release]; 
     

 
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
  
    CGPoint txty = [panGesture translationInView:self.view]; 
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, txty.x, txty.y); 
     
    [panGesture setTranslation:CGPointMake(0, 0) inView:self.view]; 
     

 
#pragma mark 旋轉(zhuǎn)手勢(shì) 
- (void)createRotationGestureRecognizer{ 
     
     
    _rotateGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureRecognizer:)]; 
    [self.imageView addGestureRecognizer:_rotateGesture]; 
    [_rotateGesture release]; 
     

 
- (void)rotationGestureRecognizer:(UIRotationGestureRecognizer *)rotateGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotateGesture.rotation); 
    rotateGesture.rotation = 0; 
     

 
#pragma mark 捏合縮放手勢(shì) 
- (void)createPinchGestureRecognizer{ 
     
    _pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureRecognizer:)]; 
    [self.imageView addGestureRecognizer:_pinchGesture]; 
    [_pinchGesture release]; 
     

 
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)pinchGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGesture.scale, pinchGesture.scale); 
    pinchGesture.scale = 1; 
     

 
#pragma mark - 輕掃手勢(shì) 
#pragma mark 上下 豎 垂直輕掃 
- (void)createVerticalSwipeGestureRecognizer{ 
    
    _verticalSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)]; 
    _verticalSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; 
    [self.imageView addGestureRecognizer:_verticalSwipeGesture]; 
    [_verticalSwipeGesture release]; 

 
#pragma mark 水平 左右輕掃 
- (void)createHorizontanlSwipeGesture{ 
     
    _horizontanlSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)]; 
    _horizontanlSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight; 
    [self.imageView addGestureRecognizer:_horizontanlSwipeGesture]; 
     

 
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGesture{ 
     
    NSLog(@"%s",__FUNCTION__); 
    
    if (swipeGesture.direction == (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown)) { 
        self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%i.jpeg",arc4random()%7+1]]; 
        ; 
 
    }else if (swipeGesture.direction == (UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)){ 
     
        self.imageView.image = nil; 
        self.imageView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]; 
    } 

 
 
#pragma mark - 設(shè)置按鈕 
- (void)createButtonView{ 
     
    NSArray * buttonArray = @[@"輕點(diǎn)",@"長(zhǎng)按",@"平移",@"旋轉(zhuǎn)",@"捏合",@"輕掃"]; 
     
    for (int i = 0; i < [buttonArray count]; i++) { 
         
        self.button = [UIButton buttonWithType:UIButtonTypeSystem]; 
        _button.frame = CGRectMake(10+50*i, 500, 50, 48); 
        [_button setTitle:[buttonArray objectAtIndex:i] forState:UIControlStateNormal]; 
        [_button addTarget:self action:@selector(onClikButton:) forControlEvents:UIControlEventTouchUpInside]; 
        _button.tag = i; 
        [self.view addSubview:_button]; 
    } 
   

 
- (void)onClikButton:(UIButton *)button{ 
     
    [self resetImageView]; 
    switch (button.tag) { 
        case 0: 
   
            [self createTapGestureRecognizer]; 
            break; 
        case 1: 
  
            [self createLongGestureRecognizer]; 
            break; 
        case 2: 
         
            [self createPanGestureRecognizer]; 
            break; 
        case 3: 
            
            [self createRotationGestureRecognizer]; 
            break; 
        case 4: 
             
            [self createPinchGestureRecognizer]; 
            break; 
        case 5: 
            if (isTopDownOfRightLeft == YES) { 
                [self createVerticalSwipeGestureRecognizer]; 
                isTopDownOfRightLeft = NO; 
            } else { 
                [self createHorizontanlSwipeGesture]; 
                isTopDownOfRightLeft = YES; 
            } 
            break; 
        default: 
            break; 
    } 
     

 
#pragma mark - 重置imageView 
- (void)resetImageView 

    for (int i = 0; i < [self.imageView.gestureRecognizers count]; i++) { 
        [self.imageView removeGestureRecognizer:[self.imageView.gestureRecognizers objectAtIndex:i]]; 
    } 
    self.imageView.alpha = 1.0; 
    self.imageView.transform = CGAffineTransformIdentity; 
    self.imageView.frame = _originalRect; 
    self.imageView.image = [UIImage imageNamed:_originalImageName]; 

 
 
- (void)didReceiveMemoryWarning 

    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 

 
@end 

相關(guān)文章

  • iOS程序性能優(yōu)化的技巧

    iOS程序性能優(yōu)化的技巧

    這篇文章主要介紹了一些優(yōu)化iOS程序性能的技巧,幫助大家更好的進(jìn)行ios開(kāi)發(fā),感興趣的朋友可以了解下
    2020-09-09
  • iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n))

    iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n))

    這篇文章主要介紹了iOS常用算法之兩個(gè)有序數(shù)組合并(要求時(shí)間復(fù)雜度為0(n)),實(shí)現(xiàn)思路是先將一個(gè)數(shù)組作為合并后的數(shù)組, 然后遍歷第二個(gè)數(shù)組的每項(xiàng)元素,需要的朋友可以參考下
    2019-07-07
  • iOS彈幕開(kāi)發(fā)中遇到的問(wèn)題匯總

    iOS彈幕開(kāi)發(fā)中遇到的問(wèn)題匯總

    最近做項(xiàng)目的時(shí)候需要實(shí)現(xiàn)彈幕這個(gè)功能, 雖然感覺(jué)實(shí)現(xiàn)起來(lái)也不是很復(fù)雜,但還是遇到了一些問(wèn)題,下面這篇文章主要給大家總結(jié)介紹了在iOS彈幕開(kāi)發(fā)中遇到的問(wèn)題,文中將解決的方法介紹的非常詳細(xì),需要的朋友可以參考下。
    2018-01-01
  • iOS?block的值捕獲與指針捕獲詳解

    iOS?block的值捕獲與指針捕獲詳解

    Block它是C語(yǔ)言級(jí)別和運(yùn)行時(shí)方面的一個(gè)特征,下面這篇文章主要給大家介紹了關(guān)于iOS?block的值捕獲與指針捕獲的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • swift 常用高階函數(shù)分享

    swift 常用高階函數(shù)分享

    Swift是一門(mén)面向協(xié)議的語(yǔ)言,在使用Swift時(shí)我們已經(jīng)充分享受到了面向協(xié)議編程帶給我們的便利,但是Swift相比Obj-C還有一個(gè)更重要的優(yōu)點(diǎn),那就是對(duì)函數(shù)式編程提供了很好的支持,其中Swift提供了map,filter,reduce這三個(gè)高階函數(shù)Higher Order function作為對(duì)容器的支持
    2017-12-12
  • iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫(kù)操作(使用FMDB)

    iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫(kù)操作(使用FMDB)

    這篇文章主要介紹了iOS學(xué)習(xí)筆記(十六)——詳解數(shù)據(jù)庫(kù)操作(使用FMDB),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • ios的簽名機(jī)制詳解

    ios的簽名機(jī)制詳解

    這篇文章主要介紹了ios的簽名機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用ios開(kāi)發(fā),感興趣的朋友可以了解下
    2021-04-04
  • 用iOS模擬器安裝App的方法

    用iOS模擬器安裝App的方法

    下面小編就為大家分享一篇用iOS模擬器安裝App的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • MAC 系統(tǒng)安裝java并配置環(huán)境變量

    MAC 系統(tǒng)安裝java并配置環(huán)境變量

    這篇文章主要介紹了MAC 系統(tǒng)安裝java并配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)

    iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)

    UILabel組件可以用來(lái)設(shè)置文字內(nèi)容的排版與字體效果等,功能非常多,下面就來(lái)為大家整理一下基本的iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
    2016-05-05

最新評(píng)論