IOS添加panGesture到視圖,更改視圖幀,而不是英語流利程度
我視圖添加到self.view
,並添加UIPangestureRecognizer
到視圖,移動視圖,但不流暢,我能感覺到不流暢, 這是我的代碼,謝謝你幫我
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)
}
}
爲什麼每次都重新指定視圖的框架?只有'y'值,什麼在改變。 – dirtydanee