gpt4 book ai didi

ios - 在 UIView 之间滑动手势

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:13 26 4
gpt4 key购买 nike

我有一个 ViewController 类,其中我在 self.view 上有一个名为 templateView 的 UIView,它由一个名为 gridView 的 UIView 组成,这里我需要在 templateView,为此我添加了滑动手势,例如,

swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction)];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction)];

swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self;

swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self;

[templateView addGestureRecognizer:swipeRight];
[templateView addGestureRecognizer:swipeLeft];

swipeRightswipeLeft 中,我需要移动 gridView 左侧和右侧。我需要在这些方法中实现什么..?

最佳答案

我建议

  1. 使用带参数的手势处理程序(以防您向多个 View 添加手势);

  2. 确保相关 View 已打开 userInteractionEnabled

  3. 您不需要设置手势的 delegate 除非您要实现 UIGestureRecognizerDelegate 之一方法。

因此,配置可能如下所示:

templateView.userInteractionEnabled = YES;

swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[templateView addGestureRecognizer:swipeRight];

swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[templateView addGestureRecognizer:swipeLeft];

然后手势处理程序可能看起来像:

- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
CGRect frame = self.gridView.frame;

// I don't know how far you want to move the grid view.
// This moves it off screen.
// Adjust this to move it the appropriate amount for your desired UI

if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
frame.origin.x += self.view.bounds.size.width;
else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
frame.origin.x -= self.view.bounds.size.width;
else
NSLog(@"Unrecognized swipe direction");

// Now animate the changing of the frame

[UIView animateWithDuration:0.5
animations:^{
self.gridView.frame = frame;
}];
}

请注意,如果您正在使用自动布局并且 View 是由约束而不是 translatesAutoresizingMaskIntoConstraints 定义的,则此处理程序代码必须适当更改。但希望这能为您提供基本概念。

关于ios - 在 UIView 之间滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16497607/

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