微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

旋转后如何调整视图大小

如何解决旋转后如何调整视图大小

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

在没有将旋转应用于视图的情况下,代码可以正常工作,但是一旦旋转,则代码将无法正常工作。

我正在使用RKUserResizableView库。

这是代码的当前行为。

enter image description here

这是用于从特定位置缩放视图的代码

@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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。