我尝试将插入的子视图从按下的网格编号(编号1)设置为顶行(位置B - 参见图像)。
我设法移动了UIView的X Y但是我似乎无法在不使用CGPoints / CGFloats的情况下找到如何将它从A移动到B.
我已尝试将CGFlaots分配给positionAViewbounds.origin.x& positionAViewbounds.origin.y但它只相对于superview而不是整个窗口视图移动
到目前为止,这是我的代码:
UIView.animateWithDuration(
0.350, delay: 0, usingSpringWithDamping: 0.850, initialSpringVelocity: 10.0, options: [], animations: {
self.yellowSquare.transform = CGAffineTransformMakeTranslation(50, -50)
}, completion:nil)
更新:
所以我设法通过使用' convertPoint' 我唯一能解决的问题是它只在我第二次按UIButton时才有效。
首先按下坐标是:
黄色坐标= -16.5
黄色坐标= 288.0
并在第二次按下时,坐标为:
黄色坐标= 24.0
黄色坐标= 18.0
到目前为止,这是我的代码:
// Inserting Yellow UIView
func createYellowSquare() {
self.yellowSquare.frame = ballEmptyR1B1.frame //Define size of UiView
self.yellowSquare.backgroundColor = colorYellow //Set colors
self.numberGrid1.insertSubview(yellowSquare, aboveSubview: numberGrid1) //Insert Yellow Square
}
// Animate the ball pressed number to empty slot
func animateBalls() {
UIView.animateWithDuration(
0.200, delay: 0, usingSpringWithDamping: 0.950, initialSpringVelocity: 5.0, options: [], animations: {
//Convert the center points
let yellowUIViewCoordinates = self.yellowSquare.convertPoint(self.yellowSquare.center, toView: self.ballEmptyR1B1)
print("Yellow coord = \(yellowUIViewCoordinates.x)")
print("Yellow coord = \(yellowUIViewCoordinates.y)")
//CGAffine Transform to new x y co-ordinates
self.yellowSquare.transform = CGAffineTransformMakeTranslation(yellowUIViewCoordinates.x - 24, yellowUIViewCoordinates.y - 18)//Move to new center point
}, completion: { _ in
self.yellowSquare.removeFromSuperview() // Remove YellowUIView after animation is complete
self.ballEmptyR1B1.transform = CGAffineTransformMakeScale(0.5, 0.5); //Scale UIView 0.5
self.selectedNumberR1B1.transform = CGAffineTransformMakeScale(0.5, 0.5); //Scale UIView 0.5
UIView.animateWithDuration(
0.450, delay: 0.0, usingSpringWithDamping: 0.450, initialSpringVelocity: 15.0, options: [], animations: {
self.ballEmptyR1B1.transform = CGAffineTransformMakeScale(1.0, 1.0); //Scale UIView 1.0
self.selectedNumberR1B1.transform = CGAffineTransformMakeScale(1.0, 1.0); //Scale UIView 1.0
}, completion:nil)
}
)
这是一个gif rec。在slo-mo中为我所指的内容提供更好的背景: