Ios 如何在SpriteKit中创建可调整大小的框?
我想在我的项目中创建一个可调整大小的框,就像苹果页面的框一样。 一切都正常(如果我用代码中的图像调整我的框的大小,效果会很好),除了旋转框时调整框的大小(里面有图像)。 我认为这是一个算法问题,但我真的不知道如何纠正它。这是我的密码:Ios 如何在SpriteKit中创建可调整大小的框?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我想在我的项目中创建一个可调整大小的框,就像苹果页面的框一样。 一切都正常(如果我用代码中的图像调整我的框的大小,效果会很好),除了旋转框时调整框的大小(里面有图像)。 我认为这是一个算法问题,但我真的不知道如何纠正它。这是我的密码: func handlePanBox(panReco:UIPanGestureRecognizer) { var touchLoc: CGPoint touchLoc = self.scene!.convertPointFromView(panRe
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)
}
}
谢谢
编辑
我的图像位置正确:
在我通过从左向右滑动调整其大小后(可以):
旋转位置的我的图像:
在我用从左向右的滑动调整它的大小后(这是错误的):
盒子旋转时会发生什么情况?如果长方体旋转,然后调整大小与调整大小,然后旋转有什么区别吗?嗨,LearnCos2D,我用一些图片编辑我的问题来解释问题:)。在这两种情况下,拉伸发生在同一方向,但是如果你的精灵旋转90度,它自然会向上/向下伸展,而不是向左/向右。看起来像是旋转矩阵的工作。很幸运,我的代码不起作用。你知道在哪里可以找到swift中的旋转矩阵吗?