ios手势学习笔记

来源:互联网 发布:代购app软件排行 编辑:程序博客网 时间:2024/05/21 16:56
废话不多说了,直接上代码,说明什么的都在注释中。。。
新建一个DemoGesture项目,将CoreGraphics.framework导入到项目中来。。。

1、.h文件中的代码如下

//
//  LMViewController.h
//  DemoGesture
//
//  Created by 路 apple on 13-9-12.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LMViewController : UIViewController {
    UILabel *myRotationLabel;
    UIButton *myButton;
}

@property (strong, nonatomic) IBOutlet UILabel *myRotationLabel;
@property (strong, nonatomic) IBOutlet UIButton *myButton;

//ios5 支持以下手势
//滑动手势
@property (nonatomic,strong) UISwipeGestureRecognizer * swipeGestureRecognizer;

//旋转手势
@property (nonatomic,strong) UIRotationGestureRecognizer * rotationGestureRecognizer;
@property (nonatomic,unsafe_unretained) CGFloat rotationAngleInRadians;

//pich 缩放手势
@property (nonatomic,strong) UIPinchGestureRecognizer * pichGestureRecognizer;

//pan 拖拽手势
@property (nonatomic,strong) UIPanGestureRecognizer * panGestureRecognizer;

//Long press长按手势
@property (nonatomic,strong) UILongPressGestureRecognizer * longpressGestureRecognizer;

//Tap点击手势
@property (nonatomic,strong) UITapGestureRecognizer * tapGestureRecognizer;


@property (nonatomic,assign) float currentsCale;
@end


2、.m中的代码如下:

//
//  LMViewController.m
//  DemoGesture
//
//  Created by 路 apple on 13-9-12.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "LMViewController.h"

@implementation LMViewController
@synthesize myRotationLabel;
@synthesize myButton;

@synthesize swipeGestureRecognizer;
@synthesize rotationGestureRecognizer,rotationAngleInRadians;
@synthesize pichGestureRecognizer;
@synthesize panGestureRecognizer;
@synthesize longpressGestureRecognizer;
@synthesize tapGestureRecognizer;
@synthesize currentsCale;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
   
    //1、滑动手势初始化部分
    self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    //滑动方向
    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;//向左滑动
//    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;//向右滑动
//    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;//向下滑动
//    self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;//向上滑动
    //如果以上四句全部写出来的话,就只能识别向上滑动的手势
    //滑动手指数
    self.swipeGestureRecognizer.numberOfTouchesRequired = 1;
    //要模拟两个手指一起滑动的话:
    //option+左键          两个手指方向相反
    //option+shift+左键    两个手指方向相同
   
    //将手势识别器添加到当前的view上面
    [self.view addGestureRecognizer:self.swipeGestureRecognizer];
   
   
    //2、旋转手势初始化部分
    self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotations:)];
    //将手势识别器添加到当前的view上面
    [self.view addGestureRecognizer:self.rotationGestureRecognizer];
   
   
    //3、pich手势初始化部分
    self.pichGestureRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePiches:)];
    [self.myRotationLabel addGestureRecognizer:pichGestureRecognizer];
    [self.view addGestureRecognizer:pichGestureRecognizer];
   
   
    //4、pan 拖拽手势。注意:如果既有swipe手势又有pan手势的话,不会捕获swipe手势了。
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePans:)];
    self.panGestureRecognizer.minimumNumberOfTouches = 1;
    self.panGestureRecognizer.maximumNumberOfTouches = 1;
    [self.view addGestureRecognizer:self.panGestureRecognizer];
    myRotationLabel.userInteractionEnabled = YES;
    myRotationLabel.exclusiveTouch = YES;
    [myRotationLabel addGestureRecognizer:panGestureRecognizer];
   
    //5、long press手势
    self.longpressGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpress:)];
    //手指数
    self.longpressGestureRecognizer.numberOfTouchesRequired = 2;
    self.longpressGestureRecognizer.allowableMovement = 100;
    self.longpressGestureRecognizer.minimumPressDuration = 1.0;
   
//    [self.myButton addGestureRecognizer:longpressGestureRecognizer];
    [self.view addGestureRecognizer:longpressGestureRecognizer];
   
   
    //6、tap手势
    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    self.tapGestureRecognizer.numberOfTapsRequired = 2;//监听双击手势
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

-(void)handleSwipes:(UISwipeGestureRecognizer *) recognizer
{
   
    if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft)
    {
        NSLog(@"往左滑动");
    }
    else if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"往右滑动");
    }
    else if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionDown)
    {
        NSLog(@"向下滑动");
    }
    else if(self.swipeGestureRecognizer.direction==UISwipeGestureRecognizerDirectionUp)
    {
        NSLog(@"向上滑动");
    }
}

-(void)handleRotations:(UIRotationGestureRecognizer*)recognizer
{
    NSLog(@"旋转");
    if(self.myRotationLabel==nil)
    {
        return;
    }
    //根据当前旋转的角度来旋转label
    self.myRotationLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians + recognizer.rotation);
    if(recognizer.state==UIGestureRecognizerStateEnded)
    {
        //每次旋转结束保存旋转角度
        self.rotationAngleInRadians += recognizer.rotation;
    }
    /*
     这个旋转手势识别器将会给我们传递一组旋转的角度,旋转手势识别器一直还在进行一个监听的动作,因 此他会一边监听我们手势旋转的角度,一边把这些捕获到的角度传递给我们,然后我们可以利用这些角度信 息,进行一些位置的计算,然后调整下一个显示的位置。
    
     在 iOS SDK 中所有以"CG"开头的都需要引入,CoreGraphice 框架包
    
    
     */
}

-(void)handlePiches:(UIPinchGestureRecognizer*)recognizer
{
    NSLog(@"pich");
    if(recognizer.state==UIGestureRecognizerStateEnded)
    {
        //聚拉结束后保存聚拉的比例
        self.currentsCale = recognizer.scale;
    }
    else if(recognizer.state==UIGestureRecognizerStateBegan&&self.currentsCale!=0.0f)
    {
        //再次开始聚拉的时候按照上一次保存的聚拉比例来聚拉,就相当于等比的缩小或者放大了
        recognizer.scale = self.currentsCale;
    }
    if(recognizer.scale!=NAN&&recognizer.scale!=0.0f)
    {
        //根据保存的聚拉比例聚拉
        recognizer.view.transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
    }
   
    /*
     一旦聚拉的手势动作产生了之后,我们就需要在捕获的事件中进行一个页面调整。其中有两个比较重要的 变量 scale 和 velocity,前者是一个比例范围,后者是一个变化速率的,也就是说每次变化的一个像素点。
     */
}

-(void)handlePans:(UIPanGestureRecognizer *)recognizer
{
    NSLog(@"pan");
    if(recognizer.state!=UIGestureRecognizerStateEnded&&recognizer.state!=UIGestureRecognizerStateFailed)
    {
        CGPoint location = [recognizer locationInView:recognizer.view.superview];
        recognizer.view.center = location;
//        注意要将拖拽手势识别器添加到label上才能移动label
    }
    /*
     通过使用 locationInView 这个方法,来获取到我们手势的坐标,为了能够捕获到多个手指的位置,我们就 需 要 使 用    locationOfTouch:inView: 这 个 方 法 。 然 后 通 过 使 用    UIPanGestureRecognizer    这 个 对 象 的 minimumNumberOfTouches 和 maximumNumberOfTouches 参数。你就可以在同一个时间来捕获多个手指的拖拽 的动作了。
    
     当手势产生的时候,并且处于 UIGestureRecognizerStateEnded 这个状态的时候,反馈出来的坐标 X 和 Y 肯能并 不是数字类型的,可能是 NAN 空。因此在这个状态的时候,我们一边建议不要采用这个状态反馈的坐标值。
     */
}

-(void)handleLongpress:(UILongPressGestureRecognizer *)recognizer
{
    NSLog(@"long press");
    if(recognizer.numberOfTapsRequired==2)
    {
        //第一个手指的位置
        CGPoint point1 = [recognizer locationOfTouch:0 inView:recognizer.view];
        //第二个手指的位置
        CGPoint point2 = [recognizer locationOfTouch:1 inView:recognizer.view];
        //取两个手指的中点位置
        CGFloat midPointX = (point1.x + point2.x)/2.0;
        CGFloat midPointy = (point1.y + point2.y)/2.0;
        CGPoint midPoint = CGPointMake(midPointX,midPointy);
        myButton.center = midPoint;
    }
}

-(void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"双击了");
}

- (void)viewDidUnload
{
    [self setMyRotationLabel:nil];
    [self setMyButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.swipeGestureRecognizer = nil;
    self.rotationGestureRecognizer = nil;
    self.pichGestureRecognizer = nil;
    self.panGestureRecognizer = nil;
    self.longpressGestureRecognizer = nil;
    self.tapGestureRecognizer = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
/*
 
 单独的手势识别器可以传递如下状态值。
 1. UIGestureRecognizerStatePossible
 2. UIGestureRecognizerStateRecognized
 3. UIGestureRecognizerStateFailed
 一组连贯的手势组可以传递如下的状态值。
 1. UIGestureRecognizerStatePossible
 2. UIGestureRecognizerStateBegan
 3. UIGestureRecognizerStateChanged
 4. UIGestureRecognizerStateEnded
 5. UIGestureRecognizerStateFailed
 
 
 rotationAngleInRadians
 这个对象就是我们在旋转的时候需要为我们的标签对象设置的一个位置对象信息,当我们每一次旋转的时 候我们都会把一个新的位置值保存在这个对象里面,达到一个旋转的效果。
 其实你也不用考虑太多,虽然标签的位置并不重要,我们还是让标签在他的周围进行一个旋转的,无论我 们的视图和标签在什么地方,唯一值得我们注意的就是,在不同的设备,由于屏幕尺寸(ipad,iphone)不一 样,所以我们都是需要来自动进行计算,计算下一次旋转的值达到旋转的效果。
 通过使用标签的 center 这个属性,然后设置一个中心位置值给我们的窗体界面,我们将会把我们的标签居 中显示,然后我们在旋转这个标签的时候也是依据这个中心位置进行一个旋转。
 */
@end


界面如下:
ios手势学习笔记 - 〇①世界 - 云端的小牛牛
0 0
原创粉丝点击