检测清扫

来源:互联网 发布:树熊网络大庆 编辑:程序博客网 时间:2024/04/30 09:51

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

新建一个single view application

#import <UIKit/UIKit.h>


@interface>UIViewController

@property (retain,nonatomic) IBOutletUILabel *label;

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

@propertyCGPoint gestureStartPoint;


@end

#import "liViewController.h"

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

#define kMinimumGestureLength 25

//偏差定义为5

#define kMaximumVariance 5


@interface liViewController ()


@end


@implementation liViewController


@synthesize label;


@synthesize gestureStartPoint;


- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

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";

        [selfperformSelector:@selector(eraseText)withObject:nilafterDelay:2];

    }elseif(deltaY >=kMaximumVariance && deltaX <= kMinimumGestureLength)

    {

        label.text =@"Vertical swipe detected";

        [selfperformSelector:@selector(eraseText)withObject:nilafterDelay:2];

    }

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc {

    [labelrelease];

    [superdealloc];

}

@end


0 0