XCode 6 - issue with registering a touch

258 Views Asked by At

So far I have this app that tracks the number of hits versus misses when the user touches a moving bug on the screen. I am able to register the touches for every time the bug is missed, however not when the bug is touched. Could someone help me understand what I am doing incorrectly and perhaps point me towards a possible solution. Thank you much.

 import UIKit

 class myViewController2: UIViewController {

@IBOutlet weak var lblBugAmount: UILabel!

var isBuggin = false
var hits = 0
var misses = 0


@IBOutlet weak var lblMissAmount: UILabel!
@IBOutlet weak var lblHitAmount: UILabel!

override func touchesBegan(touches: (NSSet!), withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event);
    ++misses
    --hits
    lblMissAmount.text = String(misses)
    lblHitAmount.text = String(hits)

}
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBAction func btnStop(sender: UIButton) {

    isBuggin = false
    super.viewDidLoad()

}

@IBOutlet var bugs : UIImageView!

@IBOutlet weak var numberOfBugsSlider: UISlider!

@IBAction func btnAnimate(sender: UIButton) {


    let numberOfBugs = Int(self.numberOfBugsSlider.value) //cast to Int, otherwise slider is decimal


    for loopNumber in 0...numberOfBugs+3{

        // constants for the animation
        let duration = 1.0
        let options = UIViewAnimationOptions.CurveLinear | UIViewAnimationOptions.Autoreverse

        // randomly assign a delay of 0.3 to 1s
        let delay =  NSTimeInterval( ((Int(rand()) %  1000)+100.0) / 1000.0)

        // constants for the bugs
        let size : CGFloat = CGFloat( Int(rand()) %  80 + 100.0) //sizes
        let yPosition : CGFloat = CGFloat( Int(rand()) %  200 + 20.0) + 80

        // create the bugs
        let bugs = UIImageView()
        bugs.image = UIImage(named: "bug")
        bugs.frame = CGRectMake(0-size, yPosition, size, size)
        self.view.addSubview(bugs)


        // define the animation
        UIView.animateWithDuration(duration, delay: delay, options: options, animations: {

            // move the bugs
            bugs.frame = CGRectMake(320, yPosition, size, size)

            }, completion: { animationFinished in

                // remove the bugs
                bugs.removeFromSuperview()
            })

        }


}

@IBAction func bugs(outlet: UIView) {
    ++hits
    --misses
    lblHitAmount.text = String(hits)
    lblMissAmount.text = String(misses)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


 }

EDIT:

touchesBegan & touchesMoved

 override func touchesBegan(touches: (NSSet!), withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event);

    var touch : UITouch! = touches.anyObject() as UITouch
    location = touch.locationInView(self.view)
    self.bugs = touch.locationInView(self.view)

}

override func touchesMoved(touches: (NSSet!), withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event);

    var touch : UITouch! = touches.anyObject() as UITouch
    location = touch.locationInView(self.view)
    bugs.center = location
 }
1

There are 1 best solutions below

1
On

User interaction is disabled by default for UIViews and its descendants, you have to explicitly enable it:

// create the bugs
let bugs = UIImageView()
bugs.image = UIImage(named: "bug")
bugs.frame = CGRectMake(0-size, yPosition, size, size)
bugs.userInteractionEnabled = true  // <--
self.view.addSubview(bugs)