手势识别器UIGestureRecognizer实现手机的各种手势操作功能

来源:互联网 发布:淘宝一直显示等待揽收 编辑:程序博客网 时间:2024/06/08 06:54

    手势识别器可以实现好多的手势功能,比如单击,双击,移动,旋转,滑动,长按等等。

这些功能都是通过创建相关类的对象,并调用相关的方法来实现的。单击,双击主要是通过UITapGestureRecognizer然后通过判断UITapGestureRecognizer的对象pDoubleTap.numberOfTapsRequired =2;来实现单击与双击的区分。

而移动主要是通过

UIPanGestureRecognizer来实现,然后通过判断视图的中心位置来确定视图平移后的位置。

长按是通过

UILongPressGestureRecognizer来实现的

旋转是通过

UIRotationGestureRecognizer来实现的

滑动是通过

UISwipeGestureRecognizer

其中滑动的方法有这么几种

UISwipeGestureRecognizerDirectionRight

UISwipeGestureRecognizerDirectionLeft

UISwipeGestureRecognizerDirectionUp

UISwipeGestureRecognizerDirectionDown

具体的代码如下:

HHLAppDelegate.h

#import <UIKit/UIKit.h>@class HHLViewController;@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HHLViewController *viewController;@end


HHLAppDelegate.m

#import "HHLAppDelegate.h"#import "HHLViewController.h"@implementation HHLAppDelegate- (void)dealloc{    [_window release];    [_viewController release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];    self.window.rootViewController = self.viewController;    [self.window makeKeyAndVisible];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end


HHLViewController.h

#import <UIKit/UIKit.h>@interface HHLViewController : UIViewController@property (retain,nonatomic) UIView *mView;@end


HHLViewController.m


#import "HHLViewController.h"@interface HHLViewController ()@end@implementation HHLViewController- (void)viewDidLoad{    [super viewDidLoad];    UITapGestureRecognizer *pSingleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];            //将手势添加 到手势识别器        [self.view addGestureRecognizer:pSingleTap];        UITapGestureRecognizer *pDoubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];    pDoubleTap.numberOfTapsRequired =2;    [self.view addGestureRecognizer:pDoubleTap];    [pDoubleTap requireGestureRecognizerToFail:pSingleTap];    [pSingleTap release];    [pDoubleTap release];        //清扫手势    UISwipeGestureRecognizer *pSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];    //设置滑动的方向    pSwip.direction = UISwipeGestureRecognizerDirectionLeft;    [self.view addGestureRecognizer:pSwip];    [pSwip release];        //平移    self.mView =[[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];    self.mView.backgroundColor = [UIColor yellowColor];     [self.view addSubview:self.mView];    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pangesture:)];    [self.view addGestureRecognizer:pan];       //长按手势    UILongPressGestureRecognizer *pLong = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressed:)];    [self.view addGestureRecognizer:pLong];        //旋转    UIRotationGestureRecognizer *protation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];    [self.view addGestureRecognizer:protation];    }#pragma mark --------- (void)singleGesture:(UITapGestureRecognizer *)tap{    NSLog(@"单击");}- (void)doubleGesture:(UITapGestureRecognizer *)tap{    NSLog(@"双击");}#pragma mark------- (void)swipGesture:(UITapGestureRecognizer *)swip{    NSLog(@"向左滑动");}#pragma mark-------------- (void)pangesture:(UIPanGestureRecognizer *)pan{    NSLog(@"平移");    CGPoint point = [pan locationInView:self.view];    //设置子视图的在视图中的坐标    self.mView.center = point;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)longPressed:(UILongPressGestureRecognizer *)pLong{    NSLog(@"长按");}#pragma mark ---------------- (void)rotationGesture:(UIRotationGestureRecognizer *)protation{    NSLog(@"旋转");    float degree = protation.rotation *(180/M_PI);    NSLog(@"旋转的角度为%2f",degree);}-(void)dealloc{    [_mView release];    [super dealloc];}@end

效果图如下:






0 0