目标:我想旋转并翻转UITextVIEw. (为什么:看我的previous question)
问题:如果我直接在UITextVIEw上进行转换,文本布局会因某种未知原因而混乱.
解决方案:将UITextVIEw放在UIVIEw容器中,然后对容器进行转换.
新问题:旋转视图上的自动布局(或任何类型的布局)变为a major headache.
建议的解决方案:创建一个UIVIEw的子类,作为旋转和翻转的UIVIEw的附加容器.然后,自动布局应该在此自定义视图上工作.
当前的问题
它首先出现时(UITextVIEw有黄色背景):
但是当有方向改变时,会发生以下情况(蓝色是子类化的UIVIEw背景,在IB中设置):
如果我禁用了rotationVIEw.addSubvIEw(textVIEw)行,那么旋转容器视图(红色)会自行重新定位,即使在方向更改时也是如此:
所以问题必须在于我添加UITextVIEw的位置.但是我该怎么办呢?
码
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()来进行转换.不过,这不是我的第一选择,因为这样做可以减慢性能.
解决方法 问题中的代码问题似乎是转换不断相互添加.为了解决这个问题,解决方案是每次都重置转换,即将其设置为身份转换.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 }}
我最近的实施最有可能在this github link中找到.
总结以上是内存溢出为你收集整理的ios – 在layoutSubviews中旋转视图全部内容,希望文章能够帮你解决ios – 在layoutSubviews中旋转视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)