Ios 在布局子视图中旋转视图 背景和目标

Ios 在布局子视图中旋转视图 背景和目标,ios,swift,uiview,rotation,subclass,Ios,Swift,Uiview,Rotation,Subclass,目标:我想旋转并翻转UITextView。(原因:请参阅我的) 问题:如果我直接在UITextView上进行转换,文本布局会因为未知原因而变得混乱 解决方案:将UITextView放入UIView容器中,然后在容器上执行转换 新问题:旋转视图上的自动布局(或任何类型的布局)变为 建议的解决方案:创建UIView的子类,作为旋转和翻转的UIView的附加容器。然后,“自动布局”应可用于此自定义视图 当前问题 当所有内容第一次出现时(UITextView的背景为黄色): 但当方向发生变化时,会发

目标:我想旋转并翻转
UITextView
。(原因:请参阅我的)

问题:如果我直接在
UITextView
上进行转换,文本布局会因为未知原因而变得混乱

解决方案:
UITextView
放入
UIView
容器中,然后在容器上执行转换

新问题:旋转视图上的自动布局(或任何类型的布局)变为

建议的解决方案:创建
UIView
的子类,作为旋转和翻转的
UIView
的附加容器。然后,“自动布局”应可用于此自定义视图

当前问题 当所有内容第一次出现时(UITextView的背景为黄色):

但当方向发生变化时,会发生以下情况(蓝色是子类
UIView
background,在IB中设置):

如果禁用
rotationView.addSubview(textView)
行,则旋转容器视图(红色)可以很好地重新定位自身,即使在方向更改时:

所以问题一定是关于我在哪里添加
UITextView
。但是我该怎么做呢

代码 如果我不能让它工作。。。
虽然我现在遇到了一堵墙,但我的下一个计划是覆盖
drawRect()
来进行转换。不过,这不是我的第一选择,因为这样做可能会降低性能。

在调试模式下检查宽度和高度。我猜他们在旋转前后是一样的。这样,您的屏幕截图将是预期的

第二件事是应用两次转换

您有旋转视图->应用转换->更改手机上的帧旋转->上一个转换仍在应用->然后应用另一个转换。 也许你的答案就在这里

编辑:

可行的解决方案:

你可以检测你的女巫方位

 UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

 if(orientation == 0) //Default orientation 
   //UI is in Default (Portrait) -- this is really a just a failsafe. 
 else if(orientation == UIInterfaceOrientationPortrait)
   //Do something if the orientation is in Portrait
 else if(orientation == UIInterfaceOrientationLandscapeLeft)
   // Do something if Left
 else if(orientation == UIInterfaceOrientationLandscapeRight)
然后决定如何处理这件事

如果方向是纵向的,则比您所采取的

width = self.bound.size.width;
height = self.bound.size.height;
如果不是

height = self.bound.size.width;
width = self.bound.size.height;

问题中的代码的问题似乎是转换不断地相互添加。为了解决这个问题,解决方案是每次重置转换,也就是说,将其设置为标识转换

rotationView.transform = CGAffineTransformIdentity
下面是一个显示关键部分的部分实现

import UIKit
@IBDesignable class UIVerticalTextView: UIView {

    var textView = UITextView()
    let rotationView = UIView()

    var underlyingTextView: UITextView {
        get {
            return textView
        }
        set {
            textView = newValue
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect){
        super.init(frame: frame)
        self.setup()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setup()
    }

    func setup() {

        rotationView.backgroundColor = UIColor.redColor()
        textView.backgroundColor = UIColor.yellowColor()
        self.addSubview(rotationView)
        rotationView.addSubview(textView)

        // could also do this with auto layout constraints
        textView.frame = rotationView.bounds
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        rotationView.transform = CGAffineTransformIdentity // *** key line ***

        rotationView.frame = CGRect(origin: CGPointZero, size: CGSize(width: self.bounds.height, height: self.bounds.width))
        rotationView.transform = translateRotateFlip()
    }

    func translateRotateFlip() -> CGAffineTransform {

        var transform = CGAffineTransformIdentity

        // translate to new center
        transform = CGAffineTransformTranslate(transform, (self.bounds.width / 2)-(self.bounds.height / 2), (self.bounds.height / 2)-(self.bounds.width / 2))
        // rotate counterclockwise around center
        transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
        // flip vertically
        transform = CGAffineTransformScale(transform, -1, 1)

        return transform
    }
}

我的最新实施最有可能在中找到。

是。
self.bounds
宽度和
高度保持不变。关于两次应用转换的优点。不过,我真的不知道如何解决这个问题。
UITextView
的文本在应用旋转时是否需要更改?您可以只快照它并旋转快照吗?@Nick,在应用旋转时,文本不需要更改,但它需要保留
UITextView
的可滚动属性。在我看来,它似乎会导致它变得不可滚动。@MarkoZadravec,我认为宽度和高度总是需要在
self
和子视图(对于任何方向)之间交换,因为子视图从
self
旋转90度。旋转它们时,您确认宽度和高度相同,所以在一种情况下,宽度是实际宽度,但在纵向模式下,宽度是实际高度,高度相同。
import UIKit
@IBDesignable class UIVerticalTextView: UIView {

    var textView = UITextView()
    let rotationView = UIView()

    var underlyingTextView: UITextView {
        get {
            return textView
        }
        set {
            textView = newValue
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect){
        super.init(frame: frame)
        self.setup()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setup()
    }

    func setup() {

        rotationView.backgroundColor = UIColor.redColor()
        textView.backgroundColor = UIColor.yellowColor()
        self.addSubview(rotationView)
        rotationView.addSubview(textView)

        // could also do this with auto layout constraints
        textView.frame = rotationView.bounds
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        rotationView.transform = CGAffineTransformIdentity // *** key line ***

        rotationView.frame = CGRect(origin: CGPointZero, size: CGSize(width: self.bounds.height, height: self.bounds.width))
        rotationView.transform = translateRotateFlip()
    }

    func translateRotateFlip() -> CGAffineTransform {

        var transform = CGAffineTransformIdentity

        // translate to new center
        transform = CGAffineTransformTranslate(transform, (self.bounds.width / 2)-(self.bounds.height / 2), (self.bounds.height / 2)-(self.bounds.width / 2))
        // rotate counterclockwise around center
        transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
        // flip vertically
        transform = CGAffineTransformScale(transform, -1, 1)

        return transform
    }
}