IOS开发之手势

   在IOS系统3.2过后apple公司提供了一个抽象类,UIGestureRecognizer类用于手势识别,它的子类有主要有六个分别是:

UITapGestureRecognizer(轻击一下)

UIPinchGestureRecognizer(两指控制的缩放)

UIRotationGestureRecognizer(旋转)

UISwipeGestureRecognizer(滑动,快速移动)

UIPanGestureRecognizer(拖移,慢慢移动)

UILongPressGestureRecognizer(长按)

继承关系如下:

手势的使用

1.创建手势的实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。

2.将实例添加到需要识别的view中,每个手势只对应一个view,但每个view可对应多个手势,当手势发生在view  边界内时,回调方法。

在虚拟机上调试时有可能会不灵敏,因此尽量在真机上运行,尤其是多指手势。

******************************************可耻的分割线********************************************************

UITapGestureRecognizer使用

UITapGestureRecognizer *tap = [

[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(huidiaohanshu:)];

[self.view addGestureRecognizer:tap];

以上内容添加到viewcontroller里面的viewDidLoad函数中,然后再定义huidiaohanshu参数就是tap,

如下

-(void)huidiaohanshu:(UITapGestureRecognizer *)tap

      //使用手势你想要达到的效果

******************************************可耻的分割线********************************************************

UIPinchGestureRecognizer使用

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(huidiaohanshu:)];

[self.view addGestureRecognizer:pinch];

以上内容添加到viewcontroller里面的viewDidLoad函数中,然后再定义huidiaohanshu参数就是pinch,

如下

-(void)huidiaohanshu:(UIPinchGestureRecognizer *)

scale = pinchGestureRecognizer.scale;

[self transformImageView];

if (pinchGestureRecognizer.state == UIGestureRecognizerStated)

{

previousScale == scale*previousScale;

scale = 1;

}

}

其中scale和previousScale是预先定义好的实例变量,scale代表当前的缩放比例,previousScale代表先前的缩放比例。

******************************************可耻的分割线********************************************************

UIRotationGestureRecognizer使用

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(huidiaohanshu:)];

[self.view addGestureRecognizer:rotation];

以上内容添加到viewcontroller里面的viewDidLoad函数中,然后再定义huidiaohanshu参数就是rotation,

如下

-(void)huidiaohanshu:(UIRotationGestureRecognizer *)rotation

rotation = rotationGestureRecognizer.rotation;

[self transformImageView];

if(rotationGestureRecognizer.state == UIGestureRecognizerStateEnded)

{

previousrotation = rotation+prevoiusrotation;

rotation = 0;

}

}

其中rotation和previousrotation是预先定义好的实例变量,rotation代表当前的缩放比例,previousrotation代表先前的缩放比例。

******************************************可耻的分割线********************************************************

UISwipeGestureRecognizer的使用

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(huidiaohanshu:)];

swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;//如果需要横向的就用Left | Right

[self.view addGestureRecognizer:swipe];

以上内容添加到viewcontroller里面的viewDidLoad函数中,然后再定义huidiaohanshu参数就是swipe如下

-(void)huidiaohanshu:(UISwipeGestureRecognizer *)swipe

{

//使用手势所要达到的效果

}

******************************************可耻的分割线********************************************************

UIPanGestureRecognizer的使用

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(huidiaohanshu:)];

[self.view addGestureRecognizer:pan];

以上内容添加到viewcontroller里面的viewDidLoad函数中,然后再定义huidiaohanshu参数就是pan,

如下

-(void)huidiaohanshu:(UIPanGestureRecognizer *)panGestureRecognize

{

//使用手势所要达到的效果
    CGPoint translation = [panGestureRecognize translationInView:self.view];
   panGestureRecognize.view.center = CGPointMake(panGestureRecognize.view.center.x + translation.x,
                                       panGestureRecognize.view.center.y + translation.y);
    [panGestureRecognize setTranslation:CGPointZero inView:self.view];
  
    if (panGestureRecognize.state == UIGestureRecognizerStateEnded) {
      
        CGPoint velocity = [panGestureRecognize velocityInView:self.view];
        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
        CGFloat slideMult = magnitude / 200;
        NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
      
        float slideFactor = 0.1 * slideMult; // Increase for more of a slide
        CGPoint finalPoint = CGPointMake(panGestureRecognize.view.center.x + (velocity.x * slideFactor),
                                         panGestureRecognize.view.center.y + (velocity.y * slideFactor));
        finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
        finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
      
        [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            panGestureRecognize.view.center = finalPoint;
        } completion:nil];

}

以上代码解析:

  1. 计算速度向量的长度(估计大部分都忘了)这些知识了。
  2. 如果速度向量小于200,那就会得到一个小于的小数,那么滑行会很短
  3. 基于速度和速度因素计算一个终点
  4. 确保终点不会跑出父View的边界
  5. 使用UIView动画使view滑动到终点

******************************************可耻的分割线********************************************************

UILongPressGestureRecognizer的使用

UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(huidiaohanshu:)];

longpress.minimumPressDuration = 1.0;

[self.view addGestureRecognizer:longpress];

以上内容添加到viewcontroller里面的viewDidLoad函数中,然后再定义huidiaohanshu参数就是longpress如下

-(void)huidiaohanshu:(UILongPressGestureRecognizer  *)longpress

{

if(longpress.state == UIGestureRecognizerStateBegan)

{

CGPoint point = [longpress locationInView:self.tableView];

if(indexPath == nil)

return;

//使用手势所要达到的效果

}

}

******************************************可耻的分割线********************************************************

自定义手势

继承UIGestureRecognizer,并实现以下方法

-touchesBegan:withEvent:

-touchesMoved:withEvent:

-touchesEnded:withEvent:

-touchesCancelled:withEvent:

具体如下:

新建一个类,继承UIGestureRecognizer,代码如下:

.h文件

************************************************************

#import
typedef enum {
    DirectionUnknown = 0,
    DirectionLeft,
    DirectionRight
} Direction;

@interface HappyGestureRecognizer : UIGestureRecognizer
@property (assign) int tickleCount;
@property (assign) CGPoint curTickleStart;
@property (assign) Direction lastDirection;

@end

************************************************************************

.m文件

**************************************************************************************

#import "HappyGestureRecognizer.h"
#import
#define REQUIRED_TICKLES        2
#define MOVE_AMT_PER_TICKLE     25

@implementation HappyGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    self.curTickleStart = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  
    // Make sure we've moved a minimum amount since curTickleStart
    UITouch * touch = [touches anyObject];
    CGPoint ticklePoint = [touch locationInView:self.view];
    CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
    Direction curDirection;
    if (moveAmt < 0) {
        curDirection = DirectionLeft;
    } else {
        curDirection = DirectionRight;
    }
    if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
  
    // 确认方向改变了
    if (self.lastDirection == DirectionUnknown ||
        (self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
        (self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
      
        // 挠痒次数
        self.tickleCount++;
        self.curTickleStart = ticklePoint;
        self.lastDirection = curDirection;
      
        // 一旦挠痒次数超过指定数,设置手势为结束状态
        // 这样回调函数会被调用。
        if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
            [self setState:UIGestureRecognizerStateEnded];
        }
    }
  
}

- (void)reset {
    self.tickleCount = 0;
    self.curTickleStart = CGPointZero;
    self.lastDirection = DirectionUnknown;
    if (self.state == UIGestureRecognizerStatePossible) {
        [self setState:UIGestureRecognizerStateFailed];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self reset];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self reset];
}

@end

**************************************************************************************

调用自定义手势和以前一样

HappyGestureRecognizer *happy = [[HappyGestureRecognizer alloc]initWithTarget:self action:@selector(handleHappy:)];

[self.view addGestureRecognizer:happy];

回调函数

-(void)handleHappy:(HappyGestureRecognizer *)happy

{

//所要达到的效果

}

资料来源:http://blog.youkuaiyun.com/totogo2010/article/details/8615940

              http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html

             http://mobile.51cto.com/iphone-403791.htm

              精通IOS开发(第五版)David Mark         Jack Nutting 等著

 

《C++编程实例100篇》是一本深入实践、极具价值的编程教程,它针对C++编程语言提供了丰富的实例,旨在帮助读者更好地理解和掌握C++的各项特性与编程技巧。这本书的经典之处在于它将理论与实践相结合,通过100个精心设计的编程实例,覆盖了C++的各个核心领域,包括基础语法、面向对象编程、模板、异常处理、STL(标准模板库)等。 我们来探讨C++的基础语法。C++是C语言的增强版,它保留了C语言的高效性和灵活性,并引入了类、对象和继承等面向对象编程概念。基础语法包括变量声明、数据类型、运算符、控制结构(如if语句、for循环、while循环)、函数的定义和调用等。在实例中,你可能会遇到如何编写简单的程序,如计算两个数的和,或者实现一个简单的猜数字游戏。 C++的面向对象编程是其一大特色。通过类和对象,你可以构建复杂的软件系统。类是对象的蓝图,它定义了对象的属性和行为。实例化一个类,就是创建一个具体的对象。继承允许你创建新的类,这些类从现有的类派生,共享其属性和方法,同时可以添加新的功能。多态性是面向对象的另一个关键特性,它使得不同类型的对象可以对同一消息作出不同的响应。这些概念在实例中会以各种形式展现,例如设计一个图形界面的类层次,或实现一个简单的模拟游戏。 接下来是模板,C++的模板功能让代码更加通用,可以处理不同类型的数据。模板分为函数模板和类模板,前者可以创建泛型函数,后者可以创建泛型类。通过模板,你可以编写出高效且灵活的代码,比如实现一个通用的排序算法。 异常处理是C++中用于处理程序运行时错误的机制。当程序出现异常情况时,可以抛出一个异常,然后在适当的点捕获并处理这个异常。这使得代码能够优雅地处理错误,而不是让程序崩溃。实例中可能会有涉及文件操作或网络通信时可能出现的异常处理示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值