如何旋转CAShapeLayer

时间:2016-06-29 11:29:54

标签: ios swift2 xcode7

我正在尝试针对特定的锚点轮换CAShapeLayer。但是,当我申请 firstLayer.transform = CATransform3DMakeRotation(CGFloat(M_2_PI), 0, 0, 0) 没有任何反应。

我正在制作自定义UIButton,其中我正在添加图层

import UIKit

@IBDesignable

class CustomButtonTwo: UIButton {
var context = UIGraphicsGetCurrentContext()
@IBInspectable var Thickness : CGFloat = 2

let firstLayer = CAShapeLayer()
var width = CGFloat()
var height = CGFloat()

override func awakeFromNib() {
    super.awakeFromNib()
    width = self.frame.width
    height = self.frame.height
    print("\(width) : \(height)")
}
override func drawRect(rect: CGRect) {
    super.drawRect(rect)
    let afirstStartPoint = CGPointMake(width * 0.1, (height - 3 * Thickness) / 6)
    let bfirstStartPoint = CGPointMake(width * 0.1, (height - 3 * Thickness) / 6 + Thickness)
    let afirstMiddlePoint = CGPointMake(width * 0.5, (height - 3 * Thickness) / 6 )
    let bfirstMiddlePoint = CGPointMake(width * 0.5, (height - 3 * Thickness) / 6 + Thickness)
    print(afirstMiddlePoint)
    print(afirstStartPoint)
    print(bfirstMiddlePoint)
    print(bfirstStartPoint)
    let firstPath = UIBezierPath()
    firstPath.moveToPoint(afirstStartPoint)
    firstPath.addLineToPoint(afirstMiddlePoint)
    firstPath.addLineToPoint(bfirstMiddlePoint)
    firstPath.addLineToPoint(bfirstStartPoint)
    firstPath.addLineToPoint(afirstStartPoint)
    firstPath.closePath()
    firstLayer.frame = self.frame
    UIColor.greenColor().setFill()
    firstPath.fill()

    firstLayer.path = firstPath.CGPath
    firstLayer.anchorPoint = afirstStartPoint
    firstLayer.transform = CATransform3DMakeRotation(CGFloat(M_2_PI), 0, 0, 1)
     layer.addSublayer(firstLayer)
}



}

我想制作一个具有特定厚度的线条并沿特定点旋转(同时制作动画)。 任何帮助表示赞赏!

simulator Screenshot

1 个答案:

答案 0 :(得分:1)

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{ NSLog(@"Script: %@",[wkWebView.configuration.userContentController.userScripts firstObject].source); } 旋转将意味着图层将最终返回其原始位置(0旋转)。尝试2_PI (360)PI_2 (90)或其他角度。您还需要指定旋转轴。

此调用围绕z轴旋转90度:

PI (180)