【问题标题】:I want to move the image left and right only我只想左右移动图像
【发布时间】:2017-04-19 11:19:15
【问题描述】:

我想通过触摸移动这个船的图像。它只需要左右移动,而不需要上下移动。问题在于这个基本的代码设置,图像到处移动。

我有以下代码

import UIKit

class ViewController: UIViewController {

var boat:UIImageView!
var stone:UIImageView!

@IBOutlet weak var myView: UIView!
var location = CGPoint(x: 0, y: 0)

func start() {

boat = UIImageView(image: UIImage(named: "boat"))
boat.frame = CGRect(x: 0, y: 0, width: 60, height: 90)
boat.frame.origin.y = self.view.bounds.height - boat.frame.size.height - 10
boat.center.x = self.view.bounds.midX

self.view.addSubview(boat)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    start()
    movingStone()
    intersectsAt()



}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch : UITouch = touches.first as UITouch!

    location = touch.location(in: self.view)
    boat.center = location


}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch : UITouch = touches.first as UITouch!
       location = touch.location(in: self.view)

    boat.center = location
}

func movingStone() {


    stone = UIImageView(image: UIImage(named: "stones.png"))
    stone.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    var stone2 = 10 + arc4random() % 20

    stone.bounds = CGRect(x:10, y:10, width:40.0, height:40.0)
    stone.contentMode = .center;
    stone.layer.position = CGPoint(x: Int(stone2), y: 10)
    stone.transform = CGAffineTransform(rotationAngle: 3.142)


    self.view.insertSubview(stone, aboveSubview: myView)



    UIView.animate(withDuration: 5, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
        self.stone.frame.origin.y = self.view.bounds.height + self.stone.frame.height + 10
    }) { (success:Bool) -> Void in

        self.stone.removeFromSuperview()
        self.movingStone()

    }



}
func intersectsAt() {


  if(boat.layer.presentation()?.frame.intersects
((stone.layer.presentation()?.frame)!))! {
            boat.image = UIImage(named: "wreckboat.png")

        }

    }

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


}

【问题讨论】:

    标签: ios swift touchesbegan


    【解决方案1】:

    您只需要更新图像的 x 坐标而不是 y 坐标。

    试试这个:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch : UITouch = touches.first as UITouch!
        let loc_tmp = touch.location(in: self.view)
    
        // only use the x coordinate of the touch location
        location = CGPoint(x: loc_tmp.x, y: boat.center.y)
        boat.center = location
    
    
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch : UITouch = touches.first as UITouch!
        let loc_tmp = touch.location(in: self.view)
    
        // only use the x coordinate of the touch location
        location = CGPoint(x: loc_tmp.x, y: boat.center.y)
        boat.center = location
    }
    

    【讨论】:

    • 嘿,这行得通,如何在图像超出范围时设置边界,当我与船和图像相交时,它会抛出错误线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0
    • 你能说明你的意思吗?我没有正确理解它
    • 我已经更新了代码。如果对船的图像感兴趣,我想用触摸将船和石头相交
    • 我正在将船图像从正常更改为 wreckboat,我想我调用它为时过早,基于触摸和交叉点我想更改图像!
    • 是的,现在我从头开始,从现在开始我将使用 SpriteKit。感谢您准确回答 touchesBegan 和 location。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多