简体   繁体   中英

How do I fix my code for the UILongPressGestureRecognizer?

I want my hero node to move to the left when there's a long press on the left side and to the right when there a long press on the right side like in this game https://itunes.apple.com/us/app/run-bird-run/id951346475?mt=8 . I'm kind of lost on what to add next.

func beginTouchRecognizer(){
    let longPress = UILongPressGestureRecognizer(target: self,
        action: Selector("longPressTouch:"))
    longPress.minimumPressDuration = 1.0
    self.view?.addGestureRecognizer(longPress)
}

In your longPressTouch: method you can find the position of the touch and see if it is within specific areas of the view that the user is tapping on.

For example, you could do something like this:

let WidthOfTapArea: CGFloat = 100

func longPressTouch(recognizer: UILongPressGestureRecognizer) {
    let location = recognizer.locationInView(self.view)
    if location.x < WidthOfTapArea {
        // Handle a tap on the left side of the view
    } else if location.x > (self.view.frame.width - WidthOfTapArea) {
        // Handle a tap on the right side of the view
    }
}

Untested code, but it should get you going.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM