在layoutSubviews中旋转视图 [英] Rotating a view in layoutSubviews

查看:95
本文介绍了在layoutSubviews中旋转视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我想旋转并翻转UITextView. (为什么:请参阅我的

Goal: I want to rotate and flip a UITextView. (Why: see my previous question)

问题::如果我直接在UITextView上进行转换,则由于某些未知原因,文本布局会混乱.

Problem: If I do the transform directly on the UITextView, the text layout gets messed up for some unknown reason.

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

Solution: Put the UITextView in a UIView container, and then do the transform on the container.

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

New problem: Auto Layout (or any sort of layout) on the rotated view becomes a major headache.

建议的解决方案:制作UIView的子类,该子类充当旋转和翻转的UIView的附加容器.然后,自动版式"应在此自定义视图上工作.

Proposed solution: Make a subclass of UIView that acts as an additional container for the rotated and flipped UIView. Auto Layout should then work on this custom view.

它在所有内容首次出现时起作用(UITextView具有黄色背景):

It works when everything first appears (UITextView has yellow background):

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

but when there is an orientation change, the following happens (blue is the subclassed UIView background, set in IB):

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

If I disable the rotationView.addSubview(textView) line, then the rotation container view (red) repositions itself just fine, even on an orientation change:

所以问题必须出在我添加UITextView的位置上.但是我该怎么办呢?

So the problem must be about where I am adding the UITextView. But how do I do it?

class MongolTextView: UIView {

    // properties
    var rotationView: UIView!
    var textView: UITextView!

    // This method gets called if you create the view in the Interface Builder
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // This method gets called if you create the view in code
    override init(frame: CGRect){
        super.init(frame: frame)
        self.setup()
    }

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

    func setup() {

        rotationView = UIView(frame: self.frame)
        rotationView.backgroundColor = UIColor.redColor()
        self.addSubview(rotationView)

        textView = UITextView(frame: CGRectZero)
        textView.backgroundColor = UIColor.yellowColor()
        textView.text = "This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text "

    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // set the size of the rotation container view
        let width = self.bounds.width
        let height = self.bounds.height
        rotationView.frame = CGRect(origin: CGPoint(x: CGFloat(0), y: CGFloat(0)), size: CGSize(width: height, height: width))

        textView.frame = rotationView.bounds // Problem lines here???
        rotationView.addSubview(textView)    // Problem lines here???

        // rotate, translate, and flip the container view
        var rotation = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
        // the following translation repositions the top left corner at the origin of the superview
        var translation = CGAffineTransformMakeTranslation((rotationView.bounds.height / 2)-(rotationView.bounds.width / 2), (rotationView.bounds.width / 2)-(rotationView.bounds.height / 2))
        var rotationAndTranslation = CGAffineTransformConcat(rotation, translation)
        var transformPlusScale = CGAffineTransformScale(rotationAndTranslation, CGFloat(-1), CGFloat(1))

        rotationView.transform = transformPlusScale

    }
}

如果我无法正常工作...

尽管我目前在这里碰壁,但我的下一个计划是重写drawRect()进行转换.不过,这不是我的首选,因为这样做可能会降低性能.

If I can't get this to work...

Although I have currently run up against a wall here, my next plan is to override drawRect() to do the transforms. This isn't my first choice, though, because performance is supposedly slowed down by doing this.

推荐答案

问题中的代码问题似乎在于,转换之间相互之间不断增加.为了解决此问题,解决方案是每次都重置转换,即将其设置为恒等转换.

The problem with the code in the question seems to be that the transformations keep getting added to each other. In order to fix this, the solution is to reset the transformations every time, that is, set it to the identity transform.

rotationView.transform = CGAffineTransformIdentity

这是显示关键部分的部分实现.

Here is a partial implementation that shows the key parts.

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

我的最新实现最有可能在

My most recent implementation is most likely to be found in this github link.

这篇关于在layoutSubviews中旋转视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆