現状のswiftの動き
①タップすると画像が現れる。
②傾けると画面領域内で加速度センサによって画像が動く。
③ドラッグすると、画像がついてくる。
④ドラッグして画面領域外に出した場合、画像が消える。 ←今回修正したい部分
⑤もしくは再度タップすると画像が消える。 ←ここは出来てます
実現したいこと
④の実現
分からない所
スクリーンサイズの取得、画像を消すと言うコードも分かるのですが、
「スクリーンサイズよりも画像が出た場合」っと言うコードの表し方が分かりません。
ご教授のほど、よろしくお願いします。
let scrSize: CGSize = UIScreen.main.bounds.size スクリーンサイズ取得。
.removeFromSuperview() 画像を消すコード
該当のソースコード
import UIKit import CoreMotion class ViewController: UIViewController { var coin = UIImageView() var coinCount = 0 @IBAction func addImage(_ sender: UITapGestureRecognizer) { if coinCount == 0 { coin = UIImageView(image: UIImage(named: "daiya")) coin.frame = CGRect(x: 0, y: 0, width: 110, height: 110) coin.center = sender.location(in: self.view) coin.contentMode = .scaleToFill coin.clipsToBounds = true coin.center = sender.location(in: self.view) UIView.animate(withDuration: 0.10, animations: { }) { (Bool) -> Void in } self.view.addSubview(coin) } else if coinCount == 1 { coin.removeFromSuperview() } coinCount = coinCount + 1 coinCount = coinCount % 2 startGame() } var index = 0 var maxIndex = 10 var dX: [CGFloat] = [] var dY: [CGFloat] = [] let cmManager = CMMotionManager() let scrSize: CGSize = UIScreen.main.bounds.size var mag: Double = 1.0 var vx, vy: Double? override func viewDidLoad() { super.viewDidLoad() vx = 0.0 vy = 0.0 dX = Array(repeating: CGFloat(0.0), count: maxIndex) dY = Array(repeating: CGFloat(0.0), count: maxIndex) startGame() } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { cmManager.stopDeviceMotionUpdates() } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { vx = 0.0 vy = 0.0 for i in 0..<dX.count { print("dx[(i)]: (dX[i]), dy[(i)]: (dY[i])") } let amp = 1.0 vx = Double(dX.reduce(0.0) { $0 + $1 } / CGFloat(dX.count)) * amp vy = Double(dY.reduce(0.0) { $0 + $1 } / CGFloat(dY.count)) * amp print("vx: ", vx!, "vy: ", vy!) startGame() } @IBAction override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { print("touchesMoved", touches.count) let aTouch = touches.first! let location = aTouch.location(in: coin) let prevLocation = aTouch.previousLocation(in: coin) var myFrame: CGRect = coin.frame let deltaX: CGFloat = location.x - prevLocation.x let deltaY: CGFloat = location.y - prevLocation.y dX[index] = deltaX dY[index] = deltaY index += 1 index %= maxIndex myFrame.origin.x += deltaX myFrame.origin.y += deltaY coin.frame = myFrame } func startGame() { cmManager.deviceMotionUpdateInterval = 0.03 let handler: CMDeviceMotionHandler = { (motionData: CMDeviceMotion?, error: Error?) -> Void in self.stepGame(motionData: motionData, error: error) } cmManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: handler) } func stepGame(motionData: CMDeviceMotion?, error: Error?) { var xMin, xMax, yMin, yMax: Int xMin = Int(coin.frame.width / 2) xMax = Int(scrSize.width) - xMin yMin = Int(coin.frame.height / 2) yMax = Int(scrSize.height) - yMin if let motion = motionData { let gravity = motion.gravity vx = vx! + gravity.x * mag vy = vy! - gravity.y * mag var x: Int = Int(Double(coin.center.x) + vx!) var y: Int = Int(Double(coin.center.y) + vy!) if (x < xMin) { x = xMin; vx = 0.0 } else if (x > xMax) { x = xMax; vx = 0.0 } if (y < yMin) { y = yMin; vy = 0.0 } else if (y > yMax) { y = yMax; vy = 0.0 } coin.center = CGPoint(x: x,y: y) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
使用している言語
swift
Xvode 11.4
ご教授のほど、よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/23 23:55
2020/04/24 00:26
2020/04/24 12:18