UIPanRecognizer:无法显示 AlertController

UIPanRecognizer: Cant present AlertController

我是 XCode 和 Swift 的新手......但是,我想我试一试......我想要两个可拖动的图像,如果它们发生碰撞警报 window 应该弹出...我附上了下面的代码...当我 运行 代码时,我收到以下警告:

UIPanRecogniserSwift[10047:300286] 警告:尝试在已经呈现的

上呈现

我不确定如何解决这个问题。已经尝试将 if 语句放在代码中的不同位置,但没有成功。 是否还有可能访问警报 window 中的 "reset" 按钮以获取命令(即,如果用户按下重置,将图片 1 带到位置 x、y)?

import UIKit

class ViewController: UIViewController {


@IBOutlet var yellowBox: UIView!

@IBOutlet var Dice: UIImageView!



@IBAction func panYellowView(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x +       translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
     self.ifCollided()


}

/*func showAlert() {
    let  alertController = UIAlertController(title: "Collided",   message: "You have collided",preferredStyle: UIAlertControllerStyle.Alert)

    alertController .addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    alertController .addAction(UIAlertAction(title: "Reset", style: .Cancel, handler: nil))

    self.presentViewController(alertController, animated: true, completion: nil)
}*/





@IBAction func panDiceView(sender: UIPanGestureRecognizer) {

    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)


}



@IBAction func ifCollided() {

    if CGRectIntersectsRect(yellowBox.frame, Dice.frame){

        //showAlert()


            let  alertController = UIAlertController(title: "Collided", message: "You have collided",preferredStyle: UIAlertControllerStyle.Alert)

            alertController .addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            alertController .addAction(UIAlertAction(title: "Reset", style: .Cancel, handler: nil))

            self.presentViewController(alertController, animated: true, completion: nil)

            //showAlert()


    }

}



override func viewDidLoad() {
    super.viewDidLoad()



     //self.ifCollided()

    // Do any additional setup after loading the view, typically from a nib.
}







override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

此代码运行良好,您没有在骰子移动时检查 ifCollided()。此代码适用于我的 Xcode 7.3.1 iOS 9.3

import UIKit

class ViewController: UIViewController {


@IBOutlet var yellowBox: UIView!
@IBOutlet var Dice: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    Dice.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.panDiceView(_:))))
    yellowBox.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.panYellowView(_:))))
}

func panYellowView(sender: UIPanGestureRecognizer)
{
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x +       translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()
}

func showAlert()
{
    let  alertController = UIAlertController(title: "Collided",   message: "You have collided",preferredStyle: UIAlertControllerStyle.Alert)
    alertController .addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    alertController .addAction(UIAlertAction(title: "Reset", style: .Cancel, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
}

func panDiceView(sender: UIPanGestureRecognizer)
{
    let translation = sender.translationInView(self.view)
    sender.view!.center = CGPoint(x: sender.view!.center.x + translation.x, y: sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
    self.ifCollided()
}

@IBAction func ifCollided()
{
    if CGRectIntersectsRect(yellowBox.frame, Dice.frame){
        showAlert()
    }
}
}

尝试使用performSelector

调用方法
self.performSelector(#selector(self.showAlert))