Ios 应用旋转后如何调整视图的大小

Ios 应用旋转后如何调整视图的大小,ios,swift,uiview,rotation,transform,Ios,Swift,Uiview,Rotation,Transform,我尝试在应用旋转后从不同位置缩放(更改高度和宽度)视图 当视图未应用旋转时,代码可以正常工作,但一旦旋转,代码就无法正常工作 我正在使用RKUSERRESIZABLEVEW库来实现它 下面是代码的当前行为 以下是从特定位置缩放视图的代码 @objc func pan(_ recognizer : UIPanGestureRecognizer) { guard !self.isResizing() && !self.isRotate else { if s

我尝试在应用旋转后从不同位置缩放(更改高度和宽度)视图

当视图未应用旋转时,代码可以正常工作,但一旦旋转,代码就无法正常工作

我正在使用RKUSERRESIZABLEVEW库来实现它

下面是代码的当前行为

以下是从特定位置缩放视图的代码

@objc func pan(_ recognizer : UIPanGestureRecognizer) {
    guard !self.isResizing() && !self.isRotate else {
        if self.isResizing() && !isRotate {
            if let superView = self.superview {
                let location = recognizer.location(in: superView)
                self.resize(usingTouchLocation: location)
            }
        }else if isRotate{
            let location = recognizer.location(in: self.superview)
            self.handleRotation(fromPoint: location)
        }
        return
    }
    let translation = recognizer.translation(in: self.superview)
    self.center.x += translation.x
    self.center.y += translation.y
    recognizer.setTranslation(.zero, in: self)
}


func handleRotation(fromPoint touchLocation:CGPoint) {
    guard let delta = self.deltaRotationAngle else {
        return
    }
    let ang = atan2(touchLocation.y - center.y, touchLocation.x - center.x)
    let angleDiff = delta - ang
    self.lastRotation = -angleDiff
    self.transform = CGAffineTransform(rotationAngle: -angleDiff)
    self.layoutIfNeeded()

}

func resize(usingTouchLocation touchPoint: CGPoint) {
    
    // (2) Calculate the deltas using the current anchor point.
    var deltaW: CGFloat = anchorPoint.adjustsW * (touchStart.x - touchPoint.x)
    let deltaX: CGFloat = anchorPoint.adjustsX * (-1.0 * deltaW)
    var deltaH: CGFloat = anchorPoint.adjustsH * (touchPoint.y - touchStart.y)
    let deltaY: CGFloat = anchorPoint.adjustsY * (-1.0 * deltaH)
    
    debugPrint("New offset : \(CGRect.init(x: deltaX, y: deltaY, width: deltaW, height: deltaH))")
    
    // (3) Calculate the new frame.
    var newX: CGFloat = frame.origin.x// + deltaX
    var newY: CGFloat = frame.origin.y// + deltaY
    var newWidth: CGFloat = bounds.size.width + deltaW
    var newHeight: CGFloat = bounds.size.height + deltaH

    // (4) If the new frame is too small, cancel the changes.
    if newWidth < minWidth {
        newWidth = frame.size.width
        newX = frame.origin.x
    }
    if newHeight < minHeight {
        newHeight = frame.size.height
        newY = frame.origin.y
    }
    
    // (5) Ensure the resize won't cause the view to move offscreen.
    if isPreventsPositionOutsideSuperview {
        if let superView = self.superview {
            if newX < superView.bounds.origin.x {
                // Calculate how much to grow the width by such that the new X coordintae will align with the superview.
                deltaW = self.frame.origin.x - superView.bounds.origin.x
                newWidth = self.frame.size.width + deltaW
                newX = superView.bounds.origin.x
            }

            if newX + newWidth > superView.bounds.origin.x + superView.bounds.size.width {
                newWidth = superView.bounds.size.width - newX
            }

            if newY < superView.bounds.origin.y {
                // Calculate how much to grow the height by such that the new Y coordintae will align with the superview.
                deltaH = self.frame.origin.y - superView.bounds.origin.y
                newHeight = self.frame.size.height + deltaH
                newY = superView.bounds.origin.y
            }

            if newY + newHeight > superView.bounds.origin.y + superView.bounds.size.height {
                newHeight = superView.bounds.size.height - newY
            }
        }
    }
    
    self.setViewFrame(CGRect(x: newX, y: newY, width: newWidth, height: newHeight))
    touchStart = touchPoint
}
@objc func-pan(uu识别器:uipangestureerecognizer){
guard!self.isResising()&&!self.isRotate else{
如果self.isResising()&&!isRotate{
如果让superView=self.superView{
let location=recognizer.location(在:superView中)
self.resize(使用TouchLocation:location)
}
}如果是旋转的话{
let location=recognizer.location(在:self.superview中)
自手动旋转(起点:位置)
}
返回
}
let translation=recognizer.translation(在:self.superview中)
self.center.x+=translation.x
self.center.y+=translation.y
recognizer.setTranslation(.zero,in:self)
}
功能手柄旋转(起点接触位置:CGPoint){
防护罩let delta=self.delta旋转角度else{
返回
}
设ang=atan2(touchLocation.y-center.y,touchLocation.x-center.x)
设angleDiff=delta-ang
self.lastRotation=-angleDiff
self.transform=CGAffineTransform(旋转角度:-angleDiff)
self.layoutifneed()
}
func调整大小(使用TouchLocation接触点:CGPoint){
//(2)使用当前定位点计算三角洲。
变量deltaW:CGFloat=anchorPoint.adjustsW*(touchStart.x-touchPoint.x)
设deltaX:CGFloat=anchorPoint.adjustsX*(-1.0*deltaW)
var deltaH:CGFloat=anchorPoint.adjustsH*(touchPoint.y-touchStart.y)
let deltaY:CGFloat=anchorPoint.adjustsY*(-1.0*deltaH)
debugPrint(“新偏移量:\(CGRect.init(x:deltaX,y:deltaY,宽度:deltaW,高度:deltaH)))
//(3)计算新的框架。
var newX:CGFloat=frame.origin.x//+deltaX
var newY:CGFloat=frame.origin.y//+deltaY
var newWidth:CGFloat=bounds.size.width+deltaW
var newHeight:CGFloat=bounds.size.height+deltaH
//(4)如果新帧太小,请取消更改。
如果newWidthsuperView.bounds.origin.x+superView.bounds.size.width{
newWidth=superView.bounds.size.width-newX
}
如果newYsuperView.bounds.origin.y+superView.bounds.size.height{
newHeight=superView.bounds.size.height-newY
}
}
}
self.setViewFrame(CGRect(x:newX,y:newY,width:newWidth,height:newHeight))
touchStart=接触点
}