iphone开发中的手势操作:Swipes

- (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 swipe detected";
[self performSelector:@selector(eraseText)
withObject:nil afterDelay:2];
} else if (deltaY >= kMinimumGestureLength &&
deltaX <= kMaximumVariance){
label.text = @"Vertical swipe detected";
[self performSelector:@selector(eraseText) withObject:nil
afterDelay:2];
}
}

亦可以使用Automatic Gesture Recognition :(UIGestureRecognizer)

- (void)viewDidLoad
{
[super viewDidLoad];

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(reportHorizontalSwipe:)];
horizontal.direction = UISwipeGestureRecognizerDirectionLeft|
UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:horizontal];
}

然后加上自定义的两个响应方法:

- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer {
label.text = @"Horizontal swipe detected";
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}

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

就OK啦!

以上的只是单个轻扫动作,下面的是多个轻扫动作同时进行的情况:

在viewDidLoad中添加循环:

- (void)viewDidLoad
{
[super viewDidLoad];

for (NSUInteger touchCount = 1; touchCount <= 5; touchCount++) {
UISwipeGestureRecognizer *vertical;
vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(reportVerticalSwipe:)];
vertical.direction = UISwipeGestureRecognizerDirectionUp|
UISwipeGestureRecognizerDirectionDown;
vertical.numberOfTouchesRequired = touchCount;
[self.view addGestureRecognizer:vertical];

UISwipeGestureRecognizer *horizontal;
horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(reportHorizontalSwipe:)];
horizontal.direction = UISwipeGestureRecognizerDirectionLeft|
UISwipeGestureRecognizerDirectionRight;
horizontal.numberOfTouchesRequired = touchCount;
[self.view addGestureRecognizer:horizontal];
}
}

修改对于响应动作函数:

- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer {
label.text = [NSString stringWithFormat:@"%@Horizontal swipe detected",
[self descriptionForTouchCount:[recognizer numberOfTouches]]];
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}

- (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer {
label.text = [NSString stringWithFormat:@"%@Vertical swipe detected",
[self descriptionForTouchCount:[recognizer numberOfTouches]]];;
[self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
}
- (NSString *)descriptionForTouchCount:(NSUInteger)touchCount {
switch (touchCount) {
case 2:
return @"Double ";
case 3:
return @"Triple ";
case 4:
return @"Quadruple ";
case 5:
return @"Quintuple ";
default:
return @"";
}
}





你可能感兴趣的:(iPhone开发)