multithreading - How to run functions on different threads? -
i need functions below run on different threads. think have use concurrent dispatch queue, i'm not sure how hoping help!
first function:
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 }
second function:
@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 \(location)") println("start location in image \(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 \(location)") println("end location in image \(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 }
not sure you're trying (haven't read whole code). still, if sure need run on different queue, here quick example might :
let acustomqueue = dispatch_queue_create("acustomqueuelabel", dispatch_queue_concurrent) let anothercustomqueue = dispatch_queue_create("anothercustomqueuelabel", dispatch_queue_concurrent) dispatch_async(acustomqueue) { _ in 0...1000 { nslog("hello") // println("hello") print char char } } dispatch_async(anothercustomqueue) { _ in 0...1000 { nslog("world") // println("world") print char char } }
otherwise, might want give @ :
- grand central dispatch (gcd) reference : https://developer.apple.com/library/prerelease/ios/documentation/performance/reference/gcd_libdispatch_ref/index.html#//apple_ref/c/func/dispatch_get_main_queue
- ray wenderlich gcd : http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1
Comments
Post a Comment