我需要按照标准距离(例如水平或垂直1个单位)移动网格板上的对象,具体取决于我是水平还是垂直滑动一个手指。我提到的距离是固定的,方向取决于我是从下到上,从左到右,反之亦然。
它将更像是这个游戏: https://www.youtube.com/watch?v=SuD_Ripl-MU
我制作此代码但它只检测左/右:
@interface ViewController()
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
@end
@implementation ViewController
- (IBAction)HandlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0,0) inView:self.view];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
}
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
CGPoint labelPosition = CGPointMake(self.pesawat.frame.origin.x - 30.0, self.pesawat.frame.origin.y);
self.pesawat.frame = CGRectMake( labelPosition.x , labelPosition.y , self.pesawat.frame.size.width, self.pesawat.frame.size.height);
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
CGPoint labelPosition = CGPointMake(self.pesawat.frame.origin.x + 30.0, self.pesawat.frame.origin.y);
self.pesawat.frame = CGRectMake( labelPosition.x , labelPosition.y , self.pesawat.frame.size.width, self.pesawat.frame.size.height);
}
}
如何向上/向下添加手势并以平滑的手势实现滑动(在我的代码上,它会立即移动到新位置,如果我从屏幕上释放手指,我希望对象移动到新位置)?
答案 0 :(得分:1)
UISwipeGestureRecognizerDirection enums
typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
} UISwipeGestureRecognizerDirection;
我相信您必须添加两个额外的 swipeGestureRecognisers
,其中包含路线UISwipeGestureRecognizerDirectionUp
和UISwipeGestureRecognizerDirectionDown
。那样可以解决你的问题。
例如,用于向上滑动
@property (nonatomic, strong) UISwipeGestureRecognizer *upSwipeGestureRecognizer;
self.upSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.upSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
.....
if (sender.direction == UISwipeGestureRecognizerDirectionUp)
{
//add your code here.
}
}
答案 1 :(得分:0)
UIPanGestureRecognizer足以完成此任务。只需检查UIPanGesture的statebegan和stateEnd属性的转换差异,并根据velocity参数转换视图。 喜欢 -
if(velocity > 0) {
//your down/right movement
}
else {
//your left or up movement
}