gpt4 book ai didi

swift - UIPan识别器 : Cant present AlertController

转载 作者:行者123 更新时间:2023-11-30 13:15:56 25 4
gpt4 key购买 nike

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

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

我不知道如何解决这个问题。已经尝试将 if 语句放在代码中的不同位置,但没有成功。是否还可以访问警报窗口中的“重置”按钮以获取命令(即,如果用户按下重置,则将图片 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))

关于swift - UIPan识别器 : Cant present AlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38218766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com