Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-拼图捕捉到位_Ios_Swift - Fatal编程技术网

Ios Swift-拼图捕捉到位

Ios Swift-拼图捕捉到位,ios,swift,Ios,Swift,因此,我正在开发一款Ipad应用程序,允许用户解决拼图游戏。我已经计算出了每一块的平移运动,但是在我想要的地方得到它们并没有正常工作。我试着让一个片段在一个小范围内快速进入它的最终目的地,然后是一个咔嗒声 下面是一段关于单个拼图的代码。当按下“我的新游戏”按钮时,图像视图将设置为相应的图片,并随机放置在画布上 @IBAction func NewGameTapped(sender: UIButton){ let bounds = UIScreen.mainScreen().bou

因此,我正在开发一款Ipad应用程序,允许用户解决拼图游戏。我已经计算出了每一块的平移运动,但是在我想要的地方得到它们并没有正常工作。我试着让一个片段在一个小范围内快速进入它的最终目的地,然后是一个咔嗒声

下面是一段关于单个拼图的代码。当按下“我的新游戏”按钮时,图像视图将设置为相应的图片,并随机放置在画布上

@IBAction func NewGameTapped(sender: UIButton){    
    let bounds = UIScreen.mainScreen().bounds
    let height = bounds.size.height
    let width = bounds.size.width

    image1.image = UIImage(named:"puzzleImage1.png")
    image1.center.x = CGFloat(100 + arc4random_uniform(UInt32(width)-300))
    image1.center.y = CGFloat(100 + arc4random_uniform(UInt32(height)-300))

    //Create Panning (Dragging) Gesture Recognizer for Image View 1
    let panRecognizer1 = UIPanGestureRecognizer(target: self, action: "handlePanning1:")

    // Add Panning (Dragging) Gesture Recognizer to Image View 1
    image1.addGestureRecognizer(panRecognizer1)
}
这就是我遇到的一些问题

func handlePanning1(recognizer: UIPanGestureRecognizer) {

    let center = dict1_image_coordinates["puzzleImage1"] as![Int]



    let newTranslation: CGPoint = recognizer.translationInView(image1)

    recognizer.view?.transform = CGAffineTransformMakeTranslation(lastTranslation1.x + newTranslation.x, lastTranslation1.y + newTranslation.y)

    if recognizer.state == UIGestureRecognizerState.Ended {
        lastTranslation1.x += newTranslation.x
        lastTranslation1.y += newTranslation.y
    }

    checkPosition(image1, center: center)
}

func checkPosition(image: UIImageView, center: [Int]){
    let distance: Double = sqrt(pow((Double(image.center.x) - Double(center[0])),2) + pow((Double(image.center.y) - Double(center[1])),2))

    //if the distance is within range, set image to new location.
    if distance <= 20{
        image.center.x = CGFloat(center[0])
        image.center.y = CGFloat(center[1])

        AudioServicesPlaySystemSound(clickSoundID)

    }
func handlePanning1(识别器:UIPanGestureRecognizer){
让中心=dict1_图像_坐标[“拼图图像1”]as![Int]
let newTranslation:CGPoint=recognizer.translationView(图像1)
recognizer.view?.transform=CGAffineTransformMakeTransation(lastTranslation1.x+newTranslation.x,lastTranslation1.y+newTranslation.y)
如果recognizer.state==UIGestureRecognizerState.Ended{
lastTranslation1.x+=newTranslation.x
lastTranslation1.y+=newTranslation.y
}
检查位置(图1,中心:中心)
}
func checkPosition(图像:UIImageView,中心:[Int]){
让距离:Double=sqrt(pow((Double(image.center.x)-Double(center[0])),2)+pow((Double(image.center.y)-Double(center[1])),2))
//如果距离在范围内,请将图像设置为新位置。

如果距离则问题可能是由这条线引起的

image1.addGestureRecognizer(panRecognizer1)
通常,人们在parentView或视图控制器的rootView(而不是image1本身)上添加手势识别器。好处是parentView从不移动,因为image1不断变换,这可能会也可能不会影响识别器。TranslationView(x)
方法返回值

改为这样做:

self.view.addGestureRecognizer(panRecognizer1)
并在
handlePlanning 1
功能中更改为此行:

image1.transform = CGAffineTransformMakeTranslation(lastTranslation1.x + newTranslation.x, lastTranslation1.y + newTranslation.y)

该问题可能是由这条线路引起的

image1.addGestureRecognizer(panRecognizer1)
通常,人们在parentView或视图控制器的rootView(而不是image1本身)上添加手势识别器。好处是parentView从不移动,因为image1不断变换,这可能会也可能不会影响识别器。TranslationView(x)方法返回值

改为这样做:

self.view.addGestureRecognizer(panRecognizer1)
并在
handlePlanning 1
功能中更改为此行:

image1.transform = CGAffineTransformMakeTranslation(lastTranslation1.x + newTranslation.x, lastTranslation1.y + newTranslation.y)

我忘了提到有很多拼图块需要移动,所以当尝试使用self.view版本时,它只注册了最后一个设置的手势识别器。这很好。你可以将同时触摸的数量设置为多个。然后使用use the Recoginator对象查找视图,以查看哪个拼图块被触摸edI忘记提到有许多拼图块需要移动,因此当尝试使用self.view版本时,它只注册了最后一个设置的手势识别器。这很好。您可以将同时触摸的数量设置为多个。然后使用use the Recoginator对象查找视图,以查看哪个拼图块被移动切德