Sur*_*gch 5 subclass rotation uiview ios swift
目标:我想旋转和翻转UITextView.(为什么:看我之前的问题)
问题:如果我直接对其进行转换UITextView,文本布局会因某种未知原因而混乱.
解决方案:将其UITextView放入UIView容器中,然后对容器执行转换.
新问题:旋转视图上的自动布局(或任何类型的布局)成为一个主要问题.
建议的解决方案:创建一个子类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
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我目前在这里碰壁,但我的下一个计划是覆盖drawRect()以进行转换.不过,这不是我的第一选择,因为这样做可以减慢性能.
问题中的代码问题似乎是转换不断相互添加.为了解决这个问题,解决方案是每次都重置转换,即将其设置为身份转换.
rotationView.transform = CGAffineTransformIdentity
Run Code Online (Sandbox Code Playgroud)
这是一个显示关键部分的部分实现.
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
}
}
Run Code Online (Sandbox Code Playgroud)
我最近的实现最有可能在这个github链接中找到.
| 归档时间: |
|
| 查看次数: |
3458 次 |
| 最近记录: |