moveDirection direction;//这个是个枚举
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//方向
UITouch *touch = [touches anyObject];
CGPoint translation = [touch locationInView:self.view];
self.translation = translation;
direction = kMoveDirectionNone;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint translation = [touch locationInView:self.view];
self.translationT = translation;
CGFloat upx = self .translation.x > self .translationT.x ? self .translation.x - self .translationT.x:self .translationT.x - self.translation.x;
CGFloat upy = self .translation.y > self .translationT.y ? self .translation.y - self .translationT.y:self .translationT.y - self.translation.y;
//上
if ((self .translation.y > self .translationT.y) && (upx < (self .translation.y - self.translationT.y))) {
direction = kMoveDirectionUp;
//下
}else if (self .translation.y < self .translationT.y && (upx < (self .translationT.y - self.translation.y))) {
direction = kMoveDirectionDown;
//左
}else if (self .translation.x > self .translationT.x && (upy < (self .translation.x - self.translationT.x))) {
direction = kMoveDirectionLeft;
//右
}else if (self .translation.x < self .translationT.x && (upy < (self .translationT.x - self.translation.x))) {
direction = kMoveDirectionRight;
}else{
direction = kMoveDirectionNone;
}
switch (direction) {
case kMoveDirectionDown:
[self compTime:6];
break ;
case kMoveDirectionUp:
[self compTime:12];
break ;
case kMoveDirectionRight:
[self compTime:3];
break ;
case kMoveDirectionLeft:
[self compTime:9];
break ;
case kMoveDirectionNone:
default :
break ;
}
}