我想在我的项目中创建可调整大小的框,例如Apple Pages的框。 一切都很好(如果我调整我的盒子,我的代码中的图像可以完美地工作),除了在旋转盒子时调整盒子的大小(内部有一个图像)。 我认为这是一个算法问题,但我真的不知道如何纠正它。这是我的代码:
func handlePanBox(panReco:UIPanGestureRecognizer)
{
var touchLoc: CGPoint
touchLoc = self.scene!.convertPointFromView(panReco.locationInView(panReco.view))
if panReco.state == UIGestureRecognizerState.Began
{
coeff_drag_x_box = touchLoc.x - box_to_touch!.position.x
coeff_drag_y_box = touchLoc.y - box_to_touch!.position.y
}
else if panReco.state == UIGestureRecognizerState.Changed
{
if (box_to_touch != nil)
{
var new_pos = touchLoc
if (self.resize == true)
{
previous_drag_x_box = coeff_drag_x_box
previous_drag_y_box = coeff_drag_y_box
coeff_drag_x_box = ceil(touchLoc.x - box_to_touch!.position.x)
coeff_drag_y_box = ceil(box_to_touch!.position.y - touchLoc.y)
let box_index: Int! = box_to_touch!.name!.substringFromIndex(box_to_touch!.name!.endIndex.predecessor()).toInt()
var move_x: CGFloat?
var move_y: CGFloat?
var size_width: CGFloat?
var size_height: CGFloat?
move_x = 0
move_y = 0
size_width = 0
size_height = 0
switch box_index
{
case 0:
move_x = coeff_drag_x_box!
move_y = 0
size_width = -coeff_drag_x_box!
size_height = -coeff_drag_x_box!
case 1:
move_x = 0
move_y = 0
size_width = 0
size_height = coeff_drag_y_box!
case 2:
move_x = 0
move_y = 0
size_width = coeff_drag_y_box!
size_height = coeff_drag_y_box!
case 3:
move_x = 0
move_y = 0
size_width = -(previous_drag_x_box! - coeff_drag_x_box!)
size_height = 0
case 4:
move_x = 0
move_y = (coeff_drag_y_box! - previous_drag_y_box!)
size_width = -(coeff_drag_y_box! - previous_drag_y_box!)
size_height = -(coeff_drag_y_box! - previous_drag_y_box!)
case 5:
move_x = 0
move_y = (coeff_drag_y_box! - previous_drag_y_box!)
size_width = 0
size_height = -(coeff_drag_y_box! - previous_drag_y_box!)
case 6:
move_x = coeff_drag_x_box!
move_y = coeff_drag_x_box!
size_width = -coeff_drag_x_box!
size_height = -coeff_drag_x_box!
case 7:
move_x = coeff_drag_x_box!
move_y = 0
size_width = -coeff_drag_x_box!
size_height = 0
default:
move_x = 0
move_y = 0
size_width = 0
size_height = 0
}
if ((self.scene!.size.width + size_width!) > 100 && (self.scene!.size.height + size_height!) > 100)
{
new_pos = CGPointMake(self.frame.origin.x + move_x!, self.frame.origin.y + move_y!)
self.frame.origin = new_pos
self.scene?.size = CGSizeMake(self.scene!.size.width + size_width!, self.scene!.size.height + size_height!)
self.frame.size = CGSizeMake(self.frame.size.width + size_width!, self.frame.size.height + size_height!)
for node_child in self.scene!.children
{
var node_to_resize = node_child as? ImageSpriteNode
if (node_to_resize?.name != kBoxContactPoint && node_to_resize?.name != kPointContact && node_to_resize?.resize == true)
{
node_to_resize!.size = CGSizeMake(node_to_resize!.size.width + size_width!, node_to_resize!.size.height + size_height!)
}
}
self.create_contact_points(box_index)
}
}
}
else if panReco.state == UIGestureRecognizerState.Ended
{
box_to_touch = nil
self.create_contact_points(-1)
}
}
谢谢!
修改
我的图片位置正确:
我用左右滑动调整大小后(没关系):
旋转位置的图像:
我用左右滑动调整大小后(这是错误的):