gpt4 book ai didi

ios - 仅按标准距离垂直/水平滑动

转载 作者:行者123 更新时间:2023-11-29 03:08:24 24 4
gpt4 key购买 nike

我需要将网格板上的对象移动标准距离(例如水平或垂直 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);
}
}

我如何添加向上/向下手势并以流畅的手势实现滑动(在我的代码中,它会立即移动到新位置,如果我从屏幕上松开手指,我希望对象移动到新位置)?

最佳答案

UISwipeGestureRecognizerDirection enums

typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
} UISwipeGestureRecognizerDirection;

我相信您必须添加两个额外 swipeGestureRecognisers,其方向为 UISwipeGestureRecognizerDirectionUpUISwipeGestureRecognizerDirectionDown。那将解决您的问题。

例如,向上滑动

@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.
}
}

关于ios - 仅按标准距离垂直/水平滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com