2016-02-27 3 views
1

У меня возникли проблемы с выравниванием UIView и CAShapeLayer.Устранение неполадок 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

 Смежные вопросы

  • Нет связанных вопросов^_^