检测清扫

当用户触摸屏幕的时候,我们将第一次触摸的位置保存在变量中,当用户的手指移动着通过屏幕时,我们将会检查,看它是否到达某个点,这个点足够远且足够直,以便能够算作清扫

新建一个single view application

#import <UIKit/UIKit.h>


@interface>UIViewController

@property (retain, nonatomic) IBOutlet UILabel *label;

//声明一个容纳用户触摸的第一个点的变量

@property CGPoint gestureStartPoint;


@end

#import "liViewController.h"

//最小手势长度定义为25

#define kMinimumGestureLength 25

//偏差定义为5

#define kMaximumVariance 5


@interface liViewController ()


@end


@implementation liViewController


@synthesize label;


@synthesize gestureStartPoint;


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

self.label.text = nil;

}

//几秒后用这个函数擦掉文本内容

- (void)eraseText

{

    label.text = @"";

}

#pragma mark ---

//从touches集中获得并存储点

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

{

    UITouch *touch = [touches anyObject];

    gestureStartPoint = [touch locationInView:self.view];

}

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

{

    //获取用户手指的当前位置

    UITouch *touch = [touches anyObject];

    CGPoint currentPosition = [touch locationInView:self.view];

    //计算用户从起始位置起 在水平方向和垂直方向上移动的距离

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);

    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);

    

    if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance)

    {

        label.text = @"Horizontal swip detected";

        [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

    }else if(deltaY >= kMaximumVariance && deltaX <= kMinimumGestureLength)

    {

        label.text = @"Vertical swipe detected";

        [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

    }

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc {

    [label release];

    [super dealloc];

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值