Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Multithreading 如何在不同的线程上运行函数?_Multithreading_Swift_Grand Central Dispatch - Fatal编程技术网

Multithreading 如何在不同的线程上运行函数?

Multithreading 如何在不同的线程上运行函数?,multithreading,swift,grand-central-dispatch,Multithreading,Swift,Grand Central Dispatch,我需要下面的函数在不同的线程上运行。我想我必须使用并发调度队列,但我不知道如何做到这一点,所以我希望得到一些帮助 第一个功能: func respondToSwipeGesture(sender: UISwipeGestureRecognizer) { switch sender.direction { case UISwipeGestureRecognizerDirection.Left: if self.imageView.t

我需要下面的函数在不同的线程上运行。我想我必须使用并发调度队列,但我不知道如何做到这一点,所以我希望得到一些帮助

第一个功能:

func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {

            switch sender.direction {
        case UISwipeGestureRecognizerDirection.Left:
            if self.imageView.tag == 1 {
                println("1 point!")
            } else {
                if self.imageView.tag == 5 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        case UISwipeGestureRecognizerDirection.Down:
            if self.imageView.tag == 2 {
                println("1 point!")
            } else {
                if self.imageView.tag == 8 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        case UISwipeGestureRecognizerDirection.Right:
            if self.imageView.tag == 3 {
                println("1 point!")
            } else {
                if self.imageView.tag == 7 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        case UISwipeGestureRecognizerDirection.Up:
            if self.imageView.tag == 4 {
                println("1 point!")
            } else {
                if self.imageView.tag == 6 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        default:
            break
        }
 @IBAction func handleAttachmentGesture(sender: UIPanGestureRecognizer) {
    let location = sender.locationInView(self.view)
    let boxLocation = sender.locationInView(self.imageView)

    switch sender.state {
    case .Began:
      println("Your touch start position is \(location)")
      println("Start location in image is \(boxLocation)")

      // 1
      animator.removeAllBehaviors()

      // 2
      let centerOffset = UIOffset(horizontal: boxLocation.x - imageView.bounds.midX,
        vertical: boxLocation.y - imageView.bounds.midY)
      attachmentBehavior = UIAttachmentBehavior(item: imageView,
        offsetFromCenter: centerOffset, attachedToAnchor: location)

      // 3
      redSquare.center = attachmentBehavior.anchorPoint
      blueSquare.center = location

      // 4
      animator.addBehavior(attachmentBehavior)

    case .Ended:
      println("Your touch end position is \(location)")
      println("End location in image is \(boxLocation)")

      animator.removeAllBehaviors()

      // 1
      let velocity = sender.velocityInView(view)
      let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))

      if magnitude > ThrowingThreshold {
        // 2
        let pushBehavior = UIPushBehavior(items: [imageView], mode: .Instantaneous)
        pushBehavior.pushDirection = CGVector(dx: velocity.x / 10, dy: velocity.y / 10)
        pushBehavior.magnitude = magnitude / ThrowingVelocityPadding

        self.pushBehavior = pushBehavior
        animator.addBehavior(pushBehavior)

        // 3
        let angle = Int(arc4random_uniform(20)) - 10

        itemBehavior = UIDynamicItemBehavior(items: [imageView])
        itemBehavior.friction = 0.2
        itemBehavior.allowsRotation = true
        itemBehavior.addAngularVelocity(CGFloat(angle), forItem: imageView)
        animator.addBehavior(itemBehavior)

        // 4
        let timeOffset = Int64(0.4 * Double(NSEC_PER_SEC))
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeOffset), dispatch_get_main_queue()) {
            self.resetDemo()
        }
      } else {
        resetDemo()
        }

    default:
        attachmentBehavior.anchorPoint = sender.locationInView(view)
        redSquare.center = attachmentBehavior.anchorPoint
    }
第二个功能:

func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {

            switch sender.direction {
        case UISwipeGestureRecognizerDirection.Left:
            if self.imageView.tag == 1 {
                println("1 point!")
            } else {
                if self.imageView.tag == 5 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        case UISwipeGestureRecognizerDirection.Down:
            if self.imageView.tag == 2 {
                println("1 point!")
            } else {
                if self.imageView.tag == 8 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        case UISwipeGestureRecognizerDirection.Right:
            if self.imageView.tag == 3 {
                println("1 point!")
            } else {
                if self.imageView.tag == 7 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        case UISwipeGestureRecognizerDirection.Up:
            if self.imageView.tag == 4 {
                println("1 point!")
            } else {
                if self.imageView.tag == 6 {
                    println("1 point!")
                } else {
                    println("Game Over!")
                }
            }
        default:
            break
        }
 @IBAction func handleAttachmentGesture(sender: UIPanGestureRecognizer) {
    let location = sender.locationInView(self.view)
    let boxLocation = sender.locationInView(self.imageView)

    switch sender.state {
    case .Began:
      println("Your touch start position is \(location)")
      println("Start location in image is \(boxLocation)")

      // 1
      animator.removeAllBehaviors()

      // 2
      let centerOffset = UIOffset(horizontal: boxLocation.x - imageView.bounds.midX,
        vertical: boxLocation.y - imageView.bounds.midY)
      attachmentBehavior = UIAttachmentBehavior(item: imageView,
        offsetFromCenter: centerOffset, attachedToAnchor: location)

      // 3
      redSquare.center = attachmentBehavior.anchorPoint
      blueSquare.center = location

      // 4
      animator.addBehavior(attachmentBehavior)

    case .Ended:
      println("Your touch end position is \(location)")
      println("End location in image is \(boxLocation)")

      animator.removeAllBehaviors()

      // 1
      let velocity = sender.velocityInView(view)
      let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))

      if magnitude > ThrowingThreshold {
        // 2
        let pushBehavior = UIPushBehavior(items: [imageView], mode: .Instantaneous)
        pushBehavior.pushDirection = CGVector(dx: velocity.x / 10, dy: velocity.y / 10)
        pushBehavior.magnitude = magnitude / ThrowingVelocityPadding

        self.pushBehavior = pushBehavior
        animator.addBehavior(pushBehavior)

        // 3
        let angle = Int(arc4random_uniform(20)) - 10

        itemBehavior = UIDynamicItemBehavior(items: [imageView])
        itemBehavior.friction = 0.2
        itemBehavior.allowsRotation = true
        itemBehavior.addAngularVelocity(CGFloat(angle), forItem: imageView)
        animator.addBehavior(itemBehavior)

        // 4
        let timeOffset = Int64(0.4 * Double(NSEC_PER_SEC))
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeOffset), dispatch_get_main_queue()) {
            self.resetDemo()
        }
      } else {
        resetDemo()
        }

    default:
        attachmentBehavior.anchorPoint = sender.locationInView(view)
        redSquare.center = attachmentBehavior.anchorPoint
    }

不确定您正在尝试做什么(尚未阅读全部代码)。但是,如果您确定需要在不同的队列上运行,下面是一个可能会帮助您的快速示例:

let aCustomQueue = dispatch_queue_create("aCustomQueueLabel", DISPATCH_QUEUE_CONCURRENT)
let anotherCustomQueue = dispatch_queue_create("anotherCustomQueueLabel", DISPATCH_QUEUE_CONCURRENT)

dispatch_async(aCustomQueue) {

    for _ in 0...1000 {
        NSLog("Hello") // println("Hello") will print char by char
    }
}


dispatch_async(anotherCustomQueue) {

    for _ in 0...1000 {
        NSLog("World") // println("World") will print char by char
    }

}
否则,您可能需要查看:

  • 中央总调度(GCD)参考:
  • Ray Wenderlich GCD:

这两个功能都以用户界面为中心(它们处理手势识别器发送的事件)。UIKit要求所有UI工作都发生在主线程上,因此即使您可以在不同的线程上运行这些函数,这样做也没有任何意义。你到底想在这里实现什么?@ipmcc我有一个可拖动的
UIImageView
和一个滑动检测器功能,它
println
“游戏结束”或“1分”取决于用户滑动的方式。每当我添加其中一个函数时,println函数将不起作用,但使
UIImageView
可拖动的函数起作用。我没有收到任何错误消息。函数就是不响应。我想他们必须在不同的线程上被调用,但如果你说那没有任何意义,我不知道该怎么做了。谢谢!这正是我要找的!