Dravidian
Dravidian

Reputation: 9945

How to rotate a CAShapeLayer

I am trying to rotate a CAShapeLayer with respect to a particular anchor point. But when i apply firstLayer.transform = CATransform3DMakeRotation(CGFloat(M_2_PI), 0, 0, 0) nothing happens.

i am making a custom UIButton , in which i am adding a layer

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)
}



}

i want to make a line with a particular thickness and rotate it along a particular point (while animating). any help appreciated!

simulator Screenshot

Upvotes: 1

Views: 1603

Answers (1)

Sonny Saluja
Sonny Saluja

Reputation: 7287

Rotating by 2_PI (360) will mean layer will end up back at its original place (0 rotation). Try PI_2 (90) or PI (180) or another angle. Also you need to specify the axis of rotation.

This call rotates by 90 degrees around z-axis:

CATransform3DMakeRotation(CGFloat(M_PI_2), 0, 0, 1.0)

Upvotes: 1

Related Questions