gpt4 book ai didi

ios - 平移手势设置边界

转载 作者:行者123 更新时间:2023-11-30 11:17:15 25 4
gpt4 key购买 nike

我使用UIPanGesture,在目标方法中,监听state,我设置了可以移动的最大值和最小值;在 changed 状态下,当我检测到 View 的 view.origin.y 超过 myOrigin.y 时,view.origin .y = myOrigin.y。当缓慢拖动 View 时,似乎没有问题,但是当我快速拖动 View 时, View 会超出这个边界,然后立即变成我设置的边界,看起来像在闪烁。如果我设置动画 UIView.animation;它不会眨眼,但很明显我超出了界限并返回到这个 Action 。我想知道如何避免这种情况。不管拖动的速度有多快,它都会停在我设定的边界处,不会超过边界然后返回。至少看起来是这样的

```

  let point = pan.translation(in: view)
switch pan.state {
case .began:
break
case .changed:
view.frame.origin.y = view.frame.origin.y + point.y
if view.frame.origin.y > 200{
view.frame.origin.y = 200
}
pan.setTranslation(.zero, in: view)
default :
break
}

```

最佳答案

您可以通过约束来做到这一点 - 这更简单、更灵活。

此代码将创建一个 100 x 100 View Controller 中心的蓝色 View ,允许拖动它(通过 PanGesture ),并将其垂直移动限制为距 View 顶部 200 点(距底部 20 点,因此您可以不要将其拖离屏幕)。

关键是使用centerYAnchor优先级为 750 ( .defaultHigh ) 的约束,以及 >= topAnchor<= bottomAnchor限制垂直定位的约束。

//
// PanLimitViewController.swift
//
// Created by Don Mag on 8/1/18.
//

import UIKit

class PanLimitViewController: UIViewController {

// blue view for panning (dragging)
var panView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .blue
return v
}()

// the panView's center y constraint
var panCenterYConstraint: NSLayoutConstraint!

// var to track the current center y
var currentCenterYConstant: CGFloat = 0.0

// pan gesture recognizer
var panGesture = UIPanGestureRecognizer()

override func viewDidLoad() {
super.viewDidLoad()

// add the panView
view.addSubview(panView)

// constrain width to 100, height equal to width
panView.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
panView.heightAnchor.constraint(equalTo: panView.widthAnchor).isActive = true

// constrain to center horizontally
panView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

// initialize panView's center y constraint to center of view
panCenterYConstraint = NSLayoutConstraint(
item: panView,
attribute: .centerY,
relatedBy: .equal,
toItem: view,
attribute: .centerY,
multiplier: 1.0,
constant: 0.0)

// set panView's center y constraint priority to 750
// required to allow dragging
panCenterYConstraint.priority = .defaultHigh

// activate panView's center y constraint
panCenterYConstraint.isActive = true

// set panView's topAnchor to >= view's top + 200
// this will prevent dragging it higher than 200 pts from the top
panView.topAnchor.constraint(greaterThanOrEqualTo: view.topAnchor, constant: 200.0).isActive = true

// set panView's bottomAnchor to <= view's bottom
// this will prevent dragging it below the bottom
panView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -20.0).isActive = true

// create pan gesture and add to panView
panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureHandler(_:)))
panView.addGestureRecognizer(panGesture)

}

@objc func panGestureHandler(_ recognizer: UIPanGestureRecognizer){

switch recognizer.state {
case .began:
// save the current center y constant value
currentCenterYConstant = panCenterYConstraint.constant
break

case .changed:
// update panView's center y, based on drag
let translation = recognizer.translation(in: self.view)
panCenterYConstraint.constant = currentCenterYConstant + translation.y
break

case .ended, .cancelled:
// update panView's center y when drag is finished
panCenterYConstraint.constant = panView.center.y - view.center.y
break

default:
break
}

}

}

关于ios - 平移手势设置边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51630747/

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