[英]Swift: rotate oblique a button in iOS8?
假設我的ViewController上有一個或多個100 * 50矩形按鈕。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func myButton(sender: UIButton) {
}
}
通常我將圖形圖像作為它們的圖像。 然后我應用自動布局。
但! 我需要它們傾斜而不是水平,即如果您有正方形,則需要將其旋轉為菱形(45°),這是相同的。 我知道不可能通過界面生成器。 如何通過代碼實現?
也許我應該使用CGAffineTransformRotate或RotateTranform,但我真的不知道如何開始
最好的方法是旋轉視圖圖層,CGAffineTransformMakeTranslation指示圖層將旋轉的圖層中的點,記住圖層從(0,0)到(1.1),所以中間是(0.5,0.5):
var transform = CGAffineTransformMakeTranslation(0.5, 0.5)
var angle = CGFloat(M_PI / 2.0) //90 degress rotation
transform = CGAffineTransformRotate(transform, angle)
button.transform = transform
button.layer.rasterizationScale = UIScreen.mainScreen().scale
button.layer.shouldRasterize = true
button.setNeedsDisplay()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.