XCode 6 - 注册触摸的问题

时间:2014-11-23 20:45:09

标签: xcode swift touchesbegan

到目前为止,我有这个应用程序可以跟踪用户触摸屏幕上移动的错误时的点击次数与未命中次数。我可以在每次错过错误时注册触摸,但不会在错误被触及时注册。有人可以帮助我理解我做错了什么,并可能指出我可能的解决方案。非常感谢。

 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.
}


 }

编辑:

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 个答案:

答案 0 :(得分:0)

默认情况下,UIView及其后代禁用了用户互动,您必须明确启用它:

// 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)