【iPhone代码片段之四】手势识别器的用法 .

swipe对应手势识别器是UISwipeGestureRecognizer,示例代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    label.text=@"";
    //注册GestureRecognizer
    //注册上下手势
    UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(reportVerticalSwipe:)];
    vertical.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:vertical];
    
    //注册左右手势
    UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(reportHoriziontalSwipe:)];
    horizontal.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:horizontal];
}

-(void)reportHoriziontalSwipe:(UIGestureRecognizer *)recognizer{
    label.text = @"左右滑动";
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}

-(void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer{
    label.text = @"上下滑动";
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}

-(void)eraseText{
    label.text = @"";
}


Tap对应UITapGestureRecognizer

Pinch对应UIPinchGestureRecognizer

使用方法与上述示例大同小异。




你可能感兴趣的:(ios)