2016-02-27 22 views
1

我在對齊UIViewCAShapeLayer時遇到問題。對齊UIView和CAShapeLayer的問題

下面是示例代碼演示該問題:

import UIKit 

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100)) 
     square.transform = CGAffineTransformMakeRotation(CGFloat(20*M_PI/180)) 
     square.backgroundColor = UIColor.grayColor() 
     view.addSubview(square) 

     let strokeLayer = CAShapeLayer() 
     strokeLayer.path = UIBezierPath(rect: square.bounds).CGPath 
     strokeLayer.position = square.center 
     strokeLayer.setAffineTransform(square.transform) 

     strokeLayer.fillColor = UIColor.clearColor().CGColor 
     strokeLayer.strokeColor = UIColor.redColor().CGColor 
     strokeLayer.lineWidth = 1 

     view.addSubview(square) 
     view.layer.addSublayer(strokeLayer) 
    } 
} 

下面是結果的圖像:

Result of code in iOS Simulator

如何獲得兩個對齊?

回答

2

你需要做的只是改變你的形狀層是什麼補充:

strokeLayer.frame = square.layer.bounds 

右後:

let strokeLayer = CAShapeLayer() 

即,

let strokeLayer = CAShapeLayer() 
strokeLayer.frame = square.layer.bounds 
strokeLayer.path = UIBezierPath(rect: square.bounds).CGPath 
strokeLayer.position = square.center 
strokeLayer.setAffineTransform(square.transform) 

strokeLayer.fillColor = UIColor.clearColor().CGColor 
strokeLayer.strokeColor = UIColor.redColor().CGColor 
strokeLayer.lineWidth = 1 
0

如果位置(0,0)

strokeLayer.position = CGPointMake(0, 0) 
+0

沒有。看起來像這樣:http://imgur.com/YvHmZr8 – Trappar

+0

也從您的形狀圖層中刪除變換 – nielsbot

+0

http://imgur.com/qh8xrWH – Trappar