我给self.view 加了一个view,给view加了一个UIPangestureRecognizer , move view,但是不流畅,感觉不流畅,
这是我的代码,谢谢你帮助我
var redView:UIView = UIView();
var redFrame:CGRect = CGRect();
override func viewDidLoad() {
super.viewDidLoad()
redView = UIView.init(frame: CGRect.init(x: 0, y: 100, width: KWidth, height: 40))
redView.backgroundColor = UIColor.red
self.redFrame = redView.frame;
self.view.addSubview(redView);
let pan:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(moveFunction(pan))
self.redView.addGestureRecognizer(pan)
}
func moveFunction(pan:UIPanGestureRecognizer) {
let point:CGPoint = pan.translation(in: self.view)
pan.setTranslation(CGPoint.zero, in: self.view)
if pan.state == .changed{
print(point)
let redY:CGFloat = redView.frame.origin.y + point.y
redView.frame = CGRect.init(x: 0, y: Int(redY), width: KWidth, height: 40)
}
}
Best Answer-推荐答案 strong>
可能此代码对您有所帮助。我正在使用平移手势更改 View (panView) 高度约束。
@IBOutlet weak var h: NSLayoutConstraint!
@IBOutlet weak var panView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let gesture = UIPanGestureRecognizer(target: self, action: #selector(self.pan(_))
panView.addGestureRecognizer(gesture)
}
@objc func pan(_ pan: UIPanGestureRecognizer) {
let translation = pan.translation(in: panView)
if pan.state == .began {
} else if pan.state == .changed {
h.constant = h.constant - translation.y
pan.setTranslation(CGPoint.zero, in: panView)
} else if pan.state == .ended {
// You can handle this how you want.
}
}
关于ios在 View 中添加panGesture,更改 View 框架,但不流畅,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/41479393/
|