ios - UIPanGestureRecognizer 滑动UIView动画

标签 ios objective-c uiview uigesturerecognizer

我知道像这样的问题有很多答案,GitHub 上有很多库,但我只是想让它简单明了。我只想将菜单移出并使其跟随我的手指。该部分有效,但 View 最初并没有顺利滑出……它只是出现在我手指所在的位置。这是我拥有的:

//viewDidLoad
UIPanGestureRecognizer *panSettings = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[panSettings setDelegate:self];
[self.view addGestureRecognizer:panSettings];

UIView *container = [[UIView alloc]initWithFrame:CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)];
//...drop shadows and such...
[self.view addSubview:_container];

- (void)handlePan:(UIPanGestureRecognizer *)sender{
    CGPoint velocity = [sender velocityInView:self.view];
    CGPoint translation = [sender translationInView:self.view];
    if (velocity.x < 0 && translation.x < -50) {
        if (_container.frame.origin.x != 0) {
            panCoord = [sender locationInView:self.view];
            [UIView animateWithDuration:0.3 animations:^{
                _container.frame = CGRectMake(panCoord.x-100, 0, self.view.frame.size.width, self.view.frame.size.height);
            }];
        }
    }
}

处理平底锅:我想检查幻灯片的方向,检查多长时间并检查容器的位置。我想让它在向左滑动更远的负值然后-50 后平滑地滑出到手指位置……有什么建议吗?

最佳答案

我用过:

  UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightRevealToggle:)];
  sgr.direction = UISwipeGestureRecognizerDirectionRight;
  [self.view addGestureRecognizer:sgr];

检查一下,也许更容易

关于ios - UIPanGestureRecognizer 滑动UIView动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24737328/

相关文章:

ios - 基于 UIView block 的动画怪异行为

ios - 无法更改以编程方式生成的UIView中的标签

ios - Objective - C,如何裁剪 .caf 文件?

ios - 如果我使用来自 xib 的自定义颜色,为什么 iOS 不会覆盖背景颜色?

ios - UIDocumentInteractionController 注解属性使用

ios - Pan 手势识别和 UIKit Dynamics Gravity

ios - 如何捕获在 iOS 模拟器中抛出的 NSError 实例

ios - 在多页面 iOS 应用程序上将数据同步到 Realm 后端的最佳方式是什么

ios - Swift - 按下 BarButtonItem 时以编程方式创建和显示 UIPickerView

ios - 调整FontSizeToFitWidth 与SizeToFit