我创建了一个具有调整大小,旋转和移动功能的UIView子类。我有一个UITextView作为子视图,它必须移动,旋转和调整父级。一切正常,直到我旋转视图,然后调整大小;子视图开始出现故障:https://gyazo.com/1341614679ada42311713cd53c1516ec(忽略旋转故障)
导致此问题的原因是什么?如何解决?
这是我使用调整大小和旋转的代码:
// Resizing
func scale(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.Changed {
let point = recognizer.locationInView(self)
var wChange = CGFloat(0.0), hChange = CGFloat(0.0)
wChange = point.x - self.prevPoint.x
let wRatioChange = wChange/bounds.size.width
hChange = wRatioChange * bounds.size.height
if abs(wChange) > 50.0 || abs(hChange) > 50.0 {
self.prevPoint = recognizer.locationOfTouch(0, inView: self)
return
}
bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width+wChange, bounds.size.height+hChange)
self.prevPoint = recognizer.locationOfTouch(0, inView: self)
update() // Updating subviews frames
}
func update() {
// resizing of other subviews
deleteButton.center.x = self.frame.width - deleteButton.frame.width * 0.5
deleteButton.frame.origin.y = 0
sizeButton.frame.origin.x = self.frame.width - sizeButton.frame.width
sizeButton.frame.origin.y = self.frame.height - sizeButton.frame.height
self.theTextView.frame.size = CGSize(width: self.frame.width - self.deleteButton.frame.width*2*0.66666666,height: self.frame.height - self.deleteButton.frame.height )
//vRotate.frame.origin.x = 0
//vRotate.frame.origin.y = self.frame.height - sizeButton.frame.height
self.theTextView.center = CGPoint(x: self.frame.width/2,y: self.frame.height/2)
updateTextFont(self.theTextView)
self.theTextView.textAlignment = NSTextAlignment.Center
...
}
旋转功能:
func rotate(recognizer: UIPanGestureRecognizer) {
if recognizer.state == .Changed {
let ang = atan2(recognizer.locationInView(self.superview).y - center.y, recognizer.locationInView(self.superview).x - center.x)
print(ang)
let angleDiff = self.deltaAngle - ang
self.transform = CGAffineTransformMakeRotation(-angleDiff)
}
}
提前致谢!
答案 0 :(得分:2)
好的我解决了,问题只是我在调整子视图时使用了frame
而不是bounds
。帧。例如:
sizeButton.frame.origin.y = self.frame.height - sizeButton.frame.height
应该是
sizeButton.frame.origin.y = self.bounds.height - sizeButton.frame.height