為View添加手勢 – iPhone手機開發技術文章 iPhone軟體開發教學課程

[cpp] 
01.#import <UIKit/UIKit.h>  
02. 
03.@interface TestGestureRecognizerViewController : UIViewController { 
04.    UIView *aView; 
05.} 
06.@property(nonatomic,retain)IBOutlet UIView *aView; 
07.@end 
08. 
09.///////////////////////////////////////////////////////////////////////////////////////////////  
10. 
11. 
12.#import "TestGestureRecognizerViewController.h"  
13.#import <QuartzCore/QuartzCore.h>//導入框架  
14. 
15.@implementation TestGestureRecognizerViewController 
16.@synthesize aView; 
17. 
18.-(void)handelPan:(UIPanGestureRecognizer*)gestureRecognizer{ 
19.    //獲取平移手勢對象在self.view的位置點,並將這個點作為self.aView的center,這樣就實現瞭拖動的效果  
20.    CGPoint curPoint = [gestureRecognizer locationInView:self.view]; 
21.    [self.aView setCenter:curPoint]; 
22.} 
23.-(void)handelTap:(UITapGestureRecognizer *)gestureRecognizer{ 
24.    NSLog(@"%s",__FUNCTION__); 
25.    [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];//雙擊事件取消延時  
26.} 
27.//單擊方法  
28.-(void)handelSingleTap:(UITapGestureRecognizer*)gestureRecognizer{ 
29.    NSLog(@"%s",__FUNCTION__); 
30.    [self performSelector:@selector(singleTap:) withObject:nil afterDelay:0.2]; 
31.} 
32.-(void)singleTap:(id)sender{ 
33.    NSLog(@"%s",__FUNCTION__); 
34.} 
35. 
36.- (void)viewDidLoad { 
37.    [super viewDidLoad]; 
38. 
39.    //創建一個平移手勢對象,該對象可以調用handelPan:方法  
40.    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)]; 
41.    [self.aView addGestureRecognizer:panGes]; 
42.    [panGes release]; 
43. 
44.    //創建一個點擊手勢對象,該對象可以調用handelTap:方法  
45.    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTap:)]; 
46.    [self.view addGestureRecognizer:tapGes]; 
47.    [tapGes release]; 
48.    [tapGes setNumberOfTouchesRequired:1];//觸摸點個數  
49.    [tapGes setNumberOfTapsRequired:2];//點擊次數  
50. 
51.    //創建一個點擊手勢對象,該對象可以調用handelSingleTap:方法  
52.    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelSingleTap:)]; 
53.    [self.view addGestureRecognizer:singleTap]; 
54.    [singleTap release]; 
55.    [singleTap setNumberOfTouchesRequired:1];//觸摸點個數  
56.    [singleTap setNumberOfTapsRequired:1];//點擊次數  
57.} 
58. 
59.- (void)dealloc { 
60.    [super dealloc]; 
61.} 
62. 
63.@end 
#import <UIKit/UIKit.h>

@interface TestGestureRecognizerViewController : UIViewController {
    UIView *aView;
}
@property(nonatomic,retain)IBOutlet UIView *aView;
@end

///////////////////////////////////////////////////////////////////////////////////////////////

#import "TestGestureRecognizerViewController.h"
#import <QuartzCore/QuartzCore.h>//導入框架

@implementation TestGestureRecognizerViewController
@synthesize aView;

-(void)handelPan:(UIPanGestureRecognizer*)gestureRecognizer{
    //獲取平移手勢對象在self.view的位置點,並將這個點作為self.aView的center,這樣就實現瞭拖動的效果
    CGPoint curPoint = [gestureRecognizer locationInView:self.view];
    [self.aView setCenter:curPoint];
}
-(void)handelTap:(UITapGestureRecognizer *)gestureRecognizer{
    NSLog(@"%s",__FUNCTION__);
    [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];//雙擊事件取消延時
}
//單擊方法
-(void)handelSingleTap:(UITapGestureRecognizer*)gestureRecognizer{
    NSLog(@"%s",__FUNCTION__);
    [self performSelector:@selector(singleTap:) withObject:nil afterDelay:0.2];
}
-(void)singleTap:(id)sender{
    NSLog(@"%s",__FUNCTION__);
}

– (void)viewDidLoad {
    [super viewDidLoad];

    //創建一個平移手勢對象,該對象可以調用handelPan:方法
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)];
    [self.aView addGestureRecognizer:panGes];
    [panGes release];

    //創建一個點擊手勢對象,該對象可以調用handelTap:方法
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTap:)];
    [self.view addGestureRecognizer:tapGes];
    [tapGes release];
    [tapGes setNumberOfTouchesRequired:1];//觸摸點個數
    [tapGes setNumberOfTapsRequired:2];//點擊次數

    //創建一個點擊手勢對象,該對象可以調用handelSingleTap:方法
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelSingleTap:)];
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];
    [singleTap setNumberOfTouchesRequired:1];//觸摸點個數
    [singleTap setNumberOfTapsRequired:1];//點擊次數
}

– (void)dealloc {
    [super dealloc];
}

@end

 

You May Also Like