欲計算邊緣的x位置(左上,右上,左下和右下),其具有目前一個CGAffineTransformation一個UIView的用rotationAngle激活。在圖片中,我想計算最右邊紅圈的x位置。
本文介紹如何在Obj-c中執行此操作,我相信:http://www.informit.com/articles/article.aspx?p=1951182。我試圖將其轉換爲swift 4,但我不得不做出很多更改。我來到這個:
func offsetPoint(toParentCoordinates aPoint: CGPoint) -> CGPoint {
return CGPoint(x: aPoint.x + self.cardView.center.x, y: aPoint.y + self.cardView.center.y)
}
func point(inViewCenterTerms aPoint: CGPoint) -> CGPoint {
return CGPoint(x: aPoint.x - self.cardView.center.x, y: aPoint.y - self.cardView.center.y)
}
func point(inTransformedView aPoint: CGPoint, someTransformation: CGAffineTransform) -> CGPoint {
let offsetItem: CGPoint = point(inViewCenterTerms: aPoint)
let updatedItem: CGPoint = offsetItem.applying(someTransformation)
let finalItem: CGPoint = offsetPoint(toParentCoordinates: updatedItem)
return finalItem
}
func originalFrame(someView: UIView, someTransformation: inout CGAffineTransform) -> CGRect {
let currentTransform: CGAffineTransform = someTransformation
someTransformation = CGAffineTransform.identity
let originalFrame: CGRect = someView.frame
someTransformation = currentTransform
return originalFrame
}
但我被困在其餘的。我在想,如果Swift 4中的事情變得更簡單,或者如何繼續其餘的代碼來使其工作。
您是否嘗試過使用簡單的座標計算:let x = r * sin(angle)並設y = r * cos(angle)? r是半徑,在你的情況下,它是對角線的一半長度。 – Woof