我是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.
}
}
答案 0 :(得分:0)
此代码工作正常,骰子移动时您没有检查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))